Springmvc中如何实现自定义参数转换器的代码解析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计479个文字,预计阅读时间需要2分钟。
Spring MVC的参数绑定有以下几种方法:
1. 默认的参数绑定:将请求参数绑定到Request、Response、Session和Model(实现ModelMap接口)。
2.简单类型参数绑定:通过方法的形参直接接收请求参数,形参类型需与请求参数类型匹配(如Integer id, String name, Double value, Boolean flag)。
3.POJO类型参数绑定:将请求参数绑定到POJO对象上,需要POJO类的属性名与请求参数名一致或通过注解指定。
4.包装类型参数绑定:将请求参数绑定到包装类对象上,如Integer、String、Double、Boolean等。
springmvc的参数绑定有以下几种方法:
1)默认的参数绑定 Request Response Session Model(实现ModelMap)
2)简单类型参数绑定 方法的形参上(Integer id,String,Double,Boolean)
3)pojo类型
4)包装类型 QueryVo
5)参数绑定之自定义参数转换
高级参数绑定
1)绑定数组
直接在方法的参数上绑定 xxx[] xxx
将数组注入对象,用该对象来接受数组
2)绑定list
使用包装类,包装类中有list集合
自定义参数转换的步骤
1、在springmvc.xml中配置Conveter转换器
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- 配置 多个转换器--> <property name="converters"> <list> <bean class="com.itheima.springmvc.conversion.DateConveter"/> </list> </property> </bean>
2、定义转换类,实现Conveter接口
DateConveter 类:
public class DateConveter implements Converter<String, Date>{ public Date convert(String source) { // TODO Auto-generated method stub try { if(null != source){//2016:11-05 11_43-50 DateFormat df = new SimpleDateFormat("yyyy:MM-dd HH_mm-ss"); return df.parse(source); } } catch (Exception e) { // TODO: handle exception } return null; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计479个文字,预计阅读时间需要2分钟。
Spring MVC的参数绑定有以下几种方法:
1. 默认的参数绑定:将请求参数绑定到Request、Response、Session和Model(实现ModelMap接口)。
2.简单类型参数绑定:通过方法的形参直接接收请求参数,形参类型需与请求参数类型匹配(如Integer id, String name, Double value, Boolean flag)。
3.POJO类型参数绑定:将请求参数绑定到POJO对象上,需要POJO类的属性名与请求参数名一致或通过注解指定。
4.包装类型参数绑定:将请求参数绑定到包装类对象上,如Integer、String、Double、Boolean等。
springmvc的参数绑定有以下几种方法:
1)默认的参数绑定 Request Response Session Model(实现ModelMap)
2)简单类型参数绑定 方法的形参上(Integer id,String,Double,Boolean)
3)pojo类型
4)包装类型 QueryVo
5)参数绑定之自定义参数转换
高级参数绑定
1)绑定数组
直接在方法的参数上绑定 xxx[] xxx
将数组注入对象,用该对象来接受数组
2)绑定list
使用包装类,包装类中有list集合
自定义参数转换的步骤
1、在springmvc.xml中配置Conveter转换器
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!-- 配置 多个转换器--> <property name="converters"> <list> <bean class="com.itheima.springmvc.conversion.DateConveter"/> </list> </property> </bean>
2、定义转换类,实现Conveter接口
DateConveter 类:
public class DateConveter implements Converter<String, Date>{ public Date convert(String source) { // TODO Auto-generated method stub try { if(null != source){//2016:11-05 11_43-50 DateFormat df = new SimpleDateFormat("yyyy:MM-dd HH_mm-ss"); return df.parse(source); } } catch (Exception e) { // TODO: handle exception } return null; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

