springMVC自定义方法属性解析器
使用场景例子:
用户登陆系统一般会往Session里放置一个VO对象,然后在controller里会来获取用户的userId等信息。
之前的写法是:@SessionAttributes配合@ModelAttribute来进行参数值的注入,但这样需要写2个注解,其中SessionAttributes加在类上,ModelAttribute加在方法的属性上。
SpringMVC提供了HandlerMethodArgumentResolver接口来处理我们的自定义参数的解析。
例子:
1、获取用户信息的注解类
import java.lang.annotation.*;/*** <p>绑定当前登录的用户</p>* <p>不同于@ModelAttribute</p>*/ @Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface CurrentUser {/*** 当前用户在request中的名字** @return*/String value() default "loginUser";}
2、自定义的参数解析器
import com.gongren.cxht.pay.web.shiro.bind.annotation.CurrentUser; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer;/*** <p>自定义方法参数解析器*/ public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {public CurrentUserMethodArgumentResolver() {}@Overridepublic boolean supportsParameter(MethodParameter parameter) {if (parameter.hasParameterAnnotation(CurrentUser.class)) {return true;}return false;}@Overridepublic Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {CurrentUser currentUserAnnotation = parameter.getParameterAnnotation(CurrentUser.class);//从session的scope里取CurrentUser注解里的value属性值的key的valuereturn webRequest.getAttribute(currentUserAnnotation.value(), NativeWebRequest.SCOPE_SESSION);} }
3、将自定义的解析器加入springmvc的配置文件里
<mvc:annotation-driven><mvc:argument-resolvers><!-- SESSION USER --><bean class="com.test.CurrentUserMethodArgumentResolver"/></mvc:argument-resolvers> </mvc:annotation-driven>
在controller里的使用方法:
@RequestMapping(value = "/test") public String test(@CurrentUser AccUserVo user) {}
转载于:https://www.cnblogs.com/moxiaotao/p/9629249.html
springMVC自定义方法属性解析器相关推荐
- SpringMVC的视图解析器
文章目录 SpringMVC的自定义视图解析器 [1] SpringMVC的视图解析器 [2] SpringMVC的自定义视图解析器 SpringMVC自定义视图解析器的使用 [1] 目前项目资源的声 ...
- springmvc自定义参数解析器/类型转换器
概述 有些时候我们需要对GET请求的入参做自定义的处理,比较常见的就是字符串反序列化时间类型了,常用的像@DateTimeFormat注解,但是这需要在每个入参的属性上都加上这个注解,比较费手,那么我 ...
- springmvc 配置 fastjson解析器
解决方案 添加fastjson 解析器 pom文件中添加 <dependency><groupId>com.alibaba</groupId><artifac ...
- 学习SpringMVC——说说视图解析器
各位前排的,后排的,都不要走,咱趁热打铁,就这一股劲我们今天来说说spring mvc的视图解析器(不要抢,都有位子~~~) 相信大家在昨天那篇如何获取请求参数篇中都已经领略到了spring mvc注 ...
- springmvc自定义参数解析器
由于开发中一般使用参数提交方式是json格式,对于单个参数的传递使用无法接收只能自定义参数解析器处理 springmvc的自定义参数解析器实现HandlerMethodArgumentResolver ...
- jsp springmvc 视图解析器_Springmvc中多视图解析器解析问题
最近被问到过几次关于springmvc多视图解析器解析的问题:总结一下. 1.问题: 假设我有两个jsp: WEB-INF/html/a.jsp WEB-INF/report/b.jsp 且我配置了视 ...
- SpringMVC视图解析器
SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...
- jsp springmvc 视图解析器_Java面试题整理——SpringMVC
SpringMVC 1.什么是SpringMVC Spring MVC是一个MVC的开源框架,Spring MVC = Struts2+spring,Spring MVC就相当于是Struts2加上S ...
- springmvc重定向到另一个项目_SpringMVC结合Ajax、请求转发重定向、视图解析器
每日总结 一.Spring结合Ajax的处理方式 1.传统解决方案 直接输出 2.使用SpringMVC框架提供的处理方法 注意: A.方法的返回值可以是对象或者集合 B.响应给前台的数据直接是jso ...
最新文章
- 一个IT人的非典型职场十年 (2)
- mysql提取数字_Mysql中实现提取字符串中的数字的自定义函数分享
- 菜鸟教程中Java语法(Java教程+Java面向对象)
- IDEA去除mapper.xml文件中的sql语句的背景色
- 修改ubuntu的IP地址,静态IP地址
- InfoComm China 2019,揭秘“NVIDIA风格”数据科学!
- jenkins部署java项目之小白的笔记
- MySQL数据库常用的操作命令(二)
- bzoj4033: [HAOI2015]树上染色(树形dp)
- Smart ORM v0.4.1开发计划
- 线性代数【18】点积和对偶性
- vbox虚拟机无法使用计算机名称,Vbox虚拟机 所选虚拟电脑不能正常访问
- 怎么用按键精灵快速开发计算距离自己最近的怪物/包裹/金矿坐标的脚本
- 算是我看源码时的一个小技巧吧~
- C语言关闭终端回显【转】
- BST、AVL、BTree、B+Tree、B*Tree、23Tree、234Tree、TTree、RBTree、LLRBTree、AATree、SplayTree、Treap、无旋Treap、scap
- python爬取百度新闻采集教程
- MySQL页面打捞工具使用方法
- 腾讯天美游戏客户端 一面凉经
- linux系统下freeradius AAA认证服务器创建