一、格式化
1.配置xml
<!-- 配置 数据格式化 注解 所依赖的bean -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
学生类:属性上加格式化注解
package entity;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import javax.validation.constraints.Past;
import java.util.Date;
public class Student {
@NumberFormat(pattern = "###,#")
private int id;
private String name;
private int age;
private Address address;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
}
public Student(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", address=" + address +
'}';
}
}
controller:
@RequestMapping("testDateTimeFormat")//如果Student格式化出错,会将错误信息 传入result中c(!注意必须是第二个参数)
public String testDateTimeFormat(Student student, BindingResult result, Map<String,Object> map){
System.out.println(student.getId()+","+student.getName()+","+student.getBirthday());
if(result.getErrorCount()>0){
for(FieldError error:result.getFieldErrors()){
System.out.println(error.getDefaultMessage());
map.put("errors",result.getFieldErrors());//将错误信息传入request域中的error中
}
}
return "success";
}
success拿错误信息:
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!--下面就是jstl提供的国际化操作-->
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach var="error" items="${requestScope.errors}" >
${error.getDefaultMessage()}<br/>
</c:forEach>
</body>
</html>
二、数据校验
使用Hibernate Validator步骤:
a.jar(注意各个jar之间可能存在版本不兼容)
hibernate-validator-5.0.0.CR2.jar classmate-0.8.0.jar jboss-logging-3.1.1.GA.jar
validation-api-1.1.0.CR1.jar hibernate-validator-annotation-processor-5.0.0.CR2.jar
b配置
<mvc:annotation-driven ></mvc:annotation-driven>
此时mvc:annotation-driven的作用:要实现Hibernate Validator/JSR303 校验(或者其他各种校验),必须实现SpringMVC提供的一个接口:ValidatorFactory
LocalValidatorFactoryBean是ValidatorFactory的一个实现类。
<mvc:annotation-driven ></mvc:annotation-driven>会在springmvc容器中 自动加载一个LocalValidatorFactoryBean类,因此可以直接实现数据校验
c.直接使用注解
public class Student {
@Past//当前时间以前
private Date birthday ;
...
}
在校验的Controller中 ,给校验的对象前增加 @Valid
//想要使用数据校验 下面就得加上@Valid注解 并在bean中加上@Past这种注解 还得导入相关jar
@RequestMapping("testDateTimeFormat")//如果Student格式化出错,会将错误信息 传入result中c(!注意必须是第二个参数)
public String testDateTimeFormat(@Valid Student student, BindingResult result, Map<String,Object> map){
System.out.println(student.getId()+","+student.getName()+","+student.getBirthday());
if(result.getErrorCount()>0){
for(FieldError error:result.getFieldErrors()){
System.out.println(error.getDefaultMessage());
map.put("errors",result.getFieldErrors());//将错误信息传入request域中的error中
}
}
return "success";
}