Struts2 提供了兩種校驗方式。
使用編碼方式進行校驗
新建 register.jsp 頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h2 style="color:blue;">用戶註冊</h2>
<s:actionerror cssStyle="color:red;" />
----------------------------------------------------------------------
<%--
<form action="register.action">
username: <input type="text" name="username" size="20" /><br/>
password: <input type="password" name="password" size="20" /><br/>
repassword: <input type="password" name="repassword" size="20" /><br/>
age: <input type="text" name="age" size="20" /><br/>
birthday: <input type="text" name="birthday" size="20"/><br/>
geaduation: <input type="text" name="graduation" size="20"/><br/>
<input type="submit" value="submit"/>
</form>
--%>
<s:fielderror cssStyle="color:blue;"></s:fielderror>
<s:form action="register" theme="simple">
username:<s:textfield name="username" label="username"></s:textfield><br/>
password: <s:password name="password" label="password"></s:password><br/>
repassword: <s:password name="repassword" label="repassword"></s:password><br/>
age: <s:textfield name="age" label="age"></s:textfield><br/>
birthday: <s:textfield name="birthday" label="birthday"></s:textfield><br/>
geaduation: <s:textfield name="graduation" label="graduation"></s:textfield><br/>
<s:submit value="submit"></s:submit>
</s:form>
<%--
<s:form action="register">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:password name="repassword" label="repassword"></s:password>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="birthday" label="birthday"></s:textfield>
<s:textfield name="graduation" label="graduation"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
--%>
</body>
</html>
流程處理的 Action
package space.terwer.struts23;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String repassword;
private Integer age;
private Date birthday;
private Date graduation;
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 getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
@Override
public void validate() {
// System.out.println("validate incoked");
if (null == this.getUsername() || this.getUsername().length() < 4 || this.getUsername().length() > 6) {
this.addActionError("username invalid");
this.addFieldError("username", "username invalid in field");
this.addFieldError("username", "username invalid in field2");
}
if (null == this.getPassword() || this.getPassword().length() < 4 || this.getPassword().length() > 6) {
this.addActionError("password invalid");
} else if (null == this.getRepassword() || this.getRepassword().length() < 4
|| this.getRepassword().length() > 6) {
this.addActionError("repassword invalid");
} else if (!this.getPassword().equals(this.getRepassword())) {
this.addActionError("the passwords not same");
}
if (this.getAge() < 10 || this.getAge() > 50) {
this.addActionError("age invalid");
}
if (null == this.getBirthday()) {
this.addActionError("birthday invalid");
}
if (null == this.getGraduation()) {
this.addActionError("graduation invalid");
}
if (null != this.getBirthday() && null != this.getGraduation()) {
Calendar c1 = Calendar.getInstance();
c1.setTime(this.getBirthday());
Calendar c2 = Calendar.getInstance();
c2.setTime(this.getGraduation());
if (c1.after(c2)) {
this.addActionError("birthday should be before graduation");
}
}
// 注意:這樣不行
// this.getFieldErrors().clear();
// this.getActionErrors().clear();
// 下面的代碼可以直接清空錯誤,直接跳轉
// this.clearFieldErrors();
// this.clearActionErrors();
System.out.println("error cleared");
}
@Override
public String execute() throws Exception {
// System.out.println("execute invoked");
return SUCCESS;
}
}
流程處理 struts.xml
<action name="register" class="space.terwer.struts23.RegisterAction">
<result name="success">/registerResult.jsp</result>
<result name="input">/register.jsp</result>
</action>
結果頁面 registerResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register result</title>
</head>
<body>
username: <s:property value="username"/><br/>
password: <s:property value="password"/><br/>
age: <s:property value="age"/><br/>
birthday: <s:property value="birthday"/><br/>
graduation: <s:property value="graduation"/><br/>
</body>
</html>
效果
加入字段校驗之後
總結
-
執行流程:然後進行輸入校驗(執行
validate
方法)- 首先進行類轉換
- 然後進行輸入校驗(執行
validate
方法) - 如果在上述過程中出現了任何錯誤,都不會再去執行
execute
方法,頁面會轉向struts.xml
該Action
的名爲input
的result
所對應的頁面。
-
ActionSupport
類的addActionError()
方法的視線:首先創建一個ArrayList
對象,然後將錯誤消息添加到該ArrayList
對象中。 -
當調用
getActionErrors()
方法返回Action
級別的錯誤信息列表時,返回的實際上是集合的一個副本,而非原集合中的元素,因此對集合副本調用 clear() 方法清除的依舊是副本中的元素而非原集合中的元素,此時原集合中的元素沒有受到任何影響。如果需要清空,可以調用
注意:清空錯誤的方法不能使用
clearActionErrors()
,這個操作的是錯誤對象列表。// 注意:這樣不行 this.getFieldErrors().clear(); this.getActionErrors().clear();
必須使用
// 下面的代碼可以直接清空錯誤,直接跳轉 this.clearFieldErrors(); this.clearActionErrors();
-
FieldError
級別的錯誤信息底層是用LinkedHashMap
實現的,該Map
的key
是String
類型,value
是List<String>
類型,這表示一個Field Name
可以對應多條錯誤信息,這些錯誤信息都放置在List<String>
集合中。