表单数据的验证:
前台验证:主要是通过JS验证, 表达数据是否合法!
后台验证:通过后台java代码进行验证!
Struts也提供了数据效验的方式!
Struts数据效验, 通过拦截器完成:
<interceptor
name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
代码方式验证Action中所有的方法:
1.在idea中直接创建struts项目
2.写注册页面
3.UserAction.java
4.以及实体类User.java
5.配置struts.xml
文件目录结构
- 注册页面
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2017/5/31
Time: 12:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="/user_register" method="post">
用户名:<input type="text" name="user.userName"><br>
密码:<input type="password" name="user.passWord"><br>
邮箱:<input type="text" name="user.email"><br>
生日:<input type="text" name="user.birth"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
- 实体类User.java
package validation;
import java.util.Date;
/**
* Created by admin on 2017/5/31.
*/
public class User {
private String userName;
private String passWord;
private String email;
private Date birth;
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", email='" + email + '\'' +
", birth=" + birth +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
- action类UserAction
package validation;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by admin on 2017/5/31.
*/
public class UserAction extends ActionSupport {
//封装数据
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//数据校验
public void validateRegister() {//验证指定的方法
//用户名
if (user.getUserName() == null || "".equals(user.getUserName())) {
super.addFieldError("userName", "用户名不能为空!");
}
//密码
if (user.getPassWord() == null || "".equals(user.getPassWord())) {
super.addFieldError("passWord", "密码不能为空!");
}
}
//业务处理方法
public String register(){
System.out.println(user);
return SUCCESS;
}
}
- struts.xml配置
1.配置数据效验过滤器validate.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="validate" extends="struts-default">
<action name="user_*" class="validation.UserAction" method="{1}">
<result name="success">/register.jsp</result>
</action>
</package>
</struts>
2.在struts.xml中引入数据校验配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<include file="validation/validate.xml"/>
</struts>
XML方式验证Action中所有的方法:
总结代码方式验证:
- 繁琐,设计很多重复的验证逻辑!例如:非空验证、数值验证、email、日期等。
- Struts对于常用的验证,进行了封装,即提供了验证器, 验证指定的常用业务逻辑!
可以在jar包里找到该文件,其路径为:\struts04\lib\xwork-core.jar!\com\opensymphony\xwork2\validator\validators\default.xml
在struts里面提供了所有通用的验证器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator Definition 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">
<!-- START SNIPPET: validators-default -->
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>
<!-- END SNIPPET: validators-default -->
创建一个xml验证器,需要遵循一定的验证规则
- xml文件名称语法:ActionClassName-validation.xml
注意:此xml需要与当期要验证的action在同一个目录:
举例:UserAction-validation.xml - 例子
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.dtd">
<validators>
<!--验证表的每一个字段field表示-->
<field name="user.userName">
<!--指定使用的验证器-->
<field-validator type="requiredstring">
<!--验证错误的提示-->
<message>用户名不能为空!</message>
</field-validator>
</field>
<!-- 验证password -->
<field name="user.passWord">
<!-- 非空 -->
<field-validator type="requiredstring">
<message>密码不能为空!</message>
</field-validator>
<!-- 长度 -->
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">8</param>
<message>密码必须为6-8位!</message>
</field-validator>
</field>
<!-- 验证日期 -->
<field name="user.birth">
<field-validator type="date">
<message>日期格式不对!</message>
</field-validator>
</field>
<!-- 验证Email -->
<field name="user.email">
<field-validator type="email">
<message>邮箱格式错误!</message>
</field-validator>
</field>
</validators>
验证:
当不遵循格式随意输出时:
控制台就会出现错误:
当遵循格式输出时:
控制台显示的结果为: