1. 验证框架 示例
表单提交Jsp, reg.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>reg.jsp</title>
</head>
<body>
<s:actionerror/>
<!-- validate="true" 能够在客户端生成脚本校验 -->
<s:form namespace="/validate" action="ValidateAction_reg" method="post" theme="xhtml" validate="true">
<s:textfield name="name" label="UserName" />
<s:textfield name="age" label="UserAge" />
<s:textfield name="password" label="password" />
<s:textfield name="confirmPassword" label="confirmPassword" />
<s:submit />
</s:form>
</body>
</html>
处理action
public class ValidateAction extends ActionSupport {
private static final long serialVersionUID = -6933309304624396640L; private String name;
private Integer age;
private String password ;
private String confirmPassword ; public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getConfirmPassword() {
return confirmPassword;
} public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String reg() {
return "success";
} @SkipValidation
public String toRegView() {
System.out.println("toRegView");
return "regView";
}
}
struts2配置文件 validate.xml, 校验表单需要加 input result,有错误回跳
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="ValidatePkg" namespace="/validate" extends="struts-default">
<action name="ValidateAction_*" class="cn.itcast.struts2.validate.ValidateAction" method="{1}">
<result name="success">/validate/reg.jsp</result>
<result name="regView">/validate/reg.jsp</result>
<result name="input">/validate/reg.jsp</result>
</action>
</package>
</struts>
ValidateAction-validation.xml 校验选项配置
<?xml version="1.0"?>
<!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">
<message>name is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">10</param>
<message key="error.name.length" />
</field-validator>
<field-validator type="email">
<message>name is invalid email address!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message>age is required</message>
</field-validator>
<field-validator type="int">
<param name="min">20</param>
<param name="max">100</param>
<message>age rang is wrong!</message>
</field-validator>
</field>
<validator type="expression">
<param name="expresssion">password == confirmPassword</param>
<message>password input isn't the same!</message>
</validator>
</validators>
message 国际化 文件 ValidateAction.properties
label.name=userName
error.name.length=${name} is goog name,but length must be between ${minLength} and ${maxLength}.
字段校验type list