这篇文章主要介绍Springmvc自定义参数转换的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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;
}
}
以上是Springmvc自定义参数转换的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。