我正在尝试使用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
为什么设备未设置并导致为空?
编辑: 日志文件似乎表明设置拦截器存在问题,但我仍然不确定哪里出错了。
2012-05-29 09:36:36,696 DEBUG [ConstructorResolver.java:201] : 忽略构造函数[public org.springframework.web.servlet.handler.MappedInterceptor(java.lang.String[],org.springframework.web.context.request.WebRequestInterceptor)] 的bean 'org.springframework.web.servlet.handler.MappedInterceptor#0': org.springframework.beans.factory.UnsatisfiedDependencyException: 创建bean时发生错误 'org.springframework.web.servlet.handler.MappedInterceptor#0': 通过构造函数参数表达的不满足的依赖项 索引1的类型为 [org.springframework.web.context.request.WebRequestInterceptor]: 无法将类型为 [org.springframework.mobile.device.DeviceResolverHandlerInterceptor] 的构造函数参数值转换为所需类型 [org.springframework.web.context.request.WebRequestInterceptor]: 无法将类型为 '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]: 找不到匹配的编辑器或转换策略。
web.xml
内容。我的过滤器设置反了!你的建议#2帮助我在正确的位置设置断点,以认识到我的错误。而且我相信我需要过滤器和拦截器才能让我的应用程序工作...至少在我的情况下是这样。 - nmc