Struts2提供了基于验证框架的输入校验,在这种校验方式下,所有的输入校验只需要编写简单的配置文件,Struts2的验证框架将会负责进行服务器校验和客户端校验。
校验失败后将Struts2将自动返回名为“input”的Result,如需制定错误显示页面,则通过struts.xml来配置“input”的Result。在Result视图中使用<s:fielderror/>标签即可输入校验失败的提示信息。
User.java
package models; import java.util.Date; import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; public class User { private String username; private String password; private Date birth; public String getUsername() { return username; } // 基于注解的输入校验 @RequiredStringValidator(key = "username.requried", message = "") @RegexFieldValidator(regex = "\\w{4,25}", key = "username.regex", message = "") public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public int hashCode() { // TODO Auto-generated method stub return getUsername().hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj != null && obj.getClass() == User.class) { User objUser = (User) obj; if (objUser.getUsername().equals(this.getUsername())) { return true; } } return false; } }
login.jsp
<%@ 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><s:text name="login page"></s:text></title> </head> <body> <s:form action="login.action" validate="true"> <s:textfield name="user.username" key="username" /> <s:password name="user.password" key="password" /> <s:textfield name="user.birth" key="birth" /> <s:submit value="login" /> </s:form> </body> </html>
LoginAction.java
package actions; import models.User; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { if (getUser().getUsername().equals("yangys") && getUser().getPassword().equals("666666")) { return SUCCESS; } return ERROR; } }
Struts2的校验文件规则为:每个Action都有一个校验文件,该文件的文件名应该遵守的规则<ActionName>-validation.xml
LoginAction-validation.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="user.username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="user.username.requried"/> </field-validator> </field> <field name="user.password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="user.password.requried"/> </field-validator> <field-validator type="regex"> <param name="regex"><![CDATA[(\w{4,25})]]></param> <message key="user.password.regex"/> </field-validator> </field> <field name="user.birth"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2016-02-14</param> <message key="user.birth.range"/> <!-- <message>生日必须在1900-01-01到2016-02-14之间</message> --> </field-validator> </field> </validators>
其中message标签内为校验失败后的提示信息,可以直接写在<message></message>标签内,也可以通过<message key=""/>从国际化资源文件中提取显示。
LoginAction_zh_CN.properties(native2ascii处理过)
user.username.requried=\u5FC5\u987B\u8F93\u5165\u7528\u6237\u540D user.password.requried=\u5FC5\u987B\u8F93\u5165\u5BC6\u7801 user.password.regex=\u5BC6\u7801\u5FC5\u987B\u662F\u5B57\u6BCD\u6216\u6570\u5B57\uFF0C\u957F\u5EA6\u57284\u523025\u4E4B\u95F4 user.birth.range=\u751F\u65E5\u5FC5\u987B\u5728${min}\u548C${max}\u4E4B\u95F4
时间: 2024-10-07 15:21:59