博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC中数据绑定(转)
阅读量:7001 次
发布时间:2019-06-27

本文共 1214 字,大约阅读时间需要 4 分钟。

比如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/>

 

转自:

转载于:https://www.cnblogs.com/javafan/p/3185855.html

你可能感兴趣的文章
一步一步学Ruby(四):Ruby标准类型
查看>>
Node.js + WebSocket 实现的简易聊天室
查看>>
JSTL标签库之fn标签
查看>>
mtu检测
查看>>
在无法改动bs架构的基础上,添加新的功能(2) 浏览器
查看>>
Android 应用程序只运行一个实例
查看>>
代码整洁
查看>>
ffmpeg cmd
查看>>
网络监控
查看>>
java创建多线程的两种方法
查看>>
财务收支问题
查看>>
ADF 客户端代码调用服务器方法
查看>>
C++输入cin详解
查看>>
java与openssl的rsa算法互用
查看>>
Python strip lstrip rstrip使用方法
查看>>
Codeforces Round #268 (Div. 2) c
查看>>
如果后台的Activity由于某原因被系统回收了,如何在被系统回收之前保存当前状态?...
查看>>
postgresql 自动备份
查看>>
读写文件之日志文件
查看>>
win7 远程桌面凭证不工作
查看>>