java拦截器没有生效_java-春季移动-拦截器没有应用?设备为空

我正在尝试使用Spring Mobile,但似乎无法使基本示例正常工作.我觉得我缺少一些愚蠢的简单东西,但我不知道它是什么.这就是我所拥有的…

在web.xml中

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

deviceResolverRequestFilter

org.springframework.mobile.device.DeviceResolverRequestFilter

deviceResolverRequestFilter

/*

在applicationContext.xml中

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">

在我的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()) //

为什么未设置设备并导致其为空?

编辑:

日志文件似乎表明设置拦截器存在问题,但我仍然不确定出错的地方.

2012-05-29 09:36:36,696 DEBUG [ConstructorResolver.java:201] :

Ignoring constructor [public

org.springframework.web.servlet.handler.MappedInterceptor(java.lang.String[],org.springframework.web.context.request.WebRequestInterceptor)]

of bean ‘org.springframework.web.servlet.handler.MappedInterceptor#0’:

org.springframework.beans.factory.UnsatisfiedDependencyException:

Error creating bean with name

‘org.springframework.web.servlet.handler.MappedInterceptor#0’:

Unsatisfied dependency expressed through constructor argument with

index 1 of type

[org.springframework.web.context.request.WebRequestInterceptor]: Could

not convert constructor argument value of type

[org.springframework.mobile.device.DeviceResolverHandlerInterceptor]

to required type

[org.springframework.web.context.request.WebRequestInterceptor]:

Failed to convert value of type

‘org.springframework.mobile.device.DeviceResolverHandlerInterceptor’

to required type

‘org.springframework.web.context.request.WebRequestInterceptor’;

nested exception is java.lang.IllegalStateException: Cannot convert

value of type

[org.springframework.mobile.device.DeviceResolverHandlerInterceptor]

to required type

[org.springframework.web.context.request.WebRequestInterceptor]: no

matching editors or conversion strategy found

解决方法:

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

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:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

deviceResolverRequestFilter

org.springframework.mobile.device.DeviceResolverRequestFilter

deviceResolverRequestFilter

/*

mvc

org.springframework.web.servlet.DispatcherServlet

1

mvc

/mvc/*

applicationContext.xml为空.

mvc-servlet.xml:

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">

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 /时,浏览器显示以下文本:

Hello desktop user!

标签:spring-mobile,spring,java,spring-mvc

来源: https://codeday.me/bug/20191201/2079574.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值