Spring Mobile - Interceptor没有应用?设备为空

时间:2012-05-29 15:06:00

标签: java spring spring-mvc 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

为什么设备未设置且结果为空?

编辑: 日志文件似乎表明设置拦截器有问题,但我仍然不确定我哪里出错了。

  

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&#39; org.springframework.web.servlet.handler.MappedInterceptor#0&#39;:   org.springframework.beans.factory.UnsatisfiedDependencyException:   使用名称创建bean时出错   &#39; org.springframework.web.servlet.handler.MappedInterceptor#0&#39 ;:   通过构造函数参数表示不满意的依赖关系   索引1的类型   [org.springframework.web.context.request.WebRequestInterceptor]:可以   不转换类型的构造函数参数值   [org.springframework.mobile.device.DeviceResolverHandlerInterceptor]   要求的类型   [org.springframework.web.context.request.WebRequestInterceptor]:   无法转换类型的值   &#39; org.springframework.mobile.device.DeviceResolverHandlerInterceptor&#39;   要求的类型   &#39; org.springframework.web.context.request.WebRequestInterceptor&#39 ;;   嵌套异常是java.lang.IllegalStateException:无法转换   类型的价值   [org.springframework.mobile.device.DeviceResolverHandlerInterceptor]   要求的类型   [org.springframework.web.context.request.WebRequestInterceptor]:没有   找到匹配的编辑器或转换策略

2 个答案:

答案 0 :(得分:3)

我已经看过这个,并设法让它自己工作。所以我有一些意见。

1a)我不认为过滤器和拦截器都是必需的。 I've just used the Filter and that was enough

1b)拦截器(如果使用)should be configured in a DispatcherServlet xml config file。你看起来好像是在使用ActionSupport使用Struts,这是正确的吗?如果是这样,你(可能)将没有DispatcherServlet,因此我不认为此配置将按预期工作。我认为这就是你获得堆栈跟踪的原因。

2)我会在org.springframework.mobile.device.DeviceResolverRequestFilter.doFilterInternal添加一个断点,以确保它被执行。

3)我会检查Struts没有对ServletRequest做一些“有趣”的事情,并以某种方式隐藏"currentDevice" request attribute。事实上,如果可能的话,我会将你的代码移植到vanilla Spring。

4)也许您可以在execute方法中使用ServletActionContext.getRequest,看看是否有效,和/或将返回的请求与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;
    }
}

当我浏览到网址http://localhost/spring-mobile-test/mvc/时,浏览器会显示以下文字:

  

Hello桌面用户!

答案 1 :(得分:0)

在web.xml中添加过滤器足以处理移动设备检测。

1-在web.xml文件中使用以下配置。

<!--Device Resolver filter start here  -->
<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>  
   

2-在jar文件下面添加

        spring-mobile-device-1.x.x.RELEASE.jar

3-代码检查设备

        Device device=DeviceUtils.getCurrentDevice(request);
        boolean isMobile = device.isMobile();
        boolean isTablet=device.isTablet();