SpringMVC:学习笔记(6)——转换器和格式化
转换器和格式化
说明
SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上。
为了能够让SpringMVC进行正确地数据绑定,我们需要用到Converter和Formatter来协助SpringMVC完成。
举例:
我们知道HTTP表单中的所有请求参数都是String类型的,而且日期时间数据没有特定的形式1997-3-20,1997:03:20乃至20/03/1997对于我们人来说都是正确的形式都应该可以映射到Date对象上,但对于计算机来说都是陌生的,我们就要构建一种桥梁,来让计算机正确绑定数据。
Converter和Formatter都可以用于将一种对象类型转换成另一种对象类型。
区别:
Converter是通用元件,可以在应用程序的任意层使用。
Formatter是专用元件,专门为Web层设计。
Converter
说明
Converter转换器可以进行格式转换,这是一种通用的元件。下面我们要实现的效果是:

按照图示所示的日期格式让SpringMVC可以识别并转换成Date对象。
编写Converter步骤:
1.编写一个实现了org.springframework.core.convert.converter.Converter接口的Java类。

public class MyConverter implements Converter<String,Date> {
//<源类型,目标类型>
private String dataPattern;
public MyConverter(String dataPattern)
{
this.dataPattern=dataPattern;
System.out.println("DataPattern is"+dataPattern);
}
public Date convert(String s) {
try {
SimpleDateFormat simpleDateFormat= new SimpleDateFormat(dataPattern);
simpleDateFormat.setLenient(false);
//设置日期/时间的解析是否不严格,为false表示严格
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2.在SpringMVC的配置文件中编写一个ConversionService bean.
说明:
Bean的类名称必须是org.springframework.context.support.ConversionServiceFactoryBean.
这个Bean必须包含一个converters属性,它将列出在应用程序中用到的所有定制Converter.
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converter.MyConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</list>
</property>
</bean>
3.要给annotation-driven元素的conversion-service属性赋bean名称
<?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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<context:component-scan base-package="controller"/>
<context:component-scan base-package="Service"/>
<!--
<mvc:annotation-driven>元素注册用于支持基于注解的控制器的请求处理方法的Bean对象。
详解:https://my.oschina.net/HeliosFly/blog/205343
-->
<mvc:annotation-driven conversion-service="conversionService"/> <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converter.MyConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</list>
</property>
</bean>
</beans>
其余代码
1.编写控制器
package controller; import domain.Employee;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; /**
* Created by zy on 17-3-3.
*/
@Controller
public class EmployeeController {
private static final Log logger = LogFactory.getLog(EmployeeController.class); @RequestMapping("employ_input")
public String inputEmployee(Model model)
{
model.addAttribute("employee",new Employee());
return "EmployeeForm";
}
@RequestMapping("employ_save")
public String saveEmployee(@ModelAttribute Employee employee,BindingResult bindingResult,Model model)
{
if(bindingResult.hasErrors())
{
FieldError fieldError = bindingResult.getFieldError();
logger.info("Code:" +fieldError.getCode()+",field"+fieldError.getField());
return "EmployeeForm";
}
model.addAttribute("employee",employee);
return "EmployeeForm";
}
}
2.编写视图
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EmployeeInput</title>
</head>
<body>
<form:form commandName="employee" action="employ_save" method="post">
<legend>Add Employee</legend>
<p>
<label for="firstName">姓</label>
<form:input path="firstName" tabindex="1"/>
</p>
<p>
<label for="lastName">名</label>
<form:input path="lastName" tabindex="2"/>
</p>
<p>
<form:errors path="birthDate" cssClass="error"/>
</p>
<p>
<label for="birthDate">出生日期</label>
<form:input path="birthDate" tabindex="3"/>
</p>
<p>
<input type="submit" value="Add Employee">
</p>
</form:form>
</body>
</html>
Formatter
说明
Formatter就像Converter一样,也是将一种类型转换成另外一种类型。但是,Formatter的源类型必须是String,而Converter则适用于任意的源类型。
Formatter更适合在Web层使用,处理表单输入,始终应该选择Formatter,而不是Converter。
编写Formatter
1.编写一个实现了org.springframework.format.Formatter接口的Java类。

2.在SpringMVC的配置文件中编写一个ConversionService bean.
<?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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<context:component-scan base-package="controller"/>
<context:component-scan base-package="Service"/>
<!--
<mvc:annotation-driven>元素注册用于支持基于注解的控制器的请求处理方法的Bean对象。
详解:https://my.oschina.net/HeliosFly/blog/205343
-->
<mvc:annotation-driven conversion-service="conversionService"/> <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--需要配置此项来扫描Formatter-->
<context:component-scan base-package="formatter"/>
<bean id="formatterConversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="formatter.DateFormatter">
<constructor-arg type="java.lang.String"
value="MM-dd-yyyy"/>
</bean>
</set>
</property>
</bean>
</beans>
选择Converter还是Formatter
在SpringMVC应用程序中当然是Formatter更合适~\(≧▽≦)/~啦啦啦
SpringMVC:学习笔记(6)——转换器和格式化的更多相关文章
- springmvc学习笔记(简介及使用)
springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
随机推荐
- Redis 过期时间
http://www.redis.cn/commands/expire.html 附录: Redis 过期时间 Keys的过期时间 通常Redis keys创建时没有设置相关过期时间.他们会一直存在, ...
- eclipse JavaEE版"javax.servlet.http.HttpServlet" was not found on the Java Build Path问题的解决办法
使用eclipse JavaEE 版,新建 Dynamic Web Project 项目.在项目里添加 JSP 文件,会在文件头部出现错误提示.提示语句为:The superclass "j ...
- 如何使用 TP中的公共函数 (定义在common/common.php中的函数)
如何使用 TP中的公共函数 (定义在common/common.php中的函数) (2011-09-30 15:32:09) 转载▼ 标签: 杂谈 1.在common/common.php 中有个 ...
- linux oracle sqlplus中文乱码解决
在oracle用户的~/.bash_profile中添加 NLS_LANG="SIMPLIFIED CHINESE"_CHINA.ZHS16GBKexport NLS_LANG 然 ...
- go语言发送邮件
package main import ( "fmt" "net/smtp" "strings" ) //发送邮件的逻辑函数 func Se ...
- lumen 单元测试
laravel学院:http://laravelacademy.org/post/238.html 简书:https://www.jianshu.com/p/d8b3ac2c4623 问题解决:htt ...
- Linux基础分析
1.系统目录 [root@15b883 ~]# tree -L 1 / ├── bin 常用二进制命令所在的目录 ├── boot 内核及系统引导程序所在的文件目录 ├── dev 设备目录 ├── ...
- Android无线测试之—UiAutomator UiDevice API介绍一
UiDevice 类介绍 1.UiDevice 代表设备状态 2.UiDevice 为单例模式 获取UiDevice实例的方式: 1) UiDevice.getInstance() 2) getUiD ...
- Django项目部署(django+guncorn+virtualenv+nginx)
一.说明 为了django项目部署到生产环境上,能够稳定的运行,且能够同时指出http和https的访问,对django的部署进行了一些研究,决定采用django + gunicorn + virtu ...
- c++获取读写文本权限
#include<cstdio> #include<iostream> #include<fstream> using namespace std; int tot ...