问题描述
- springmvc自定义日期类型绑定问题
-
我在用springmvc自定义日期类型绑定,但是根本debug的时候进入不了自定义转换的类DateConverter中,也就是没有转换,我的birthday参数前后台都是birthday,用得是extjs4.0
源码如下 求解决<context:component-scan base-package="cn.qs.ctrl"></context:component-scan> <mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 转换器 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="cn.qs.ctrl.converter.DateConverter"></bean> </set> </property> </bean> public class DateConverter implements Formatter<Date> { @Override public String print(Date arg0, Locale arg1) { return null; } @Override public Date parse(String text, Locale locale) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(text); } catch (Exception e) { format = new SimpleDateFormat("yyyy-MM-dd"); date = format.parse(text); } return date; } } @ResponseBody @RequestMapping(value="/addStu",produces = {"text/json;charset=UTF-8"}) public String addStu(Student student) throws ParseException { this.studentService.add(student); String response = "{success:true"+",id:"+student.getId()+"}"; System.out.println(response); return response; } public class Student implements Serializable{ private static final long serialVersionUID = 8277935630491910878L; @Expose private Long id; @Expose private String name; @Expose private Date birthday; @Expose private String address; private String startBirth; private String endBirth; public String getStartBirth() { return startBirth; } public void setStartBirth(String startBirth) { this.startBirth = startBirth; } public String getEndBirth() { return endBirth; } public void setEndBirth(String endBirth) { this.endBirth = endBirth; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", birthday=" + birthday + ", address=" + address + "]"; } }
解决方案
SpringMVC自定义日期类型的数据绑定
SpringMVC自定义日期类型的数据绑定
SpringMVC自定义日期类型的数据绑定
解决方案二:
貌似时间类型不需要写自定义类型转换器吧,SpringMVC貌似提供了……我博客里好像写过,你可以看一下 http://mritd.me
解决方案三:
http://www.th7.cn/Program/java/201312/163370.shtml
时间: 2024-11-01 19:23:41