简体   繁体   中英

Adobe CQ5 request parameter binding

Does anyone know of any way of binding request parameters to objects in adobe CQ5 (like in spring MVC or other such frameworks)?

Or if there is no way to do this directly in CQ5, has anyone ever integrated a 3rd party framework to do this?

I haven't found anything that binds within CQ5 itself. You can use the Spring DataBinder classes in CQ5 though, which is what I do. Just use the org.springframework.web.bind.ServletRequestDataBinder class directly within a component like so:

ServletRequestDataBinder dataBinder = new ServletRequestDataBinder(myModel, "myModel");
dataBinder.bind(slingRequest);

You can get the org.springframework.validation.BindingResult from the dataBinder instance through the #getBindingResult() method if you want. Additionally you can configure the org.springframework.core.convert.ConversionService to use through the ServletRequestDataBinder#setConversionService(ConversionService) method.

One thing I do is also bind Sling suffixes. I follow a ${key}=${value} pattern in my URL suffixes in order to utilize dispatcher caching where possible when using Sling suffixes. For example, given a URL of /content/myApp/en_US/myPage.html/key1=value1/key2=value2.html , we parse the suffix into a Map<String, String> of {"key1":"value1","key2":"value2"} . You can bind these values by extending the ServletRequestDataBinder and overriding the #addBindValues(MutablePropertyValues mpvs, ServletRequest request) method. For example:

public class SlingServletRequestDataBinder extends ServletRequestDataBinder {
  public SlingServletRequestDataBinder(Object target, String objectName) {
    super(target, objectName);
  }

  @Override
  protected void addBindValues(MutablePropertyValues mpvs,
      ServletRequest request) {
    SlingHttpServletRequest slingHttpServletRequest = unwrap(request);

    if (null == slingHttpServletRequest) {
      return;
    }

    String suffix = slingHttpServletRequest.getRequestPathInfo().getSuffix();
    Map<String, String> suffixParameters = // parse suffix into Map here...
    mpvs.addPropertyValues(suffixParameters);

    super.addBindValues(mpvs, request);
  }

  public static SlingHttpServletRequest unwrap(ServletRequest servletRequest) {
    // immediate termination if we found one
    if (servletRequest instanceof SlingHttpServletRequest) {
      return (SlingHttpServletRequest) servletRequest;
    }

    while (servletRequest instanceof ServletRequestWrapper) {
      servletRequest = ((ServletRequestWrapper) servletRequest).getRequest();

      // immediate termination if we found one
      if (servletRequest instanceof SlingHttpServletRequest) {
        return (SlingHttpServletRequest) servletRequest;
      }
    }

    return null;
  }
}

The above should work with a combination of a Sling suffix and URL parameters. For example, given this model:

public class MyModel {
  private String key1;
  private String key2;
  private String key3;
  // getters and setters go here...
}

and a URL of /content/myApp/en_US/myPage.html/key1=value1/key2=value2.html?key3=value3 , your model will end up as {"key1":"value1", "key2":"value2", "key3":"value3"} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM