Spring Mobile-拦截器未应用?设备为空
问题内容:
我正在尝试使用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]类型:找不到匹配的编辑器或转换策略
问题答案:
我对此进行了查看,并设法使其自行工作。所以我有几点意见。
1a)我认为过滤器和拦截器都不是必需的。我只用了过滤器就足够了。
1b)拦截器(如果使用)应在DispatcherServlet
xml配置文件中进行配置。您看起来像是通过使用来使用Struts
ActionSupport
,这是正确的吗?如果是这样,您(可能)将没有DispatcherServlet
,因此我认为此配置不会按预期工作。我认为这就是为什么要获取堆栈跟踪的原因。
2)我将添加一个断点以org.springframework.mobile.device.DeviceResolverRequestFilter.doFilterInternal
确保它正在执行。
3)我会检查Struts是否没有对进行“有趣”的操作ServletRequest
,并以某种方式对您隐藏“
currentDevice”请求属性
。实际上,如果可能的话,我会将您的代码移植到Vanilla
Spring。
4)也许您可以ServletActionContext.getRequest
在您的execute
方法中使用它,看看是否可行,并且/或者将返回的请求与中的设置进行比较setServletRequest
。
使用Spring MVC,这对我有效。我的项目称为spring-mobile-test,“ spring-mobile-test”是其上下文根:
web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<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>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/mvc/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml为空。
mvc-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="temp" />
</beans>
temp.TestController:
package temp;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
private static final Logger logger = Logger.getLogger(TestController.class);
@RequestMapping("/")
public @ResponseBody
String home(HttpServletRequest req) {
Device device = DeviceUtils.getCurrentDevice(req);
String msg = "";
if (device.isMobile()) {
msg = "Hello mobile user!";
} else {
msg = "Hello desktop user!";
}
logger.info(msg);
return msg;
}
}
当我浏览到URL时,浏览器显示以下文本http://localhost/spring-mobile-test/mvc/
:
桌面用户您好!