Spring MVC框架:第十四章:數(shù)據(jù)校驗
第十二章 數(shù)據(jù)校驗
在Web應用三層架構體系中,表述層負責接收瀏覽器提交的數(shù)據(jù),業(yè)務邏輯層負責數(shù)據(jù)的處理。為了能夠讓業(yè)務邏輯層基于正確的數(shù)據(jù)進行處理,我們需要在表述層對數(shù)據(jù)進行檢查,將錯誤的數(shù)據(jù)隔絕在業(yè)務邏輯層之外。
1.校驗概述
JSR 303是Java為Bean數(shù)據(jù)合法性校驗提供的標準框架,它已經(jīng)包含在JavaEE 6.0標準中。JSR 303通過在Bean 屬性上標注類似于@NotNull、@Max等標準的注解指定校驗規(guī)則,并通過標準的驗證接口對Bean進行驗證。
JSR 303只是一套標準,需要提供其實現(xiàn)才可以使用。Hibernate Validator是JSR 303的一個參考實現(xiàn),除支持所有標準的校驗注解外,它還支持以下的擴展注解:
Spring4.0擁有自己獨立的數(shù)據(jù)校驗框架,同時支持JSR
303標準的校驗框架。Spring在進行數(shù)據(jù)綁定時,可同時調用校驗框架完成數(shù)據(jù)校驗工作。在Spring
MVC中,可直接通過注解驅動(mvc:annotation-driven)的方式進行數(shù)據(jù)校驗。Spring的LocalValidatorFactroyBean既實現(xiàn)了Spring的Validator接口,也實現(xiàn)了JSR
303的
Validator接口。只要在Spring容器中定義了一個LocalValidatorFactoryBean,即可將其注入到需要數(shù)據(jù)校驗的
Bean中。Spring本身并沒有提供JSR 303的實現(xiàn),所以必須將JSR 303的實現(xiàn)者的jar包放到類路徑下。
配置mvc:annotation-driven/后,SpringMVC會默認裝配好一個LocalValidatorFactoryBean,通過在處理方法的入?yún)⑸蠘俗Validated注解即可讓Spring MVC在完成數(shù)據(jù)綁定后執(zhí)行數(shù)據(jù)校驗的工作。
2.操作步驟
①導入驗證環(huán)境
[1]在當前工程類路徑下加入以下jar包
1classmate-0.8.0.jar
2hibernate-validator-5.0.0.CR2.jar
3hibernate-validator-annotation-processor-5.0.0.CR2.jar
4jboss-logging-3.1.1.GA.jar
5validation-api-1.1.0.CR1.jar
[2]在Tomcat的lib目錄下加入以下jar包[Tomcat7及以上版本不需要加]
1javax.el-2.2.4.jar
2javax.el-api-2.2.4.jar
3el-api-2.2.jar
②在需要驗證的字段上添加驗證注解
@NotEmpty
private String empName;
③目標方法的bean前添加@Validated注解
※注意:在@Validated注解修飾的bean之后, 緊跟Errors(或BindingResult)類型的參數(shù)。在@Validated注解修飾的bean和Errors參數(shù)之間不能再有其他的參數(shù)!
④調用hasErrors()方法在目標方法中檢驗是否沒有通過驗證
//檢測是否存在“數(shù)據(jù)綁定”錯誤
boolean hasErrors = bindingResult.hasErrors();
if(hasErrors) {
return "error";
}
- 1
- 2
- 3
- 4
- 5
⑤顯示錯誤消息
[1]使form:errors標簽 [2]form:errors 標簽必須在 SpringMVC 的 form:form 標簽中使用
<form:form modelAttribute=“employee”>
<form:input path=“age”/><form:errors path=“age”/>
</form:form>
實戰(zhàn)案例:
正確的數(shù)據(jù)
頁面:
1
- 1
Employee
public class Employee {
private Integer age;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
- 1
- 2
- 3
- 4
spring-mvc.xml
<context:component-scan base-package=“com.spring.mvc.handlers”/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
- 1
- 2
- 3
- 4
handlers
@RequestMapping("/convert")
public String convertFail(Employee employee, BindingResult bindingResult, Model model) {
System.out.println(employee);
//檢測是否存在“數(shù)據(jù)綁定”錯誤
boolean hasErrors = bindingResult.hasErrors();
if(hasErrors) {
return “error”;
}
model.addAttribute(“employee”, employee);
return “target”;
}
error.jsp
Error
target.jsp
Target
頁面點擊‘’測試自動的類型轉換‘’
控制臺輸出Employee [age=15, birthday=null]
給一個類型錯誤的數(shù)據(jù)
頁面:
1
- 1
Employee
public class Employee {
private Integer age;
- 1
點擊頁面‘’測試自動的類型轉換‘’后
控制臺輸出Employee [age=null, birthday=null]
頁面顯示
我們再深入了解數(shù)據(jù)校驗,自定義類型轉換
直接上代碼更直觀
還是引用上面的代碼
spring-mvc.xml
<context:component-scan base-package=“com.spring.mvc.handlers”/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
- 1
- 2
- 3
- 4
Address
public class Address {
private String city;
private String street;
private String no;
- 1
- 2
- 3
Student
public class Student {
private Integer stuId;
private String stuName;
private Address address;
- 1
- 2
- 3
- 4
AddressConvert
import org.springframework.core.convert.converter.Converter;
import com.atguigu.spring.mvc.entity.Address;
public class AddressConvert implements Converter<String, Address> {
@Override
public Address convert(String source) {
//使用逗號分隔符拆分字符串
String[] split = source.split(",");
//從拆分得到的數(shù)組中獲取Address對象的各個屬性值
String city = split[0];
String street = split[1];
String no = split[2];
//創(chuàng)建對象并返回
return new Address(city, street, no);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
}
頁面:
id:姓名:
地址:
handlers
@Controller
public class ConvertHandler {
@RequestMapping("/convert/save/stu")
public String saveStu(Student student) {
System.out.println(student);
return "target";
}
- 1
- 2
- 3
- 4
- 5
頁面
控制臺輸出
Student [stuId=45, stuName=gf, address=Address [city=fgf, street=hfdfd, no=gfddf]]
如果Student和Address實體類沒有給toString方法,那address=只有地址
執(zhí)行saveStu方法完