SpringMVC 表單驗證
本章節內容很豐富,主要有基本的表單操作,數據的格式化,數據的校驗,以及提示信息的國際化等實用技能。
首先看效果圖
項目結構圖
接下來用代碼重點學習SpringMVC的表單操作,數據格式化,數據校驗以及錯誤提示信息國際化。請讀者將重點放在UserController.java,User.java,input.jsp三個文件中。
maven 項目必不可少的pom.xml文件。里面有該功能需要的所有jar包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springmvc</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 若不配置,打包時會提示錯誤信息
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project springmvc: Compilation failure:
提示 未結束的字符串文字 ,若字符串后面加上空格后可以打包成功,但會亂碼。
原因是:maven使用的是默認的compile插件來進行編譯的。complier是maven的核心插件之一,然而complier插件默認只支持編譯Java 1.4
-->
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.1.3.RELEASE</spring.version>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 缺少jsp-api 則提示 javax.servlet.jsp.JspException cannot be resolved to a type -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- JSR 303 start -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- JSR 303 end -->
</dependencies>
</project>
SpringMVC的核心配置文件
<?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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自定掃描的包 -->
<context:component-scan base-package="com.itdragon.springmvc" />
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置注解驅動 -->
<mvc:annotation-driven />
<!-- 配置視圖 BeanNameViewResolver 解析器
使用視圖的名字來解析視圖
通過 order 屬性來定義視圖解析器的優先級, order 值越小優先級越高
-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
<!-- 配置直接跳轉的頁面,無需經過Controller層
http://localhost:8080/springmvc/index
然后會跳轉到 WEB-INF/views/index.jsp 頁面
-->
<mvc:view-controller path="/index" view-name="index"/>
<mvc:default-servlet-handler/>
<!-- 配置國際化資源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
</beans>
以上是准備工作。下面開始核心代碼介紹。
數據的校驗思路:
第一步,在實體類中指定屬性添加校驗注解(如@NotEmpty),
第二步,在控制層目標方法實體類參數添加注解@Valid,
第三步,在返回頁面加上<form:errors path="xxx"></form:errors>顯示提示錯誤信息
數據格式化思路:只需要在實體類中加上注解即可。
信息國際化思路:
第一步,在SpringMVC配置文件中配置國際化資源文件
第二步,創建文件i18n_zh_CN.properties文件
第三步,在i18n_zh_CN.properties文件配置國際化信息(要嚴格按照SpringMVC的語法)
UserController.java,兩個重點知識。一個是SpringMVC的rest風格的增刪改查。另一個是@Valid注解用法。具體看代碼。
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.itdragon.springmvc.crud.dao.PositionDao;
import com.itdragon.springmvc.crud.dao.UserDao;
import com.itdragon.springmvc.crud.orm.User;
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@Autowired
private PositionDao positionDao;
private static final String INPUT = "input"; // 跳轉到編輯頁面
private static final String LIST = "list"; // 跳轉到用戶列表頁面
@ModelAttribute
public void getUser(@RequestParam(value="id",required=false) Integer id,
Map<String, Object> map){
if(id != null){
map.put("user", userDao.getUserById(id));
}
}
// 更新用戶,用put請求方式區別get請求方式,屬於SpringMVC rest 風格的crud
@RequestMapping(value="/user", method=RequestMethod.PUT)
public String updateUser(User user){
userDao.save(user);
return "redirect:/users";
}
// 點擊編輯跳轉編輯頁面
@RequestMapping(value="/user/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id, Map<String, Object> map){
map.put("user", userDao.getUserById(id));
map.put("positions", positionDao.queryAllPositions());
return INPUT;
}
// 通過id刪除用戶
@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public String delete(@PathVariable("id") Integer id){
userDao.deleteUserById(id);
return "redirect:/users";
}
/**
* 新增用戶,若保存成功則跳轉到用戶列表頁面,若失敗則跳轉到編輯頁面
* @param user 用 @Valid 注解修飾后,可實現數據校驗的邏輯
* @param result 數據校驗結果
* @param map 數據模型
* @return
*/
@RequestMapping(value="/user", method=RequestMethod.POST)
public String save(@Valid User user, Errors result, Map<String, Object> map){
if(result.getErrorCount() > 0){
for(FieldError error : result.getFieldErrors()){
System.out.println(error.getField() + " : " + error.getDefaultMessage());
}
map.put("positions", positionDao.queryAllPositions());
return INPUT;
}
userDao.save(user);
return "redirect:/users";
}
@RequestMapping(value="/user", method=RequestMethod.GET)
public String input(Map<String, Object> map){
map.put("positions", positionDao.queryAllPositions());
map.put("user", new User());
return INPUT;
}
// 跳轉用戶列表頁面
@RequestMapping("/users")
public String list(Map<String, Object> map){
map.put("users", userDao.queryAllUsers());
return LIST;
}
}
User.java,兩個重點知識。一個是數據的格式化(包括日期格式化和數值格式化)。另一個是使用 JSR 303 驗證標准數據校驗。
數據格式化,由於前端傳給后台的是字符串,對於比較特殊的屬性,比如Date,Float類型就需要進行數據格式化
@NumberFormat 數值格式化
可以格式化/解析的數字類型:Short、Integer、Long、Float、Double、BigDecimal、BigInteger。
屬性參數有:pattern="###,###.##"(重點)。
style= org.springframework.format.annotation.NumberFormat.Style.NUMBER(CURRENCY / PERCENT)。其中Style.NUMBER(通用樣式,默認值);Style.CURRENCY(貨幣樣式);Style.PERCENT(百分數樣式)
@DateTimeFormat 日期格式化
可以格式化/解析的數字類型:java.util.Date 、java.util.Calendar 、java.long.Long。
屬性參數有:pattern="yyyy-MM-dd hh:mm:ss"(重點)。
iso=指定解析/格式化字段數據的ISO模式,包括四種:ISO.NONE(不使用ISO模式,默認值),ISO.DATE(yyyy-MM-dd),ISO.TIME(hh:mm:ss.SSSZ),ISO.DATE_TIME(yyyy-MM-dd hh:mm:ss.SSSZ);style=指定用於格式化的樣式模式,默認“SS”,優先級: pattern 大於 iso 大於 style,后兩個很少用。
數據校驗
空檢查
@Null 驗證對象是否為null
@NotNull 驗證對象是否不為null, 無法查檢長度為0的字符串
@NotBlank 檢查約束字符串是不是Null還有被Trim的長度是否大於0,只對字符串,
且會去掉前后空格
@NotEmpty 檢查約束元素是否為NULL或者是EMPTY
Booelan檢查
@AssertTrue 驗證 Boolean 對象是否為 true
@AssertFalse 驗證 Boolean 對象是否為 false
長度檢查
@Size(min=, max=) 驗證對象(Array,Collection,Map,String)值是否在給定的范圍之內
@Length(min=, max=) 驗證對象(CharSequence子類型)長度是否在給定的范圍之內
日期檢查
@Past 驗證 Date 和 Calendar 對象是否在當前時間之前
@Future 驗證 Date 和 Calendar 對象是否在當前時間之后
@Pattern 驗證 String 對象是否符合正則表達式的規則
數值檢查
@Min 驗證 Number 和 String 對象是否大等於指定的值
@Max 驗證 Number 和 String 對象是否小等於指定的值
@DecimalMax 被標注的值必須不大於約束中指定的最大值. 這個約束的參數
是一個通過BigDecimal定義的最大值的字符串表示.小數存在精度
@DecimalMin 被標注的值必須不小於約束中指定的最小值. 這個約束的參數
是一個通過BigDecimal定義的最小值的字符串表示.小數存在精度
@Digits 驗證 Number 和 String 的構成是否合法
@Digits(integer=,fraction=) 驗證字符串是否是符合指定格式的數字,interger指定整數精度,fraction指定小數精度
@Range(min=, max=) 檢查數字是否介於min和max之間
@CreditCardNumber 信用卡驗證
@Email 驗證是否是郵件地址,如果為null,不進行驗證,算通過驗證
@ScriptAssert(lang= ,script=, alias=) 通過腳本驗證
其中有幾點需要注意:
空判斷注解
String name @NotNull @NotEmpty @NotBlank
null false false false
"" true false false
" " true true false
"ITDragon!" true true true
數值檢查:建議使用在Stirng,Integer類型,不建議使用在int類型上,因為表單值為“”時無法轉換為int,但可以轉換為Stirng為"",Integer為null
import java.util.Date;
import javax.validation.constraints.DecimalMin;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
public class User {
private Integer id;
@NotEmpty
private String account;
@Email
@NotEmpty
private String email;
private Integer sex;
private Position position;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date createdDate;
@NumberFormat(pattern="###,###.#")
@DecimalMin("2000")
private Double salary;
public User() {
}
public User(Integer id, String account, String email, Integer sex,
Position position, Date createdDate, Double salary) {
this.id = id;
this.account = account;
this.email = email;
this.sex = sex;
this.position = position;
this.createdDate = createdDate;
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User [id=" + id + ", account=" + account + ", email=" + email
+ ", sex=" + sex + ", position=" + position + ", createdDate="
+ createdDate + ", salary=" + salary + "]";
}
}
input.jsp,SpringMVC 表單標簽知識點詳解 http://www.cnblogs.com/liukemng/p/3754211.html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC 表單操作</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!--
1. 使用 form 標簽可以更快速的開發出表單頁面, 而且可以更方便的進行表單值的回顯。
step1 導入標簽 taglib prefix="form" uri="http://www.springframework.org/tags/form"
step2 和普通的form用法差不多。path 相當於 普通的form的name,form:hidden 隱藏域,form:errors 提示錯誤信息。
2. 使用form 標簽需要注意:
通過 modelAttribute 屬性指定綁定的模型屬性, 該數據模型必須是實例化過的。
若沒有 modelAttribute 指定該屬性,則默認從 request 域對象中讀取 command 的表單 bean (如果該屬性值也不存在,則會發生錯誤)。
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
-->
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="panel panel-info" style="margin-top:10px;">
<div class="panel-heading">
<h3 class="panel-title">修改或創建用戶信息</h3>
</div>
<div class="panel-body">
<form:form action="${pageContext.request.contextPath }/user" method="POST"
modelAttribute="user" class="form-horizontal" role="form">
<c:if test="${user.id == null }">
<!-- path 屬性對應 html 表單標簽的 name 屬性值 -->
<div class="form-group">
<label class="col-sm-2 control-label">Account</label>
<div class="col-sm-10">
<form:input class="form-control" path="account"/>
<form:errors style="color:red" path="account"></form:errors>
</div>
</div>
</c:if>
<c:if test="${user.id != null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT"/>
<%-- 對於 _method 不能使用 form:hidden 標簽, 因為 modelAttribute 對應的 bean 中沒有 _method 這個屬性 --%>
<%--
<form:hidden path="_method" value="PUT"/>
--%>
</c:if>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<form:input class="form-control" path="email"/>
<form:errors style="color:red" path="email"></form:errors>
</div>
</div>
<!-- 這是SpringMVC 不足之處 -->
<%
Map<String, String> genders = new HashMap();
genders.put("1", "Male");
genders.put("0", "Female");
request.setAttribute("genders", genders);
%>
<div class="form-group">
<label class="col-sm-2 control-label">Sex</label>
<div class="col-sm-10">
<form:radiobuttons path="sex" items="${genders }" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Position</label>
<div class="col-sm-10">
<form:select class="form-control" path="position.id" items="${positions}" itemLabel="level" itemValue="id">
</form:select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Date</label>
<div class="col-sm-10">
<form:input class="form-control" path="createdDate"/>
<form:errors style="color:red" path="createdDate"></form:errors>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Salary</label>
<div class="col-sm-10">
<form:input class="form-control" path="salary"/>
<form:errors style="color:red" path="salary"></form:errors>
</div>
</div>
<input class="btn btn-success" type="submit" value="Submit"/>
</form:form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
i18n國際化文件
#語法:實體類上屬性的注解.驗證目標方法的modleAttribute 屬性值(如果沒有默認為實體類首字母小寫).注解修飾的屬性
#以第一個為例:User實體類中 屬性account用了NotEmpty注解修飾,表示不能為空。所以前綴是NotEmpty
#驗證的目標方法 public String save(@Valid User user, ...) User被注解@Valid 修飾,但沒有被modleAttribute修飾。所以中間是user
#后綴就是被注解修飾的屬性名 account
NotEmpty.user.account=用戶名不能為空
Email.user.email=Email地址不合法
#typeMismatch 數據類型不匹配時提示
typeMismatch.user.createdDate=不是一個日期
#required 必要參數不存在時提示
#methodInvocation 調用目標方法出錯的時提示
其他文件,Position 實體類
public class Position {
private Integer id;
private String level;
public Position() {
}
public Position(Integer id, String level) {
this.id = id;
this.level = level;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
@Override
public String toString() {
return "Position [id=" + id + ", level=" + level + "]";
}
}
模擬用戶操作的dao,UserDao.java
[java] view plain copy
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.itdragon.springmvc.crud.orm.Position;
import com.itdragon.springmvc.crud.orm.User;
@Repository
public class UserDao {
private static Map<Integer, User> users = null;
@Autowired
private PositionDao positionDao;
// 模擬數據庫查詢數據
static{
users = new HashMap<Integer, User>();
users.put(1, new User(1, "ITDragon", "11@xl.com", 1, new Position(1, "架構師"), new Date(), 18888.88));
users.put(2, new User(2, "Blog", "22@xl.com", 1, new Position(2, "高級工程師"), new Date(), 15555.55));
users.put(3, new User(3, "Welcome", "33@xl.com", 0, new Position(3, "中級工程師"), new Date(), 8888.88));
users.put(4, new User(4, "To", "44@xl.com", 0, new Position(4, "初級工程師"), new Date(), 5555.55));
users.put(5, new User(5, "You", "55@xl.com", 1, new Position(5, "java實習生"), new Date(), 2222.22));
}
// 下一次存儲的下標id
private static Integer initId = 6;
public void save(User user){
if(user.getId() == null){
user.setId(initId++);
}
user.setPosition(positionDao.getPositionById(user.getPosition().getId()));
users.put(user.getId(), user);
}
public Collection<User> queryAllUsers(){
return users.values();
}
public User getUserById(Integer id){
return users.get(id);
}
public void deleteUserById(Integer id){
users.remove(id);
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.itdragon.springmvc.crud.orm.Position;
@Repository
public class PositionDao {
private static Map<Integer, Position> positions = null;
static{
positions = new HashMap<Integer, Position>();
positions.put(1, new Position(1, "架構師"));
positions.put(2, new Position(2, "高級工程師"));
positions.put(3, new Position(3, "中級工程師"));
positions.put(4, new Position(4, "初級工程師"));
positions.put(5, new Position(5, "java實習生"));
}
// 模擬查詢所有數據
public Collection<Position> queryAllPositions(){
return positions.values();
}
// 模擬通過id查詢數據
public Position getPositionById(Integer id){
return positions.get(id);
}
}
用戶列表頁面的list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC 表單操作</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function(){
$(".delete").click(function(){
var msg = confirm("確定要刪除這條數據?");
if (true == msg) {
$(this).onclick();
} else {
return false;
}
});
})
</script>
</head>
<body>
<!-- 用於刪除的form -->
<form action="" method="POST" id="deleteForm">
<input type="hidden" name="_method" value="DELETE"/>
</form>
<div class="container">
<div class="row">
<div class="col-sm-9">
<c:if test="${empty requestScope.users }">
沒有任何員工信息.
</c:if>
<c:if test="${!empty requestScope.users }">
<div class="table-responsive">
<table class="table table-bordered">
<caption>用戶信息表 <a href="user" class="btn btn-default" >Add Account</a></caption>
<thead>
<tr>
<th>用戶編碼</th>
<th>賬號名</th>
<th>郵箱</th>
<th>性別</th>
<th>職位</th>
<th>薪水</th>
<th>時間</th>
<th>編輯</th>
<th>刪除</th>
</tr>
</thead>
<tbody>
<c:forEach items="${requestScope.users }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.account }</td>
<td>${user.email }</td>
<td>${user.sex == 0 ? 'Female' : 'Male' }</td>
<td>${user.position.level }</td>
<td>${user.salary }</td>
<td><fmt:formatDate value="${user.createdDate }" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td><a href="user/${user.id}">Edit</a></td>
<td><a class="delete" href="delete/${user.id}">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:if>
</div>
</div>
</div>
</body>
</html>
注意事項
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint '
使用hibernate validator出現上面的錯誤, 需要 注意
@NotNull 和 @NotEmpty 和@NotBlank 區別
@NotEmpty 用在集合類上面
@NotBlank 用在String上面
@NotNull 用在基本類型上
如果在基本類型上面用NotEmpty或者NotBlank 會出現上面的錯,筆者將@NotEmpty用到了Date上,導致出了這個問題。若還有問題,還繼續在這里補充。
以上便是SpringMVC的表單操作,其中包含了常用知識,如數據的格式化,數據的校驗,提示信息國際化,Form標簽的用法。