我正在尝试使用 Spring Mobile,但我似乎无法让基本示例正常工作。我有一种感觉,我错过了一些非常简单的东西,但我不知道它是什么。这是我所拥有的......
在 web.xml 中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>deviceResolverRequestFilter</filter-name>
<filter-class>org.springframework.mobile.device.DeviceResolverRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>deviceResolverRequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在 applicationContext.xml
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:device="http://www.springframework.org/schema/mobile/device"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mobile/device
http://www.springframework.org/schema/mobile/device/spring-mobile-device-1.0.xsd">
<!-- Interceptors that execute common control logic across multiple requests -->
<interceptors>
<!-- Detects the client's Device -->
<beans:bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</interceptors>
</beans:beans>
在我的 Java 类中:
public class TestAction extends ActionSupport implements ServletRequestAware {
// So that we can lookup the current Device
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public String execute() {
Device currentDevice = DeviceUtils.getCurrentDevice(request);
if (currentDevice.isMobile()) // <-- fails here with NPE
为什么设备未设置并导致为空?
编辑:日志文件似乎表明设置拦截器有问题,但我仍然不确定我哪里出错了。
无法将“org.springframework.mobile.device.DeviceResolverHandlerInterceptor”类型的值转换为所需的“org.springframework.web.context.request.WebRequestInterceptor”类型;嵌套异常是 java.lang.IllegalStateException:无法将类型 [org.springframework.mobile.device.DeviceResolverHandlerInterceptor] 的值转换为所需类型 [org.springframework.web.context.request.WebRequestInterceptor]:找不到匹配的编辑器或转换策略