一. ActionSupport是個工具類,他實現(xiàn)了Action, Validatable等接口, Validatable提供validate()方法進行數(shù)據(jù)驗證.Action只要繼承ActionSupport類,重寫validate()方法就可以進行數(shù)據(jù)驗證
二. 校驗的流程
首先,Struts框架對輸入數(shù)據(jù)進行類型轉(zhuǎn)換,然后再進行數(shù)據(jù)校驗,如果類型轉(zhuǎn)換與數(shù)據(jù)校驗都沒有錯誤發(fā)生, 就進入execute(),否則請求將被轉(zhuǎn)發(fā)到input視圖
三. 注冊實例
首先新建RegistAcion.java
- package com;
- import java.util.Date;
- import com.opensymphony.xwork2.ActionSupport;
- public class RegistAction extends ActionSupport {
- private String userName;
-
- private Integer age;
-
- private Date birthday;
-
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- 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;
- }
- @Override
- public String execute() throws Exception {
- System.out.println("注冊成功");
- return SUCCESS;
- }
- @Override
- public void validate() {
- if("".equals(userName)){
- addFieldError("userName", "username is empty");
- }
- if(null != age){
- if(1 > age || 150 < age){
- addFieldError("age", "age invalid");
- }
- }
- }
- }
package com;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegistAction extends ActionSupport {
private String userName;
private Integer age;
private Date birthday;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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;
}
@Override
public String execute() throws Exception {
System.out.println("注冊成功");
return SUCCESS;
}
@Override
public void validate() {
if("".equals(userName)){
addFieldError("userName", "username is empty");
}
if(null != age){
if(1 > age || 150 < age){
addFieldError("age", "age invalid");
}
}
}
}
配置Action
- <action name="regist" class="com.RegistAction">
- <result name="success">/welcome.jsp</result>
- <result name="input">/regist.jsp</result>
- </action>
<action name="regist" class="com.RegistAction">
<result name="success">/welcome.jsp</result>
<result name="input">/regist.jsp</result>
</action>
接著是注冊頁面和注冊成功頁面
regist.jsp
- <body>
- <form action="regist.action" method="post">
- <s:fielderror></s:fielderror>
- <table><tr>
- <td>userName:</td>
- <td>
- <input type="text" name="userName">
- </td>
- </tr>
- <tr>
- <td>age:</td>
- <td>
- <input type="text" name="age">
- </td>
- </tr>
- <tr>
- <td>birthday:</td>
- <td>
- <input type="text" name="birthday">
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <s:submit value="注冊"></s:submit>
- </td>
- </tr>
- </form>
- </body>
<body>
<form action="regist.action" method="post">
<s:fielderror></s:fielderror>
<table><tr>
<td>userName:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>age:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>birthday:</td>
<td>
<input type="text" name="birthday">
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注冊"></s:submit>
</td>
</tr>
</form>
</body>
如果不輸入userName, age輸入為abc,會提示
Invalid field value for field "age".
username is empty
1. 其中Invalid field value for field "age" 信息是struts2通過內(nèi)置的類型轉(zhuǎn)換器進行類型轉(zhuǎn)換時,如果不能成功轉(zhuǎn)換, struts2框架自動生成一條錯誤信息,并將該錯誤信息放到addFieldError里面,這種默認(rèn)的輸出信息格式是在 xwork-2.0.4.jar中定義的. com/opensymphony/xwork2/xwork-messages.properties文件中有一條xwork.default.invalid.fieldvalue=Invalid field value for field "{0}".
2. 這是一種全局的錯誤提示方式,整個系統(tǒng)中只要是字段類型轉(zhuǎn)換錯誤都會這樣提示,我們也可以改變這種輸出格式,只要在全局的國際資源文件中重寫xwork.default.invalid.fieldvalue就可以了.
實現(xiàn)方式:
在struts.xml中加入<constant name="struts.custom.i18n.resources" value="messageResource"></constant> (此處i18n,不是l,是1)
或者也可以在struts.properties中加入struts.custom.i18n.resources=messageResource
指定國際化資源文件名為messageResource. Properties
新建messageResource. Properties資源文件并添加數(shù)據(jù)xwork.default.invalid.fieldvalue={0} failure
修改之后字段類型轉(zhuǎn)換錯誤提示為 : {0} failure
3 所有的類型轉(zhuǎn)換失敗后,struts2會將基本類型設(shè)置為0,對象類型設(shè)置為null,這里的age的類型為Integer,當(dāng)類型轉(zhuǎn)換失敗age值為null,如果age的類型為int,那么轉(zhuǎn)換失敗后值為0
4.這種提示信息不夠友好,也可以定義局布的提示信息,為每一個Action新建一個properties文件,文件名為XXX.properties(Action名.properties)
實現(xiàn)方式:新建RegistAction.properties并添加
invalid.fieldvalue.age=age error
invalid.fieldvalue.birthday=birthday error
其中age和birthday分別為字段的名稱
四.
Struts2也提供類似BaseDispatchAction的功能
- package com;
- import com.opensymphony.xwork2.ActionSupport;
- public class Regist2Action extends ActionSupport {
- private String userName;
-
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String regist() throws Exception {
- System.out.println("注冊成功-regist");
- return SUCCESS;
- }
-
- public void validateRegist() {
- if(userName.equals("")){
- addFieldError("userName", "請輸入用戶名-registValidate");
- }
- }
- }
package com;
import com.opensymphony.xwork2.ActionSupport;
public class Regist2Action extends ActionSupport {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String regist() throws Exception {
System.out.println("注冊成功-regist");
return SUCCESS;
}
public void validateRegist() {
if(userName.equals("")){
addFieldError("userName", "請輸入用戶名-registValidate");
}
}
}
<action name="regist2" class="com.Regist2Action" method="regist">
<result name="success">/welcome.jsp</result>
<result name="input">/regist2.jsp</result>
</action>
指定了method為regist,當(dāng)請求時會執(zhí)行regist(),不會再去執(zhí)行默認(rèn)的execute()方法了,
validateRegist()方法是專門針對regist校驗的.(格式為validate+方法名)