spring mvc使用@InitBinder 标签对表单数据绑定
2023-09-27 14:27:16 时间
在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定。
解决的办法就是使用spring mvc提供的@InitBinder标签
在我的项目中是在BaseController中增加方法initBinder,并使用注解@InitBinder标注,那么spring mvc在绑定表单之前,都会先注册这些编辑器,当然你如果不嫌麻烦,你也可以单独的写在你的每一个controller中。剩下的控制器都继承该类。spring自己提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等许多,基本上够用。
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后再SET进去。
当然,我们也可以不使用他自己自带这些编辑器类,那下面我们自己去构造几个
1 import org.springframework.beans.propertyeditors.PropertiesEditor; 2 3 public class DoubleEditor extends PropertiesEditor { 4 @Override 5 public void setAsText(String text) throws IllegalArgumentException { 6 if (text == null || text.equals("")) { 7 text = "0"; 8 } 9 setValue(Double.parseDouble(text)); 10 } 11 12 @Override 13 public String getAsText() { 14 return getValue().toString(); 15 } 16 }
1 import org.springframework.beans.propertyeditors.PropertiesEditor; 2 3 public class FloatEditor extends PropertiesEditor { 4 @Override 5 public void setAsText(String text) throws IllegalArgumentException { 6 if (text == null || text.equals("")) { 7 text = "0"; 8 } 9 setValue(Float.parseFloat(text)); 10 } 11 12 @Override 13 public String getAsText() { 14 return getValue().toString(); 15 } 16 }
1 import org.springframework.beans.propertyeditors.PropertiesEditor; 2 3 public class IntegerEditor extends PropertiesEditor { 4 @Override 5 public void setAsText(String text) throws IllegalArgumentException { 6 if (text == null || text.equals("")) { 7 text = "0"; 8 } 9 setValue(Integer.parseInt(text)); 10 } 11 12 @Override 13 public String getAsText() { 14 return getValue().toString(); 15 } 16 }
1 import org.springframework.beans.propertyeditors.PropertiesEditor; 2 3 public class LongEditor extends PropertiesEditor { 4 @Override 5 public void setAsText(String text) throws IllegalArgumentException { 6 if (text == null || text.equals("")) { 7 text = "0"; 8 } 9 setValue(Long.parseLong(text)); 10 } 11 12 @Override 13 public String getAsText() { 14 return getValue().toString(); 15 } 16 }
在BaseController中
1 @InitBinder 2 protected void initBinder(WebDataBinder binder) { 3 binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); 4 / binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true)); 5 binder.registerCustomEditor(int.class, new IntegerEditor()); 6 / binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true)); 7 binder.registerCustomEditor(long.class, new LongEditor()); 8 binder.registerCustomEditor(double.class, new DoubleEditor()); 9 binder.registerCustomEditor(float.class, new FloatEditor()); 10 }
如果你的编辑器类直接继承PropertyEditorSupport也可以。
相关文章
- springboot项目:以run as-->spring boot app方式启动,配置热部署(亲测可用!!!)
- Spring、Hibernate 数据不能插入到数据库问题解决
- 护网必备技能:Spring Boot 接口数据加解密 功能实现
- 《Spring 5 官方文档》5. 验证、数据绑定和类型转换(一)
- Spring Boot 多数据配置更新
- spring bean
- 《精通Spring MVC 4》——1.9 小结
- Spring boot(四)之保存数据到数据库
- Spring Boot + Mybatis 实现动态数据源
- spring框架返回json数据null转空字符串
- Java spring mvc jsp excel 导入
- Spring+Ibatis集成开发实例
- spring cloud 搭建(JPA数据访问)
- 【spring-bean】浅谈Spring的依赖注入(基础)
- Spring Boot @Autowired 没法自动注入的问题
- spring boot integrated mybatis three ways!--转
- spring boot 集成 mybatis,数据库为mysql
- spring源码分析之cache demo
- Use Spring Insight Developer to Analyze Code, Install it with Tomcat, and Extend it with Plugins--转载
- Spring RESTFul Client – RestTemplate Example--转载
- 一篇文章全面解析大数据批处理框架Spring Batch
- 整合SSM时报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;