关于spring-mvc的InitBinder注解的参数
关于spring-mvc的InitBinder注解的参数
比如设置Validator。
我一直在想能不能为每个Request或者每个Action方法单独设置Validator。
也就是说Controller里有多个被@InitBinder标注的方法。 在不同的Action时被分别调用。
我注意到了@InitBinder的value参数,
api docs里是这样描述的:
The names of command/form attributes and/or request parameters that this init-binder method is supposed to apply to.
Default is to apply to all command/form attributes and all request parameters processed by the annotated handler class. Specifying model attribute names or request parameter names here restricts the init-binder method to those specific attributes/parameters, with different init-binder methods typically applying to different groups of attributes or parameters.
是乎是可以针对不同的Form对象或命令调用不同的InitBinder方法。
于是我写了下面的Controller试了一下
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@InitBinder("action1")
public void initBinder1(WebDataBinder binder){
logger.info("initBinder1");
}
@InitBinder("action2")
public void initBinder2(WebDataBinder binder){
logger.info("initBinder2");
}
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/doit", method = RequestMethod.POST, params="action1")
public String doit1(@RequestParam("value1") int value1,
@RequestParam("action1") String action, Model model) {
logger.info("value1={}",value1);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/doit", method = RequestMethod.POST, params="action2")
public String doit2(@RequestParam("value1") int value1,
@RequestParam("action2") String action, Model model) {
logger.info("value1={}",value1);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
画面上的Form是这样的
<input type="text" name="value1" value="100"/>
<input type="submit" name="action1" value="Action1"/>
<input type="submit" name="action2" value="Action2"/>
</form>
我的意愿是如果画面上,点击action1按钮择调用initBinder1方法。 如果点击action2按钮则调用initBinder2方法。
实际上也确实是这样的。
当点击action1时,logger.info("initBinder1"); 会执行
当点击action2是,logger.info("initBinder2"); 会执行
是乎是可以实现“每个Action方法单独设置Validator”这个目的。
但是其实并不是这样的。
我调式进去了InitBinder相关的源代码InitBinderDataBinderFactory, 结果发现它只是在对action这个参数绑定的时候调用了上面这个方法, 对value1绑定的时候那个方法都没有调用。而我们常常要解决的是对同一个参数对于不同的action应用不同的Validator。
所以目前还是没有好的方法能直接解决这个问题!
也许我们可以自己实现一个DataBinderFactory来解决这个问题。
关于spring-mvc的InitBinder注解的参数的更多相关文章
- spring mvc接收JSON格式的参数
1.配置spring解析json的库 <dependency> <groupId>org.codehaus.jackson</groupId> ...
- 0001 - Spring MVC中的注解
1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- Spring MVC中@RequestMapping注解使用技巧(转)
@RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...
- Spring MVC(六)--通过URL传递参数
URL传递参数时,格式是类似这样的,/param/urlParam/4/test,其中4和test都是参数,这就是所谓的Restful风格,Spring MVC中通过注解@RequestMapping ...
- Spring MVC在接收复杂集合参数
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式
ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...
随机推荐
- CLR via C# 序列化读书笔记
1. 序列化格式类 a. 二进制BinaryFormatter b. XML流 NetDataContractSerializer c. CLR类据类型与非CLR数据类型之间互操作 XmlSerial ...
- GridView中的荧光棒效果
使用 ASP.NET中的GridView控件的时候会遇到这个效果,当时觉得很神奇,其实就是两句代码的事儿,可是时间长了,有点儿忘了,今天练习一下, 顺便把删除的时候弹出js中的confirm对话框也写 ...
- python学习2——数据类型
1. python是强类型 动态类型的语言,动态类型表明它可以在声明变量的时候,不必指定数据类型,强类型规定了它不能容忍隐式类型转换 2. python中的不可变类型有:int,string,tupl ...
- 2016 Multi-University Training Contest 1 Chess 组合游戏+状压(预处理)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5724 题意:一个n*20的棋盘,n <= 1000;棋盘上有一些棋子,每颗棋子只能移动到右边的第一个 ...
- Vim配置IDE开发环境
我的vim IDE界面: 1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可:lingd@ubuntu:~/arm$sudo apt-get instal ...
- ubuntu 12.04版本出现界面终端打开broken pipe,但是tty1这些可以。
sudo apt-get remove xserver-xorg sudo apt-get install xserver-xorg
- 一些 PHP 管理系统程序中的后门
一些php网站管理程序的,一些后门,其实官方也没有恶意,主要是大家为了自己的安全. 我倒不怎么关心提示框,SABLOG怎么知道我的版本有漏洞呢,程序肯定有后门.每次登陆后台自动检测官方版本跟当前版本对 ...
- ffmpeg 从视频流中抓取图片
从视频中不断抓取图片的基本流程:打开视频流地址->获取视频流packt->解码成图片帧->输出图片 一.初始化Ffmpeg void ffmpegInit(){ av_registe ...
- mac mysql安装
一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...
- Eclipse maven工程 Missing artifact com.sun:tools:jar:1.5.0:system 解决方法
今天同事在使用eclipse,引入一个新的maven工程时报错: Missing artifact com.sun:tools:jar:1.6.0:system 这个问题很奇怪,相同的代 ...