表单验证属性:

@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max, min) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达式
Hibernate Validator 附加的 constraint
Constraint详细信息
@Email 被注释的元素必须是电子邮箱地址
@Length 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range 被注释的元素必须在合适的范围内

1、加依赖:第一个依赖是由hibernate提供的

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.13.Final</version>
</dependency>
<dependency>
     <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

2、均是加在实体类的属性上

@NotEmpty(message=“用户不能为空”)字符串不能为空,括号内给参数message赋值,赋值内容为一旦注解为真的注释
如;
    @NotEmpty(message="账户不能为空")
    private String name;

    @NotEmpty(message="密码不能为空")
    private String pwd;

一旦为空可以返回页面后将message的内容在标签中输出 在jsp页面用form:form标签输出
例:<form:errors path=“user”/>
必须指定path为注解的属性 且此标签必须写在spring容器支持的form:form标签中

3、控制器写法

@ModelAttribute是将注解的参数以regUser为名保存到model中 也可以不指定名称 不指定就会默认以类型名第一个字母小写来保存
@Valid注解需要验证的对象 Errors errors获取到的验证报错信息对象

String register(@ModelAttribute("regUser") @Valid User user,Errors errors)
{
	if(errors.hasErrors())//判断室友通过验证
	{
		return "reg";
	}
	return "success";
	
}

在jsp中需要在form中回写ModelAttribute 写法例子:
要在head开头加上: <%@ taglib prefix=“form” uri=“http://www.springframework.org/tags/form”%>

<form:form action="register" modelAttribute="regUser" method="POST"  enctype="multipart/form-data">
用户名:<form:input path="user"/><br><form:errors path="user"/><br> 
密码:<form:input path="pwd"/><br><form:errors path="pwd"/><br>             
学历:<form:select path="education" items="${educationItems}"></form:select><br>
爱好:<form:checkboxes path="like" items="${likeItems}"/><br>
籍贯:<form:radiobuttons path="location" items="${locationItems}" itemValue="value" itemLabel="label"/><br>
生日:<form:input path="birthday"/><form:errors path="birthday"/> <br>
文件:<input type="file" name="file"/><br>
等级:<form:input path="level.value"/><form:errors path="level.value"/> <br>
<input type="submit" value="登录">
</form:form>

login a标签也可以做表单提交