栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Mobile-拦截器未应用?设备为空

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Mobile-拦截器未应用?设备为空

我对此进行了查看,并设法使其自行工作。所以我有几点意见。

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;@Controllerpublic 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/

桌面用户您好!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号