Spring MVC中数据绑定(转)
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中数据绑定(转)的更多相关文章
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring MVC 中 @ModelAttribute 注解的妙用
Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...
- spring mvc中DispatcherServlet如何得到ModelAndView的
首先看下面这种张图,这张图说明了spring mvc整体的流程. 本文讲的就是如何从DispatcherServlet中得到ModerAndView的过程. 首先看DispatherServlet这个 ...
- Spring MVC 中的基于注解的 Controller(转载)
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...
- Java Web 学习(5) —— Spring MVC 之数据绑定
Spring MVC 之数据绑定 数据绑定是将用户输入绑定到领域模型的一种特性. Http 请求传递的数据为 String 类型,通过数据绑定,可以将数据填充为不同类型的对象属性. 基本类型绑定 @R ...
- Spring mvc中@RequestMapping 6个基本用法
Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: Java代码 @Reques ...
- spring mvc中使用freemark的一点心得
参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
随机推荐
- JS常用的方法
1.时间戳转换 //时间戳(有Date和无Date的都可)转换为日期 “2016年5月30日 10:29:30 2016-05-20 09:11” function TimeConversion(ti ...
- 自动生成代码工具【JAVA版】
发现任何项目无非五类操作:新增.修改.删除.查询详细.查询列表 大多数的服务端基础代码都是相同的,但是每次开发一个新项目都会做很多重复工作,从controller,bean,service,到数据库访 ...
- windows下使用php重命名目录下的文件
rename函数一直报错,最后发现是windows下文件名的编码问题,如果项目文件是utf-8的话,一定要经过一步转码 $dir = $path . '/../resource/logo'; $han ...
- python functools.wraps装饰器模块
# -*-coding=utf-8 -*-#实现一个函数执行后计算执行时间的功能 __author__ = 'piay' import time, functools def foo(): ''' 定 ...
- JDK Windows环境配置
[CLASSPATH] .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar [JAVA_HOME] D:\Java\jdk1.8.0_11 [Path] %JAVA ...
- TypeScript环境搭建
环境搭建 本篇将简单介绍一下TypeScript,并记录开发环境的搭建.使用Visual Studio Code进行一个简单的Demo开发过程. 第一部分.简介 TypeScript是一种由微软开发的 ...
- python小程序之并发连接
import threading import socket import time def conn(): cli = socket.socket() cli.connect(("58.6 ...
- js与webview 常用交互代码
常用js交互 css常用参数::: 是否允许用户选择元素的内容,选择值包括: auto:用户可以选择元素内的内容 none:用户不能选择任何内容 text:用户只能选择元素内的 ...
- IC封装形式COF介绍
其实这个真不太懂,没有太多接触也没有比较好的资料,只能简单的了解一下了. 什么是卷带式覆晶薄膜封装 COF(Chip on film) COF是一种 IC 封装技术,是运用软性基板电路(flexibl ...
- openssl对数组加密解密的完整实现代码
本例是用C实现的对一个数组进行加密,加密到第二个数组,然后解密到另一个数组的完整实现代码. #include <stdio.h> #include <string.h> #in ...