spring mvc 参数类型转换
实现方式以字符串转Date为例说明:
全局配置
第一种:实现 Converter 接口
实现类:
public class StringToDateConveter implements Converter {private String formatPatten; public StringToDateConveter(String formatPatten){
this.formatPatten=formatPatten;
} @Override
public Date convert(String s) {
return DateUtil.string2Date(s,formatPatten);
}
}
mvc.xml配置
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.lannong.api.www.converter.StringToDateConveter">
<constructor-arg name="formatPatten" value="yyyy-MM-dd"/>
</bean>
</set>
</property>
</bean>
配置到handlerAdapter
<!--使用 ConfigurableWebBindingInitializer 注册conversionService-->
<bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"/>
</bean> <!-- 注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="webBindingInitializer"/>
</bean>
第二种:实现Formatter接口,与第一种实现方式类似
实现类
public class MyDateFormater implements Formatter<Date> { @Override
public Date parse(String s, Locale locale) throws ParseException {
return DateTimeUtil.string2Date(s,"yyyy-MM-dd");
} @Override
public String print(Date date, Locale locale) {
return null;
}
}
mvc.xml配置
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.lannong.api.www.converter.MyDateFormater"/>
</set>
</property>
</bean>
第三种:实现WebBindingInitializer接口
实现类
public class MyWebBindingInitializer implements WebBindingInitializer { @Override
public void initBinder(WebDataBinder binder, WebRequest request) { binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
}
});
}
}
mvc.xml配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.lannong.api.www.binder.MyWebBindingInitializer"/>
</property>
<!-- others config -->
</bean>
局部配置
在Controller中添加转换方法并添加@InitBinder
代码
@InitBinder
public void initBinder(WebDataBinder webDataBinder) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
simpleDateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
} 或 @InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
}
});
}
两种方式都可以,作用域和该方法作用域一样
使用@DateTimeFormat(pattern = "yyyy-MM-dd")
注解可以加在属性上,也可以加在方法上,需要导入joda-time.jar。另外日期参数的格式需要和patten定义的一致,否则会报400错误
spring mvc 参数类型转换的更多相关文章
- Spring mvc参数类型转换
1,需求 有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能. 2,配置文件 在springmvc ...
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- spring mvc 参数
Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...
- spring mvc参数绑定
spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...
- Spring MVC参数封装传递
在Spring MVC中,前端JSP页面可以传递 基本类型(int,String).实体类型.包装类型.数组类型.集合类型(List.map )等. 假如在传递的类型中有 Date类型的字段,需要在 ...
- spring mvc 参数绑定
基础类型 原始类型:id必须要传,否则报错. @RequestMapping("/test") @ResponseBody public ResponseData test(int ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
- Spring MVC 参数的绑定方法
在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...
- Spring MVC参数处理
使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...
随机推荐
- python开发总结
1.思维缜密的编程逻辑 2.满足明确的目的需求 3.运用现成的轮子加以改造 4.学会装饰自己的程序 5.化繁为简 6.多用配置文件作为入口 7.注意扩展兼容
- 项目Alpha冲刺——集合
作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 完成项目Alpha冲刺 团队信息 队名:火鸡堂 队员学号 队员姓名 博客地址 ...
- 各大公司Java面试题收录含答案(整理版)持续中....
本文分为17个模块,分别是:Java基础.容器.多线程.反射.对象拷贝.Java web.异常.网络.设计模式.算法.Spring/Spring MVC.Spring Boot/Spring Clou ...
- c++中关联容器map的使用
C++关联容器<map>简单总结(转) 补充: 使用count,返回的是被查找元素的个数.如果有,返回1:否则,返回0.注意,map中不存在相同元素,所以返回值只能是1或0. 使用find ...
- 我永远讨厌gch文件
一个学期没写博客了. 今天写OOP作业见鬼了, 调了半天. 我写了一个match.h和一个match.cpp, 然后match.cpp里面#include"match.h", 然后 ...
- C#中ref和out的原理
去年在CSDN上写的,现在把它搬过来. 一.引发问题 用了那么久的 ref 和 out ,你真的了解它们是如何使得实参与形参的值保持同步的吗? 二.研究前提 要研究这个问题,前提是要了解 C# 中方法 ...
- AtCoder Beginner Contest 132 解题报告
前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using names ...
- LightOJ - 1318 - Strange Game(组合数)
链接: https://vjudge.net/problem/LightOJ-1318 题意: In a country named "Ajob Desh", people pla ...
- ESP8266 tcp透传AP+STA
AP 建立WIFI,接受STA连接,串口数据和TCP互传 #include <ESP8266WiFi.h> const char *ssid = "esp8266_666&quo ...
- 权限管理(chown、chgrp、umask)
对于文件或目录的权限的修改,只能管理员和文件的所有者拥有此权限,但是对于文件或目录的的所有者的更改,只有管理员拥有此权限(虽然普通用户创建的文件或目录,用户也不能修改文件或目录的所有者). 1.cho ...