1 Formatter格式化器
Formatter就像Converter –样,也是将一种类型转换成另一种类型。但是,Formatter的源类型必须是一个String,而Converter则适用于任意的源类型。Formatter更适合W曲层,而Converter则可以用在任意层中。
为了转换Spring MVC应用程序表单中的用户输入,始终应该选择Formatter,而不是Converter。
2 Spring MVC中格式化器的使用案例
2.1 程序的目录结构
2.2 domain
package app06b.domain;
import java.io.Serializable;
import java.util.Date;
public class Employee implements Serializable {
private static final long serialVersionUID = -908L;
private long id;
private String firstName;
private String lastName;
private Date birthDate;
private int salaryLevel;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public int getSalaryLevel() {
return salaryLevel;
}
public void setSalaryLevel(int salaryLevel) {
this.salaryLevel = salaryLevel;
}
}
2.3 Converter转换器类
为了创建Formatter,要编写一个实现org.springframework.format.Formatter接口的Java类。
下面是这个接口的声明:
Public interface Formatter<T>
这里的T表示输入字符串要转换的目标类型。该接口有parse和print两个方法,所有实现都必须覆盖。
T parse (String text,java.util.Locale locale>
String print (T object, java.util.Locale locale)
Parse方法利用指定的Locale将一个String解析成目标类型。
Print方法与之相反,它是返回目标对象的字符串表示法。
DateFormatter
package app06b.formatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
public class DateFormatter implements Formatter<Date> {
private String datePattern;
private SimpleDateFormat dateFormat;
public DateFormatter(String datePattern) {
System.out.println("DateFormatter()5b========");
this.datePattern = datePattern;
dateFormat = new SimpleDateFormat(datePattern);
dateFormat.setLenient(false);
}
@Override
public String print(Date date, Locale locale) {
return dateFormat.format(date);
}
@Override
public Date parse(String s, Locale locale) throws ParseException {
try {
return dateFormat.parse(s);
} catch (ParseException e) {
// the error message will be displayed when using <form:errors>
throw new IllegalArgumentException(
"invalid date format. Please use this pattern\""+datePattern + "\"");
}
}
}
2.4 Controller类
package app06b.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import app06b.domain.Employee;
@org.springframework.stereotype.Controller
public class EmployeeController {
private static final Log logger = LogFactory.getLog(EmployeeController.class);
@Autowired
ConversionService conversionService;
@RequestMapping(value="employee_input")
public String inputEmployee(Model model) {
model.addAttribute(new Employee());
logger.info("inputEmployee called 2. map:" + model.asMap());
return "EmployeeForm";
}
@RequestMapping(value="employee_save")
public String saveEmployee(@ModelAttribute Employee employee, BindingResult bindingResult,
Model model) {
logger.info("saveEmployee called 2");
System.out.println("type of conversion service:" + conversionService.getClass());
DefaultFormattingConversionService cs = (DefaultFormattingConversionService) conversionService;
logger.info("as map:" + model.asMap());
// we don't need ProductForm anymore,l
// Spring MVC can bind HTML forms to Java objects
if (bindingResult.hasErrors()) {
System.out.println("has errors");
FieldError fieldError = bindingResult.getFieldError();
System.out.println("Code:" + fieldError.getCode()
+ ", field:" + fieldError.getField());
return "EmployeeForm";
}
// save product here
model.addAttribute("employee", employee);
return "EmployeeDetails";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.initDirectFieldAccess();
binder.setDisallowedFields("id");
// binder.setRequiredFields("username", "password", "emailAddress");
logger.info("initBinderin EmployeeController");
}
}
2.5 Spring MVC配置文件
为了在Spring MVC应用程序中使用Formatter,需要利用conversionService bean对它进行注册。
Bean的类名祢必须为:org. springframework. Format. Support.FormattingConversionService-FactoryBean。
这个bean可以用一个Formatters属性注册Formatter,用一个converters属性注册converter。
springmvc-config.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="app06b.controller" />
<context:component-scan base-package="app06b.formatter" />
<mvc:annotation-driven conversion-service="conversionService" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/*.html" location="/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="app06b.formatter.DateFormatter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy" />
</bean>
</set>
</property>
</bean>
</beans>
2.6 View视图层
2.6.1 EmployeeForm.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML>
<html>
<head>
<title>Add Product Form</title>
<style type="text/css">@import url("<c:url value="/css/main.css"/>");</style>
</head>
<body>
<div id="global">
<form:form commandName="employee" action="employee_save" method="post">
<fieldset>
<legend>Add an employee</legend>
<p>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" name="firstName" tabindex="1">
</p>
<p>
<label for="lastName">First Name: </label>
<input type="text" id="lastName" name="lastName" tabindex="2">
</p>
<p>
<form:errors path="birthDate" cssClass="error"/>
</p>
<p>
<label for="birthDate">Date Of Birth: </label>
<form:input path="birthDate" id="birthDate" />
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5" value="Add Employee">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>
2.6.2 EmployeeDetails.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML>
<html>
<head>
<title>Save Employee</title>
<style type="text/css">@import url("<c:url value="/css/main.css"/>");</style>
</head>
<body>
<div id="global">
<h4>The employee details have been saved.</h4>
<p>
<h5>Details:</h5>
First Name: ${employee.firstName}<br/>
Last Name: ${employee.lastName}<br/>
Date of Birth: ${employee.birthDate}
</p>
</div>
</body>
</html>
2.6.3 main.css
#global {
text-align: left;
border: 1px solid #dedede;
background: #efefef;
width: 560px;
padding: 20px;
margin: 100px auto;
}
form {
font:100% verdana;
min-width: 500px;
max-width: 600px;
width: 560px;
}
form fieldset {
border-color: #bdbebf;
border-width: 3px;
margin: 0;
}
legend {
font-size: 1.3em;
}
form label {
width: 250px;
display: block;
float: left;
text-align: right;
padding: 2px;
}
#buttons {
text-align: right;
}
#errors, li {
color: red;
}
.error {
color: red;
font-size: 9pt;
}
2.7 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>YJYSpring_MVC_Formatter_PaulDeckCH06</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3 运行和测试
运行
http://localhost:8080/YJYSpring_MVC_Formatter_PaulDeckCH06/employee_input
输入错误的信息
点击add employee,显示错误提示
修改为正确的信息,重新提交