SpringMVC自定义绑定参数
背景:因为在前段发送日期类型的数据的时候,日期的格式有很多,但是SpringMVC在前端控制器(Controller)中的Date类型参数只能够识别并转换格式为"yyyy-mm-dd"这种格式的日期类型,所以在设置日期的时候需要自定义一种日期格式。
步骤:
1.创建转换器
package com.helong.web.convert;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//转换接收参数为日期类型的方法
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(s != null){
//设置转换参数的格式(将一个字符串类型转换为一个日期格式)
//设置之后就不能使用之前的数据格式了,如果不是按照下面的格式进行输入的话就会报错
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
2.在SpringMVC核心配置文件中自定义转换器(转换工厂和注解扫描)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.helong"/>
<!--视图解析器前/后缀设置-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!–设置前缀–>
<property name="prefix" value="/WEB-INF/helong/"/>
<!–设置后缀–>
<property name="suffix" value=".jsp"/>
</bean>-->
<!--配置Converter转换器 转换工厂-->
<bean id="dataConvert" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!--可以配置多个转换器-->
<property name="converters">
<list>
<bean class="com.helong.web.convert.DateConvert"></bean>
</list>
</property>
</bean>
<!--注解驱动-->
<mvc:annotation-driven conversion-service="dataConvert"/>
</beans>
3.在前端控制器中接收参数
package com.helong.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class MyController2 {
/*接收日期类型的参数的时候2010/01/01这种格式不会发生错误
* 但是由于日期有很多格式,springmvc没有办法把其他类型字符串转换成日期格式。所以需要自己自定义参数绑定。
* 解决方法:自定义类型转换器
* 1.创建转换器
* 2.在springmvc核心配置文件中自定义转换器
* */
@RequestMapping("/convert")
public ModelAndView myform1(Integer age, Date birthday,String name ){
ModelAndView modelAndView = new ModelAndView();
System.out.println(age);
//将接收到的日期格式的数据装换为自己想要的日期的字符串格式的方法
System.out.println(new SimpleDateFormat("yyyy-mm-dd").format(birthday));
System.out.println(name);
modelAndView.setViewName("/second.jsp");
return modelAndView;
}
}
注:SpringMVC的目录结构