1.在springMVC配置文件配置添加如下信息

<!-- 表单验证框架 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> </bean>
<!-- 配置错误信息用于国际化信息验证 -->
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validatemessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>

2.在你对想要验证的Javabean中添加如下信息注解

package com.xtc.bean.teacher;

import java.util.Date;

import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotEmpty;

public class Teacher {
/***
* @author
* time 2016-8-9
**/ //教师ID
private String id;
//教师姓名
@NotEmpty(message="姓名不能为空")
private String name;
//登录密码
@NotEmpty(message="密码不能为空")
private String password;
//性别
@Pattern(regexp="[男女]",message="只能为男或者女,且不能为空")
private String sex;
//个人简历
@NotEmpty(message="个人简介不能为空")
private String description;
//头像URL
@NotEmpty(message="头像Url不能为空")
private String picUrl;
//学校名称
@NotEmpty(message="学校名称不能为空")
private String schoolName;
//注册时间
private Date registDate;
//备注
private String remark;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public Date getRegistDate() {
return registDate;
}
public void setRegistDate(Date registDate) {
this.registDate = registDate;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
} }

3.在controller中代码如下

@RequestMapping("/addTeacher")
public ModelAndView addTeacher(@Valid Teacher teacher,BindingResult result){ Map< String, Object> map=new HashMap<String, Object>(); if (result.hasErrors()) {
List<FieldError> errors=result.getFieldErrors();
for (FieldError fieldError : errors) {
map.put(fieldError.getField(), fieldError.getDefaultMessage());
}
return new ModelAndView("insert",map);
}
else {
teacher.setId(uutil.UUIDUtil.getUUID());
teacher.setRegistDate(new Date());
teacherService.insert(teacher);
return new ModelAndView("success");
}
}

@Valid Teacher teacher注解teacher,对表单提交过来的teacher这个bean进行校验,返回校验结果给BindingResult result这个对象,在result.hasErrors()中循环把错误信息添加到map对象里面返回到insert.jsp页面

4.insert.jsp页面代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加教师数据</title> </head> <body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="<%=basePath + "/teacher/getAllTeacher"%>">首页</a>
</div> <div class="collapse navbar-collapse"
id="bs-example-navbar-collapse-1"> <form class="navbar-form navbar-left" role="search"
action="getTeacherByName" method="post">
<div class="form-group">
<input type="text" class="form-control" placeholder="教师名字"
name="getName">
</div>
<button type="submit" class="btn btn-default btn-lg">
<span class="glyphicon glyphicon-search" aria-hidden="true">
</span>
</button> </form> <ul class="nav navbar-nav ">
<li><a class="btn btn-default btn-lg" href="<%=basePath + "/teacher/insert"%>">
<span class="glyphicon glyphicon-plus" aria-hidden="true">
</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid --> </nav>
<center>
<h3>添加教师</h3> <form action="addTeacher" method="post" class="form-horizontal">
<div class="table-responsive">
<table width="500" class="table" >
<tr> <td align="right">姓名:</td>
<td><input type="text" name="name"></td>
<td style=" color:#CC3300">${name}</td> </tr>
<tr> <td align="right">登录密码: </td>
<td><input type="password" name="password" id="inputPassword3" ></td>
<td style=" color:#CC3300">${password}</td> </tr>
<tr>
<td align="right">性别:</td>
<td><input type="text" name="sex"></td>
<td style=" color:#CC3300">${sex}</td>
</tr>
<tr>
<td align="right">个人简介:</td>
<td><input type="text" name="description"></td>
<td style=" color:#CC3300">${description}</td>
</tr>
<tr>
<td align="right">头像Url:</td>
<td><input type="text" name="picUrl"></td>
<td style=" color:#CC3300">${picUrl}</td>
</tr>
<tr>
<td align="right">学校名称:</td>
<td><input type="text" name="schoolName"></td>
<td style=" color:#CC3300">${schoolName}</td>
</tr>
<tr>
<td align="right">备注:</td>
<td><input type="text" name="remark"></td>
<td style=" color:#CC3300"></td>
</tr> </table>
</div>
<input type="submit" value="确认添加" class="btn btn-default"> </form> </center>
</body>
</html>

 最后附上结果截图:

点击确认添加后如下

框架所需要的jar包在这里http://download.csdn.net/detail/qq_32780741/9601388