Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>ServletRequestDataBinder dataBinder = new ServletRequestDataBinder(myModel, "myModel"); dataBinder.bind(slingRequest); </code></pre> <p>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.</p> <p>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 <strong>/content/myApp/en_US/myPage.html/key1=value1/key2=value2.html</strong>, we parse the suffix into a Map&lt;String, String&gt; of <strong>{"key1":"value1","key2":"value2"}</strong>. You can bind these values by extending the ServletRequestDataBinder and overriding the #addBindValues(MutablePropertyValues mpvs, ServletRequest request) method. For example:</p> <pre><code>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&lt;String, String&gt; 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; } } </code></pre> <p>The above should work with a combination of a Sling suffix and URL parameters. For example, given this model:</p> <pre><code>public class MyModel { private String key1; private String key2; private String key3; // getters and setters go here... } </code></pre> <p>and a URL of <strong>/content/myApp/en_US/myPage.html/key1=value1/key2=value2.html?key3=value3</strong>, your model will end up as <strong>{"key1":"value1", "key2":"value2", "key3":"value3"}</strong>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload