Spring Mobile - Interceptor not applied? Device is null

此生再无相见时 提交于 2019-12-05 20:02:57

I've had a look at this and managed to get it to work myself. So I have a few comments.

1a) I don't think both the Filter and the Interceptor are required. I've just used the Filter and that was enough.

1b) The Interceptor (if used) should be configured in a DispatcherServlet xml config file. You look like you are using Struts from the use of ActionSupport, is this correct? If so, you (probably) won't have a DispatcherServlet and therefore I don't think this config will work as expected. I think that's why you're getting the stack trace.

2) I would add a breakpoint to org.springframework.mobile.device.DeviceResolverRequestFilter.doFilterInternal to make sure it's being executed.

3) I would check that Struts isn't doing something 'funny' with the ServletRequest, and hiding the "currentDevice" request attribute from you somehow. In fact, I would port your code to vanilla Spring if possible.

4) Maybe you could use ServletActionContext.getRequest in your execute method and see if that works, and/or compare the returned request to that set in setServletRequest.


Using Spring MVC, this is what works for me. My project is called spring-mobile-test, and "spring-mobile-test" is its context root:

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 is empty.

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;
    }
}

The browser shows the following text when I browse to the URL http://localhost/spring-mobile-test/mvc/:

Hello desktop user!

Adding filter in web.xml is ample enough to handle mobile device etection.

1- Use Below config in web.xml file.

<!--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- Adding below jar file

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

3- Code To check device

        Device device=DeviceUtils.getCurrentDevice(request);
        boolean isMobile = device.isMobile();
        boolean isTablet=device.isTablet();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!