拦截器获取返回值中怎么获取pos

struts2&Interceptor(拦截器)中获取HTTP&参数的方法
HttpServletRequest request= (HttpServletRequest)
actionContext.get(StrutsStatics.HTTP_REQUEST);
System.out.println("Here"+request.getParameter("goods_id"));
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
Spring拦截器中通过request获取到该请求对应Controller中的method对象
摘要:背景:项目使用Spring3.1.0.RELEASE,从dao到Controller层全部是基于注解配置。我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Controller中的目标method方法对象。Controller和拦截器代码如下:&AdminController&@(&/admin&)publicclassAdminController{/***init:初
背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置。我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Controller中的目标method方法对象。Controller和拦截器代码如下:
AdminController
@(&/admin&)public class AdminController {/** * init:初始页面. &br/& * * @author chenzhou * @param request 请求 * @param response 响应 * @return 登陆页 * @since JDK 1.6 */@RequestMapping(&/init&)public ModelAndView init(HttpServletRequest request,HttpServletResponse response){Map&String, Object& model = new HashMap&String, Object&();List&Role& roleList = this.adminService.getRoleList();model.put(&roleList&, roleList);return new ModelAndView(this.getLoginPage(), model);}//……}&
LoginInterceptor
public class LoginInterceptor extends HandlerInterceptorAdapter {/** * This implementation always returns &code&true&/code&. */public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {}/** * This implementation is empty. */public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {}/** * This implementation is empty. */public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {}}
servlet xml配置文件定义:
/& &bean /&&bean &&property&&list&&bean /&&/list&&/property&&/bean&&&
& & &我的需求是想在preHandle方法中通过request获取该请求访问的目标Controller中的方法对象。之前找了很久也没有找到比较好的方案,就采取了最老土的通过比较requestURL和Controller类和方法上的RequestMappingURL来进行获取,这样也能勉强实现,但是这种方式我自己都觉得特别恶心。首先,这种方式需要使用反射来获取Controller中的所有方法,然后遍历method数组,逐个进行RequestMappingURL的比对,效率低下。其次,如果RequestMapping定义了类似于@RequestMapping(&/{id}&)这种动态参数url,则无法进行比较。
& & &因为上面这种方式不好,我就一直想找一个更好的方案。不得已只能向人求助,第一个就想到了Iteye上对于Spring研究得很熟悉的jinnianshilongnian龙年兄,我相信经常上iteye的博友们对龙年兄应该都很熟悉。龙年兄给了我一个方案,就是通过把handler对象转换为HandlerMethod类型,然后直接getMethod,代码如下:
/** * This implementation always returns &code&true&/code&. */public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {System.out.println(&*********************preHandle********************&);System.out.println(handler.getClass());HandlerMethod handlerMethod = (HandlerMethod)System.out.println(handlerMethod.getMethod());}
注:HandlerMethod类是Spring 3.1.0.RELEASE版本中才有的,之前我使用的Spring 3.0.6.RELEASE版本,里面是找不到这个类的
根据龙年兄提供的方法,测试之后报错,报错信息如下:
*********************preHandle********************class com.chenzhou.examples.erm.web.AdminController 16:28:25 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet erm threw exceptionjava.lang.ClassCastException: com.chenzhou.examples.erm.web.AdminController cannot be cast to org.springframework.web.method.HandlerMethodat com.chenzhou.examples.erm.util.interceptor.LoginInterceptor.preHandle(LoginInterceptor.java:37)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:891)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)……
& & 根据错误提示可以看出是HandlerMethod handlerMethod = (HandlerMethod)这一步报错了,根据System.out.println(handler.getClass());打印的结果可以得知handler是该请求访问的Controller类,无法转换成HandlerMethod对象。这次还是龙年兄帮我找出了原因,解决方案是使用
&bean &&替换&&bean /&&
因为DefaultAnnotationHandlerMapping只能返回Controller对象,不会映射到Controller中的方法级别。替换之后servlet xml配置如下:
/& &bean /&&bean & &property&&list&&bean /&&/list&&/property&&/bean&
& & 重启tomcat测试之后发现再次报错,报了另外一个错误,具体信息如下:
16:39:39 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet erm threw exceptionjavax.servlet.ServletException: No adapter for handler [public org.springframework.web.servlet.ModelAndView com.chenzhou.examples.erm.web.AdminController.init(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]: Does your handler implement a supported interface like Controller?……
& & 这一次,请求根本没有到达拦截器容器就已经报错了,错误提示的意思是找不到handler对象对应的Adapter类。我在RequestMappingHandlerMapping类对应的spring-webmvc-3.1.0.RELEASE.jar 包里找到了该类对应的Adapter类:RequestMappingHandlerAdapter,然后在servlet xml中进行了配置:
/& &bean /&&bean & &property&&list&&bean /&&/list&&/property&&/bean&&bean /&
& & 然后重新启动tomcat后访问http://localhost:8080/erm/admin/init 结果正常,控制台日志信息如下:
*********************preHandle********************class org.springframework.web.method.HandlerMethodpublic org.springframework.web.servlet.ModelAndView com.chenzhou.examples.erm.web.AdminController.init(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
& & 从日志信息可以看出,handler对象在经过类型转换后转换成了HandlerMethod类型,通过handler.getMethod方法,获取到了该请求访问的方法为com.chenzhou.examples.erm.web.AdminController.init
& & &注:非常感谢jinnianshilongnian 开涛兄的帮助。
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
Spring拦截器中通过request获取到该请求对应Controller中的method对象相关信息,包括
的信息,所有Spring拦截器中通过request获取到该请求对应Controller中的method对象相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
Internationalspring MVC
拦截器怎么获取controller返回值
[问题点数:40分,无满意结帖,结帖人u]
本版专家分:0
CSDN今日推荐
本版专家分:0
本版专家分:351
本版专家分:10
本版专家分:36284
2017年 总版技术专家分年内排行榜第九
2017年11月 Java大版内专家分月排行榜第一2017年10月 Java大版内专家分月排行榜第一2017年9月 Java大版内专家分月排行榜第一2017年8月 Java大版内专家分月排行榜第一2017年7月 Java大版内专家分月排行榜第一2017年6月 Java大版内专家分月排行榜第一2017年5月 Java大版内专家分月排行榜第一2017年4月 Java大版内专家分月排行榜第一2017年3月 Java大版内专家分月排行榜第一2017年2月 Java大版内专家分月排行榜第一
2017年12月 Java大版内专家分月排行榜第三
本版专家分:0
本版专家分:0
匿名用户不能发表回复!|
CSDN今日推荐struts2 拦截器Interceptor中取得request
public class AuthInterceptor extends MethodFilterInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!当前位置: →
→ struts2里能不能在拦截器中获取封装的数据如何获取,求指点
struts2里能不能在拦截器中获取封装的数据如何获取,求指点
& 作者:佚名 & 来源: 互联网 & 热度:
&收藏到→_→:
摘要: struts2里能不能在拦截器中获取封装的数据?怎么获取,求指点!页面&s:form action=&login.action&& & &...
"struts2里能不能在拦截器中获取封装的数据如何获取,求指点"::
struts2里能不能在拦截器中获取封装的数据?怎么获取,求指点!页面&s:form action=&login.action&& & &s:textfield name=&user.name& label=&请输入用户名&&&/s:textfield& & &s:password name=&user.password& label=&请输入密 码&&&/s:password& & &s:submit value=&提交&&&/s:submit& & &/s:form&loginaction:(有get、set方法,user为实体类,有name和password两个属性)public string execute() throws exception { & //........}我知道怎么在拦截器里获得表单里的数据,可是获得封装的user数据就不知道怎么弄了,求大神指点一下& ------解决方案--------------------
哦 ,我懂了,你在action中定义的user封装后可以接收到user对象,那么同样的,你在interceptor中也可以定义user之后封装就可以获取了嘛。。struts都封装好了。
------解决方案--------------------
探讨哦 ,我懂了,你在action中定义的user封装后可以接收到user对象,那么同样的,你在interceptor中也可以定义user之后封装就可以获取了嘛。。struts都封装好了。 搜索此文相关文章:此文来自: 马开东博客
网址: 站长QQ
上一篇:没有了
struts2里能不能在拦截器中获取封装的数据如何获取,求指点_java其他相关文章
java其他_总排行榜
java其他_最新
java其他_月排行榜
java其他_周排行榜
java其他_日排行榜

我要回帖

更多关于 拦截器获取session 的文章

 

随机推荐