springMVC中时间参数自动绑定失败问题

一、报错页面:

二、解决方案:

方案一、二、三前提:

(1)jsp页面:

<tr>
	<td>商品生产日期</td>
	<td><input type="text" name="createtime" value="<fmt:formatDate value='${item.createtime}' type='date' pattern='yyyy-MM-dd HH:mm:ss'/>" ></td>
</tr>

方案一:

(1)在springmvc.xml中要开启注解适配器:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

(2)对应提交URL的controller类中添加自定义映射字段方法:

	 /**
	  * 注册属性编辑器(字符串转日期)
	  * @param binder
	  */
	  @InitBinder
	 public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//是否允许为空
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
	}

方案二:

(1)对应VO类中的字段添加注解:

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createtime;

(2)在springmvc.xml中开启<mvc:annotation-driven>注解:

<mvc:annotation-driven></mvc:annotation-driven>

方案三:

(1)自定义Converter类:

//需要实现Converter接口,这里是将String类型转换成Date类型
public class DateConverter implements Converter<String, Date> {
 
    @Override
    public Date convert(String source) {
        //实现将字符串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)
        try {
        	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果参数绑定失败返回null
        return null;
    }

(2)在配置文件中配置注解适配器(方式一)

    <!--注解适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		 <property name="webBindingInitializer" ref="customBinder"></property> 
	</bean>
	
	<!-- 自定义webBinder转换器 -->
	<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
		<property name="conversionService" ref="conversionService" />
	</bean>
	
	<!-- conversionService -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!-- 转换器 -->
		<property name="converters">
			<list>
				<bean class="com.audrey.springMVC.tools.utils.DateConverter"/>
			</list>
		</property>
	</bean>

(2)在配置文件中配置注解适配器(方式二)

	<!--注解映射器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
	
    <!--注解适配器 -->
<!-- 	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		 <property name="webBindingInitializer" ref="conversionService"></property> 
	</bean> -->

    <!-- 方式二:自定义Date转化器 -->
	<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
	<!-- conversionService -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!-- 转换器 -->
		<property name="converters">
			<list>
				<bean class="com.audrey.springMVC.tools.utils.DateConverter"/>
			</list>
		</property>
	</bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值