spring mvc使用@InitBinder 标签对表单数据绑定
来源:互联网 发布:淘宝新店推广方案 编辑:程序博客网 时间:2024/05/16 16:21
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。
解决的办法就是使用spring mvc提供的@InitBinder标签
在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
当然,我们也可以不使用他自己自带这些编辑器类,那下面我们自己去构造几个
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class DoubleEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Double.parseDouble(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class IntegerEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Integer.parseInt(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class FloatEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Float.parseFloat(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
- import org.springframework.beans.propertyeditors.PropertiesEditor;
- public class LongEditor extends PropertiesEditor {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- if (text == null || text.equals("")) {
- text = "0";
- }
- setValue(Long.parseLong(text));
- }
- @Override
- public String getAsText() {
- return getValue().toString();
- }
- }
在BaseController中
- @InitBinder
- protected void initBinder(WebDataBinder binder) {
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
- / binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));
- binder.registerCustomEditor(int.class, new IntegerEditor());
- / binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
- binder.registerCustomEditor(long.class, new LongEditor());
- binder.registerCustomEditor(double.class, new DoubleEditor());
- binder.registerCustomEditor(float.class, new FloatEditor());
- }
- public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {
看到没?如果你的编辑器类直接继承PropertyEditorSupport也可以。
0 0
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- Spring mvc使用@InitBinder标签对表单数据绑定
- spring mvc使用@InitBinder 标签对表单数据绑定
- 使用@InitBinder 标签对表单数据绑定
- sql排名有名次
- 学习PHP遇到的session不能删除、清空的问题,手贱啊手贱
- Android ActionBar拆分操作栏模式
- java.sql.SQLException: Value '0000-00-00 ' can not be represented as java.sql.Timest
- hdu 4941map的运用
- spring mvc使用@InitBinder 标签对表单数据绑定
- PagerAdapter、FragmentPagerAdapter、FragmentStatePagerAdapter区别
- 6种提升ASP.NET Web API性能的方法
- android 下拉列表
- Java中HashMap遍历的两种方式
- android手机无法ping通pc,netconnection失败
- [程序设计实习]时光机
- linux命令系列之iconv
- android:layout_gravity和android:gravity的区别