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/liukem...

<%@ 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标签的用法。

SpringMVC 表单验证相关推荐

  1. SpringMVC表单验证器的使用

    转载自 SpringMVC表单验证器的使用 本章讲解SpringMVC中怎么通过注解对表单参数进行验证. SpringBoot配置 使用springboot, spring-boot-starter- ...

  2. SpringMVC表单验证与Velocity整合

    阅读本文约"1.2分钟" 定义表单类 以Login为例,有username和password两个字段 import javax.validation.constraints.Not ...

  3. Spring4 MVC表单验证代码示例

    在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...

  4. Spring in Action:@Vaild 表单验证不起作用

    在按照<Spring in Action>进行实践的过程中,发现@Vaild表单验证不起作用.最后查明原因如下: 仔细阅读<Spring in Action>,书中说: 从Sp ...

  5. Spring MVC 第四章:Form表单验证-JSR303和Spring框架验证以及国际化语言、Filter过滤器

    Form表单的验证验证,就是在提交表单的时候进行验证是否为空,是否满足特定条件才可以创建.常见的表单有前端验证和后端验证. 其中,前端验证有:HTML验证,JS验证,Jquery验证. 后端验证有:J ...

  6. spring+thymeleaf实现表单验证数据双向绑定

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  7. [JAVA EE] Thymeleaf 高级用法:模板布局,带参数的引用片段,表单验证,常用校验注解

    模板布局 公共部分通常定义为模板布局:如页眉,页脚,公共导航栏.菜单等. 模板布局定义方法 布局页中用 th:fragment 定义模板片段,其他页面用 th:insert 引用片段 例如:foote ...

  8. float js 正则 验证_使用HTML和Vuejs进行表单验证

    他们说大多数网络应用只是HTML表单.好吧,表单需要验证,谢天谢地,HTML5带有许多优秀的内置表单验证功能,可用于电子邮件,数字,最大值,分钟等.您甚至可以使用模式编写自己的验证规则.在本文中,我将 ...

  9. JavaScript 表单与表单验证

    JavaScript 表单 JavaScript 表单验证 HTML 表单验证可以通过 JavaScript 来完成. 以下实例代码用于判断表单字段(fname)值是否存在,如果存在,则弹出信息,否则 ...

最新文章

  1. 性能测试应用领域分析
  2. 卷积层计算量(FLOPS)和参数量的计算
  3. 【C 语言】结构体 ( 结构体中嵌套二级指针 | 为 结构体内的二级指针成员 分配内存 | 释放 结构体内的二级指针成员 内存 )
  4. 常考数据结构与算法:判断二叉树是否对称(迭代法,递归法)
  5. 报告显示:2018年北京人工智能相关产业达1500亿元
  6. 【疼逊】致广大QQ用户的一封信
  7. SAP UI5 Mock server,为什么运行时无法返回 JSON 类型的测试数据?
  8. where is Fiori count server execution interval configured
  9. Perl 6 语言的糟粕
  10. ROS入门笔记(二):ROS安装与环境配置及卸载(重点)
  11. php后端文件,【后端开发】php文件用啥打开
  12. 产品经理的方向感-产品生命周期
  13. VR 游戏开发资料收集
  14. Shutdown In Period 1.0
  15. 绝地求生登录计算机需要授权,Steam第三方授权登录错误 《绝地求生大逃杀》国服绑定受影响!...
  16. ArcCatalog 连接远程SDE 连接字符
  17. 信息系统项目管理师 论文
  18. Google Chrome 试用感受
  19. 视频怎么做gif表情包?教你一个快速生成的方法
  20. 微软WebMatrix介绍

热门文章

  1. Linux 学习日记 3: 环境变量与文件查找
  2. Linuxshell之高级Shell脚本编程-创建函数
  3. 【前端来刷LeetCode】两数之和与两数相加
  4. java基础-数据类型
  5. 表格行与列边框样式处理的原理分析及实战应用
  6. 乐视美国官网下线,官方公告称一周后还会回来
  7. idea 设置光标回到上一次位置的快捷键
  8. JMeter基础之一个简单的性能测试
  9. Oracle手工建库
  10. Visual C++ 对话框增加菜单栏