springmvc自定义参数绑定实现日期参数绑定
来源:互联网 发布:淘宝怎么进行实名认证 编辑:程序博客网 时间:2024/05/21 07:01
springmvc默认的参数绑定对日期类型是无效。对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。将String类型转化为java.util.Date类型。
第一步:定义转化类,实现org.springframework.core.convert.converter.Converter接口,覆写convert方法
package ssm.controller.converter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.core.convert.converter.Converter;public class String2DateConverter implements Converter<String,Date>{ @Override public Date convert(String source) { //实现 将日期串转成日期类型(格式也可为yyyy-MM-dd HH:mm:ss) SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { //转成直接返回 return simpleDateFormat.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果参数绑定失败返回null return null; }}
第二步:向处理器适配器中注入自定义的参数绑定组件。在springmvc.xml中注入自定义的参数绑定转化器
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 配置自定义参数绑定 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- 转换器 --> <property name="converters"> <list> <!-- 注入自定义的参数绑定转化类 --> <bean class="ssm.controller.converter.String2DateConverter"/> </list> </property> </bean>
另外,在jsp页面中指定日期显示的格式
需要引入标签库:
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<TD><fmt:formatDate value="${order.date }" pattern="yyyy-MM-dd"/></TD>
<INPUT type="date" class="form-control" id=sChannel2 value="<fmt:formatDate value='${detail.date }' pattern='yyyy-MM-dd'/>" style="WIDTH: 180px" maxLength=50 name="date" >
阅读全文
0 0
- springmvc自定义参数绑定实现日期参数绑定
- SpringMVC自定义日期格式进行参数绑定
- SpringMVC自定义参数绑定
- springmvc参数绑定-自定义参数绑定
- SpringMVC实现自定义类型参数绑定
- springmvc自定义参数类型绑定
- springmvc自定义参数绑定用户信息
- SpringMVC参数绑定和自定义参数类型绑定
- SpringMVC初始化参数绑定--日期格式
- springmvc参数绑定-pojo绑定
- springmvc参数绑定-复杂绑定
- springMVC请求参数绑定
- springmvc 的参数绑定
- SpringMVC参数绑定
- springmvc参数绑定
- SpringMVC参数绑定注解
- springmvc参数绑定
- SpringMVC 参数绑定
- 试试写个博客
- 升级nodejs, npm到某一版本
- php实现兼容Unicode文字的字符串大小写转换strtolower()和strtoupper()
- history.go(-1)、history.back()区别
- 实验6:图的实验1
- springmvc自定义参数绑定实现日期参数绑定
- sql(join on 和where的执行顺序)
- cenos7操作-未完待续
- CentOS下通过yum安装svn及配置
- Spring的@Value获取不到值的问题
- RecyclerView 滚动到指定position,并置顶
- H(n) UVA
- 玫瑰花
- 我的电路实践