有没有人知道将请求参数绑定到adobe CQ5中的对象的任何方式(如在Spring MVC或其他此类框架中)?
或者如果在CQ5中没有办法直接这样做,有没有人集成第三方框架来做到这一点?
答案 0 :(得分:1)
我没有发现任何与CQ5本身有关的东西。您可以使用CQ5中的Spring DataBinder类,这就是我所做的。只需在组件中直接使用org.springframework.web.bind.ServletRequestDataBinder类,如下所示:
ServletRequestDataBinder dataBinder = new ServletRequestDataBinder(myModel, "myModel");
dataBinder.bind(slingRequest);
如果需要,可以通过#getBindingResult()方法从dataBinder实例获取org.springframework.validation.BindingResult。另外,您可以配置org.springframework.core.convert.ConversionService以通过ServletRequestDataBinder #setConversionService(ConversionService)方法使用。
我做的一件事也是绑定Sling后缀。我在URL后缀中遵循$ {key} = $ {value}模式,以便在使用Sling后缀时尽可能利用调度程序缓存。例如,如果网址为 /content/myApp/en_US/myPage.html/key1=value1/key2=value2.html ,我们会将后缀解析为Map< String,String> {“key1”:“value1”,“key2”:“value2”} 。您可以通过扩展ServletRequestDataBinder并覆盖#addBindValues(MutablePropertyValues mpvs,ServletRequest请求)方法来绑定这些值。例如:
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;
}
}
以上内容应与Sling后缀和URL参数结合使用。例如,给定此模型:
public class MyModel {
private String key1;
private String key2;
private String key3;
// getters and setters go here...
}
以及 /content/myApp/en_US/myPage.html/key1=value1/key2=value2.html?key3=value3 的网址,您的模型最终会以 {“结尾key1“:”value1“,”key2“:”value2“,”key3“:”value3“} 。