第四个示例:注解方式校验器---用户注册页面
user2_registe.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts2中基于XML配置式的校验器使用示例</title>
</head>
<body>
<h3>XML配置式校验器---注册页面</h3><hr/>
<div style="color:red"><s:fielderror/></div>
<form action="user2_registe.action" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="user.id" value="${param['user.id']}"/></td>
</tr>
<tr>
<td>登录名</td>
<td><input type="text" name="user.name" value="${param['user.name']}"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="user.pwd"/></td>
</tr>
<tr>
<td>重复密码</td>
<td><input type="password" name="user.pwd2"/></td>
</tr>
<tr>
<td>时间</td>
<td><input type="text" name="user.registed_date" value="${param['user.registed_date']}"/></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="user.email" value="${param['user.email']}"/></td>
</tr>
<tr>
<td>考试成绩</td>
<td><input type="text" name="user.score" value="${param['user.score']}"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value=" 提交 "/></td>
</tr>
</table>
</form>
</body>
</html>
src/struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
<constant name="struts.action.extension" value="action,do,go,xkk"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="false"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 是否允许在OGNL表达式中调用静态方法,默认值为false -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<!-- 指定由spring负责action对象的创建
<constant name="struts.objectFactory" value="spring" />
-->
<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="my" extends="struts-default" namespace="/">
<!-- 使用Annotation配置校验的Action -->
<action name="user2_*" class="com.javacrazyer.web.action.User2Action" method="{1}">
<result name="success">/info.jsp</result>
<result name="input">/user2_{1}.jsp</result>
</action>
</package>
</struts>
User2Action.java
package com.javacrazyer.web.action;
import com.javacrazyer.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
/**
* 使用注解来配置校验器
*
*/
public class User2Action extends ActionSupport {
private static final long serialVersionUID = -2554018432709689579L;
private User user; //自定义类型Field
@Validations(
requiredStrings= {@RequiredStringValidator(fieldName="user.name",message="你的用户名是必须的",shortCircuit=true,trim=true,type=ValidatorType.FIELD),
@RequiredStringValidator(fieldName="user.pwd",message="人的密码是必须的",shortCircuit=true,trim=true,type=ValidatorType.FIELD)},
fieldExpressions={@FieldExpressionValidator(fieldName="user.pwd", message="两次密码不相同",expression="user.pwd==user.pwd2")},
regexFields={@RegexFieldValidator(fieldName="user.email",message="邮箱不合法",expression="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+((\\.com)|(\\.cn)|(\\.net)|(\\.org)|(\\.info)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.biz)|(\\.ws)|(\\.us)|(\\.tv)|(\\.cc)|(\\.aero)|(\\.arpa)|(\\.coop)|(\\.int)|(\\.jobs)|(\\.museum)|(\\.name)|(\\.pro)|(\\.travel)|(\\.nato)|(\\..{2,3})|(\\..{2,3}\\..{2,3}))$")}
)
public String registe() throws Exception{
System.out.println("registe======================");
return SUCCESS;
}
public String login() throws Exception{
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
另附:
User.java
package com.javacrazyer.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String pwd;
private String pwd2;
private Double score;
private Date registed_date;
private String email;
public User(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPwd2() {
return pwd2;
}
public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public Date getRegisted_date() {
return registed_date;
}
public void setRegisted_date(Date registedDate) {
registed_date = registedDate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
与user.java同一目录的User-userContext-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<!-- 字段校验 -->
<field name="id">
<field-validator type="required" short-circuit="true">
<message>ID必填的</message>
</field-validator>
<field-validator type="int">
<param name="min">20</param>
<param name="max">50</param>
<message>ID必须在 ${min} 到 ${max} 之间</message>
</field-validator>
</field>
<field name="name">
<field-validator type="requiredstring" short-circuit="true">
<message>姓名是必填的</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(^[a-zA-Z_]\w{3,9}$)]]></param>
<message>姓名不合法</message>
</field-validator>
</field>
<field name="pwd">
<field-validator type="requiredstring" short-circuit="true">
<message>密码是必填的</message>
</field-validator>
</field>
<!-- 非字段校验 -->
<validator type="fieldexpression">
<param name="fieldName">pwd</param>
<param name="fieldName">pwd2</param>
<param name="expression"><![CDATA[pwd==pwd2]]></param><!-- OGNL表达式 -->
<message>确认密码和密码输入不一致</message>
</validator>
<field name="score">
<field-validator type="double">
<param name="minInclusive">0.0</param>
<param name="maxInclusive">100.0</param>
<message>成绩必须在${minInclusive}和${maxInclusive}之间</message>
</field-validator>
</field>
<field name="email">
<field-validator type="regex">
<param name="expression"><![CDATA[(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+((\.com)|(\.cn)|(\.net)|(\.org)|(\.info)|(\.edu)|(\.mil)|(\.gov)|(\.biz)|(\.ws)|(\.us)|(\.tv)|(\.cc)|(\.aero)|(\.arpa)|(\.coop)|(\.int)|(\.jobs)|(\.museum)|(\.name)|(\.pro)|(\.travel)|(\.nato)|(\..{2,3})|(\..{2,3}\..{2,3}))$)]]></param>
<message>邮箱不合法</message>
</field-validator>
</field>
<field name="registed_date">
<field-validator type="date">
<param name="min">1970-01-01</param>
<param name="max">2019-01-01</param>
<message>注册日期不合法</message>
</field-validator>
</field>
</validators>
与User.java同一目录下的User-userLoginContext-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring" short-circuit="true">
<message>姓名是必填的</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(^[a-zA-Z_]\w{3,9}$)]]></param>
<message>姓名不合法</message>
</field-validator>
</field>
<field name="pwd">
<field-validator type="requiredstring" short-circuit="true">
<message>密码是必填的</message>
</field-validator>
</field>
</validators>
struts2在学习(十二)--表单验证的两种方式
最新推荐文章于 2018-03-27 09:39:42 发布