Spring MVC中数据绑定

比如Product有一个createTime属性,是java.util.Date类型。那么最简单的转型处理是,在SimpleFormController中覆盖initBinder方法。如:

@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true)); }

复用这部分代码的办法,可以编写自己的SimpleFormController子类,再以此为父类,所有表单的Controller通过继承复用。

或者,编写一个WebBindingInitializer的子类,比如:

public class MyBindingInitializer implements WebBindingInitializer { @Override public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, true)); } }

然后配置到比如SimpleFormController中:

<bean name="/save.htm" class="product.SaveProductController"> <property name="formView" value="input" /> <property name="successView" value="redirect:/browse.htm" /> <property name="commandClass" value="product.Product" /> <property name="productDao" ref="productDao" /> <property name="webBindingInitializer"> <bean class="product.MyBindingInitializer"/> </property>

<bean/>

转自:http://marshal.easymorse.com/archives/1243

Spring MVC中数据绑定(转)的更多相关文章

  1. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  2. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

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

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

  4. spring mvc中DispatcherServlet如何得到ModelAndView的

    首先看下面这种张图,这张图说明了spring mvc整体的流程. 本文讲的就是如何从DispatcherServlet中得到ModerAndView的过程. 首先看DispatherServlet这个 ...

  5. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  6. Java Web 学习(5) —— Spring MVC 之数据绑定

    Spring MVC 之数据绑定 数据绑定是将用户输入绑定到领域模型的一种特性. Http 请求传递的数据为 String 类型,通过数据绑定,可以将数据填充为不同类型的对象属性. 基本类型绑定 @R ...

  7. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  8. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  9. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

随机推荐

  1. RouteHttpMap要添加的引用

    System.Web.Routing.RouteCollection' does not contain a definition for 'MapHttpRoute' 此错的解决方式是添加 Syst ...

  2. Javascript的块级作用域

      一.块级作用域的说明 在学习JavaScript的变量作用域之前,我们应当明确几点: a.JavaScript的变量作用域是基于其特有的作用域链的. b.JavaScript没有块级作用域. c. ...

  3. Java数据库缓存思路

    为什么要用缓存?如果问这个问题说明你还是新手,数据库吞吐量毕竟有限,每秒读写5000次了不起了,如果不用缓存,假设一个页面有100个数据库操作,50个用户并发数据库就歇菜,这样最多能支撑的pv也就50 ...

  4. web开发常用样式

    1.div保持底部浮动(不受滚动条影响) position:fixed;_position:absolute;bottom:0px;_bottom:0px;_margin-top:expression ...

  5. hdu2588 gcd 欧拉函数

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. C语言中的结构体和C++中的结构体以及C++中类的区别

    c++中结构体可以定义一个函数 C中的结构体和C++中结构体的不同之处:在C中的结构体只能自定义数据类型,结构体中不允许有函数,而C++中的结构体可以加入成员函数. C++中的结构体和类的异同: 一. ...

  7. java 编码转换

    在网络中爬取到的数据,编码可能与当前编译器的编码不相同,而导致可能产生显示乱码的问题.那么如何将网络的编码,转换为当前编译器认可的编码(一般为UTF-8),就是个问题了. 主要使用了两个方法: Str ...

  8. Double Strings Solved Problem code: DOUBLE

    # Fuking silly, OTZ.... import sys def main(): n = int(raw_input()) for num in sys.stdin: if int(num ...

  9. 丰富的else语句及简洁的with语句 - 零基础入门学习Python034

    丰富的else语句及简洁的with语句 让编程改变世界 Change the world by program 丰富的else语句 有鱼油可能会说,else语句还有啥好讲的?经常跟if语句进行搭配用于 ...

  10. 异常处理:你不可能总是对的 - 零基础入门学习Python032

    异常处理:你不可能总是对的 让编程改变世界 Change the world by program 因为我们是人,不是神,所以我们经常会犯错.当然程序员也不例外,就算是经验丰富的码农,也不能保证写出来 ...