springMVC前台传递日期类型到后台
来源:互联网 发布:淘宝哪家阿迪衣服 编辑:程序博客网 时间:2024/06/05 00:07
在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题。
在需要日期转换的Controller中使用SpringMVC的注解@initbinder
和Spring自带的WebDateBinder
类来操作。 WebDataBinder
是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set这个值的时候,如果set的这个属性是个对象,Spring就会去找到对应的editor进行转换,然后再SET进去。
代码如下:
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
或者
@InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); simpleDateFormat.setLenient(false); CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat, true); binder.registerCustomEditor(Date.class,dateEditor); }
ServletRequestDataBinder
是WebDataBinder
的子类。
对于springboot
应用,上面的操作就可以了。
如果是传统的 spring xml 配置方式,需要:
需要在SpringMVC的配置文件加上
<!-- 解析器注册 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter"/> </list> </property> </bean> <!-- String类型解析器,允许直接返回String类型的消息 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
换种写法
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> </mvc:message-converters></mvc:annotation-driven>
拓展:
spring mvc在绑定表单之前,都会先注册这些编辑器,Spring自己提供了大量的实现类,诸如CustomDateEditor
,CustomBooleanEditor
,CustomNumberEditor
等许多,基本上够用。
使用时候调用WebDataBinder
的registerCustomEditor
方法 registerCustomEditor
源码:
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) { getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor);}
第一个参数requiredType是需要转化的类型。
第二个参数PropertyEditor是属性编辑器,它是个接口,以上提到的如CustomDateEditor等都是继承了实现了这个接口的PropertyEditorSupport类。
我们也可以不使用他们自带的这些编辑器类。
我们可以自己构造:
import org.springframework.beans.propertyeditors.PropertiesEditor;public class DoubleEditor extends PropertyEditorSupport { @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(); }}
参考http://www.cnblogs.com/soundcode/p/6519036.html
- springMVC前台传递日期类型到后台
- SpringMvc后台传递Map到前台并解析
- springMVC 前台传递json格式数据到后台
- springmvc值的传递(从后台到前台)
- SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解
- springMVC传递日期类型参数
- 前台dataGrid表单数据传递到后台
- Struts2从后台传递数据到前台
- 后台数组传递到前台<script>
- 解决SpringMvc前台传Date类型到后台报400错误问题
- 前台到后台Java时data日期类型的转化,java到数据库data日期格式转化
- springmvc框架前台传递tree和后台接收
- SpringMVC 之 后台接收参数 与前台传递数据
- SpringMVC前台传递数组,后台使用list接收
- SpringMVC 页面数据传递到后台
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- 自定义日期类型的数据绑定 前台 - 后台 或 后台 - 前台 互相转换
- Springmvc接收前台字符串类型日期格式转换
- C++中使用vector建立最大堆和最小堆
- spring多个定时器的写法
- Java RMI 框架(远程方法调用Remote Method Invoke)
- C语言strcpy()库函数的实现
- 任正非致歉华为前程序员:回来吧,公司错了
- springMVC前台传递日期类型到后台
- 设置EditText获得焦点,同时弹出小键盘
- WOJ1288-Changing String
- 基于GPU加速的二维TTI介质一阶拟声波方程有限差分地震正演模拟
- Ruby 日期 & 时间(Date & Time)
- web.xml配置文件标签元素详解
- Spring之组件bean的生命周期
- 蘑菇街小程序开发系统公司
- linux下usb传输实现