Struts2的输入校验(2)
——客户端校验
Struts2客户端校验的使用:
(1)使用Struts2的标签生成输入页面的表单;
(2)为该<s:form>元素添加validate="true"属性
示例:
(1)第一个输入页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注册 - 1</title> </head> <body> <s:form action="regist_1"> <s:textfield name="username" key="用户名"/> <s:password name="password" key="密码"/> <s:textfield name="age" key="年龄"/> <s:textfield name="birth" key="生日"/> <tr> <td rowspan="1"> <s:reset value="重置" theme="simple"/> <s:submit value="注册" theme="simple"/> </td> </tr> </s:form> </body> </html>
(2)返回的重新输入页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:fielderror/> <s:form action="regist_1" validate="true"> <s:textfield name="username" key="用户名"/> <s:password name="password" key="密码"/> <s:textfield name="age" key="年龄"/> <s:textfield name="birth" key="生日"/> <tr> <td rowspan="1"> <s:reset value="重置" theme="simple"/> <s:submit value="注册" theme="simple"/> </td> </tr> </s:form> </body> </html>
(3)Action类:
import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class Regist1Action extends ActionSupport{ //封装请求参数 private String username; private String password; private int age; private Date birth; //setter、getter方法 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } //处理逻辑使用ActionSupport基类的默认方法 }
(4)校验规则的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!-- 指定校验配置文件的DTD信息 --> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <!-- 校验文件的根元素 --> <validators> <!-- 校验Action的username属性 --> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>必须输入用户名</message> </field-validator> <field-validator type="regex"> <param name="regex"><![CDATA[(\w(4,25))]]></param> <message>输入的用户名必须是字母和数字,且长度在4-25之间</message> </field-validator> </field> <!-- 校验Action的password属性 --> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>必须输入密码</message> </field-validator> <field-validator type="regex"> <param name="regex"><![CDATA[(\w(4,25))]]></param> <message>输入的密码必须是字母和数字,且长度在4-25之间</message> </field-validator> </field> <!-- 校验Action的age属性 --> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年纪必须在1-150之间</message> </field-validator> </field> <!-- 校验Action的birth属性 --> <field name="birth"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2050-01-01</param> <message>输入的生日必须在${min}到${max}之间</message> </field-validator> </field> </validators>
(5)注意事项:
浏览者不能直接访问使用客户端校验的表单页。可以把启用客户端校验的表单页放在WEB-INF路径下,让浏览者访问所有资源前都经过它的核心Filter。
时间: 2024-10-23 18:53:38