关于spring-mvc的InitBinder注解的参数

通过Spring-mvc的@InitBinder注释的方法可以对WebDataBinder做一些初始化操作。
比如设置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试了一下

@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是这样的

<form action="doit" method="post" enctype="application/x-www-form-urlencoded">
    <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注解的参数的更多相关文章

  1. spring mvc接收JSON格式的参数

    1.配置spring解析json的库   <dependency>         <groupId>org.codehaus.jackson</groupId> ...

  2. 0001 - Spring MVC中的注解

    1.概述 Spring MVC框架提供了功能强大的注解,大大简化了代码开发的同时也提升了程序的可扩展性 2.注解 2.1.@RequestMapping Spring MVC通过@RequestMap ...

  3. Spring MVC 中 @ModelAttribute 注解的妙用

    Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...

  4. Spring MVC中@RequestMapping注解使用技巧(转)

    @RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...

  5. Spring MVC(六)--通过URL传递参数

    URL传递参数时,格式是类似这样的,/param/urlParam/4/test,其中4和test都是参数,这就是所谓的Restful风格,Spring MVC中通过注解@RequestMapping ...

  6. Spring MVC在接收复杂集合参数

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是applica ...

  7. Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

    Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...

  8. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  9. Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式

    ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...

随机推荐

  1. 利用ajax在javascript中获取后台的值

    <script type="text/javascript"> function login() { var sa = WebForm1.Hello().value; ...

  2. linux安装IPython四种方法

    IPython是Python的交互式Shell,提供了代码自动补完,自动缩进,高亮显示,执行Shell命令等非常有用的特性.特别是它的代码补完功能,例如:在输入zlib.之后按下Tab键,IPytho ...

  3. owin 中间件 katana 如何解密cookie

    .NET MVC5 默认的用户登录组件是AspNet.Identity ,支持owin,并且微软自己实现的一套owin 中间件叫 katana 补充一下 katana项目源码地址:https://ka ...

  4. nullptr和NULL

    nullptr是c++11中的关键字,表示空指针 要区分nullptr和NULL,首先要明白NULL的含义: NULL是一个宏定义,在c和c++中的定义不同,c中NULL为(void*)0,而c++中 ...

  5. 浅析JVM内存结构和6大区域(转)

    其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...

  6. SqlBulkCopy批量写入25万条数据只需3s

    Microsoft SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表既可以在同一个服务器上,也可以在不同服务器上).SqlBulkCopy  ...

  7. Problem 1016 咒文卷轴 优先队列+前缀和+rmq

    题目链接: 题目 Problem 1016 咒文卷轴 Time Limit: 3000 mSec Memory Limit : 131072 KB 问题描述 小Y 是一个魔法师,有一天他获得了一卷神秘 ...

  8. SVN--下载、安装VisualSVN server 服务端和 TortoiseSVN客户端

    前言: 在http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html的博客中已经很详细地介绍了SVN的服务器--VisualS ...

  9. JavaScript跨域总结与解决办法(转)

    JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首先什么是跨域 ...

  10. Windows下JNI执行步骤

    问题描述:     java JNI使用 问题解决: JNI编写步骤: (1)编写带有native声明的方法的java类     (2)使用javac编译所写的java类,然后使用javah生成扩展名 ...