我是 CQ 5 移动应用程序开发的新手,我想要实现的是,当移动设备向页面发送请求时,它会捕获移动设备的属性,如移动浏览器、移动浏览器版本、移动操作系统和移动操作系统版本。我想记录这些信息,并在稍后阶段使用它来将它们分类到不同的设备组中,并在不同的设备组中呈现不同的组件。
注意我已经浏览了其中一些链接 CQ5 Mobile Development Docs
http://www.cognifide.com/blogs/cq/adobe-cq-5-5-mobile-improvements/ http://wurfl.sourceforge.net/help_doc.php
我已经尝试过抛出空指针异常的代码,因为设备具有空值。如果我在某处做错了,请纠正我
SlingHttpServletRequest slingRequest = this.request.getSlingRequest();
Device device = slingRequest.adaptTo(Device.class);
Map<String, String> deviceAttributes = device.getAttributes();
LOG.info("Request is sent from Device with OS"+deviceAttributes.get("device_os"));
这是我测试此代码的方式,每当将请求发送到具有此组件的页面时,该组件从其中一个模拟器记录移动设备的属性,我检查我的日志文件
如果我在获取移动设备属性的代码或方法中出错,请帮助我。如果您需要更多详细信息,请告诉我。
问候, 亚什
嗨,这是堆栈跟踪:
Caused by: org.apache.sling.api.SlingException: An exception occurred processing JSP page /apps/companyname/components/content/mobilebreadcrum/mobilebreadcrum.jsp at line 5
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:571)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:496)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:448)
at org.apache.sling.scripting.jsp.JspServletWrapperAdapter.service(JspServletWrapperAdapter.java:59)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:173)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:84)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:388)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:358)
... 147 more Caused by: javax.el.ELException: Error reading 'modelName' on type com.companyname.www.components.content.mobilebreadcrumb.MobileBreadCrum
at javax.el.BeanELResolver.getValue(BeanELResolver.java:66)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
at org.apache.el.parser.AstValue.getValue(AstValue.java:97)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.sling.scripting.jsp.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:923)
at org.apache.jsp.apps.companyname.components.content.mobilebreadcrum.mobilebreadcrum_jsp._jspService(mobilebreadcrum_jsp.java:160)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
... 152 more Caused by: java.lang.NullPointerException
at com.companyname.www.components.content.mobilebreadcrumb.MobileBreadCrum.getModelName(MobileBreadCrum.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
... 160 more
JSP代码是:
<%@include file="/apps/companyname/components/global.jsp"%><ct:component
className="com.companyname.www.components.content.mobilebreadcrumb.MobileBreadCrum"
name="mobilebreadcrum" />${mobilebreadcrum.modelName}
Java代码是:
public String getModelName() {
SlingHttpServletRequest slingRequest = this.request.getSlingRequest();
String resourceType = slingRequest.getResource().getResourceType();
String resourceSuperType = slingRequest.getResource().getResourceSuperType();
LOG.info("RESOURCE TYPE:"+resourceType);
LOG.info("RESOURCE SUPER TYPE:"+resourceSuperType);
Device device = request.getCurrentPage().adaptTo(Device.class);
device.getAttributes();
return modelName;
}
如果您还想要别的东西,请告诉我
问候, 亚什