- 輸入校驗(yàn)
- 編程式校驗(yàn)(對(duì)action內(nèi)所有方法進(jìn)行校驗(yàn))
個(gè)人感覺應(yīng)該就是類似裝飾器的意思往衷。。口叙。
這個(gè)方法必須實(shí)現(xiàn)實(shí)現(xiàn)Validateable接口炼绘,重寫其中的validate方法,因?yàn)槲覀兊腶ction繼承自actionSupport妄田,actionsupport幫我們實(shí)現(xiàn)了這接口俺亮,所以我們只需要重寫validate方法。
package action;
import com.opensymphony.xwork2.ActionSupport;
import model.CheakModel;
public class CheakAction extends ActionSupport {
private CheakModel cheakModel;
@Override
public void validate() {
String user_name = cheakModel.getUser_name();
if(user_name.trim().equals("") || user_name == null || user_name.length() == 0){
addFieldError("name","error username!");
}
}
@Override
public String execute() throws Exception {
return "success";
}
public CheakModel getCheakModel() {
return cheakModel;
}
public void setCheakModel(CheakModel cheakModel) {
if(this.cheakModel == null){
this.cheakModel = cheakModel;
}
}
}
在使用addFieldError的時(shí)候發(fā)現(xiàn)會(huì)報(bào)沒有定義result的錯(cuò)誤
Could not find action or result: /Mystruts2_new_war_exploded/cheak?cheakModel.user_name=&cheakModel.password=123&cheakModel.age=5
com.opensymphony.xwork2.config.ConfigurationException: No result defined for action action.CheakAction and result input
發(fā)現(xiàn)是因?yàn)樵趈sp頁(yè)面中沒有使用<s>來實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)疟呐,要解決這個(gè)也很簡(jiǎn)單脚曾,在jsp頁(yè)面設(shè)置為<s:url action = "your action">就可以了。但是也可以在struts.xml中為添加一個(gè)name為input的result
<action name="cheak" class="action.CheakAction" method="execute">
<result>/cc.jsp</result>
<result name="input">/ii.jsp</result>
</action>
這樣頁(yè)面就會(huì)出現(xiàn)你自己設(shè)置的錯(cuò)誤信息~
- 在xml中設(shè)置校驗(yàn)
action中必須為屬性提供getXXX启具、setXXX方法本讥,因?yàn)榇a校驗(yàn)是在Action本類中來完成校驗(yàn),這說明我們可以直接使用本類的private屬性,但如果使用XML配置方式校驗(yàn)拷沸,這需要使用校驗(yàn)框架的代碼來完成校驗(yàn)工作色查,那么校驗(yàn)框架需要調(diào)用Action的getXXX()方法來獲取被校驗(yàn)的屬性,所以一定要為被校驗(yàn)的屬性提供getXXX()方法- 創(chuàng)建 xml 文件
路徑:必須與action同包下
命名規(guī)范:
actionClass-actionName-validation.xml
actionClass:action的類名
actionName:action的訪問名稱撞芍,即 在struts.xml中配置的秧了,<action name="">
validation.xml:固定后綴名⌒蛭蓿
比如:Demo02Action-Demo02Action_add-validation.xml 這種是對(duì)特定方法進(jìn)行校驗(yàn)
- 創(chuàng)建 xml 文件
-
填寫xml內(nèi)容
struts2 中有很多的內(nèi)置校驗(yàn)方法
圖片.png
-
若使用xml配置的方式實(shí)現(xiàn)校驗(yàn)验毡,并且想看到錯(cuò)誤信息
在jsp文件中需要這么寫
<s:fielderror fieldName="cheakModel.user_name"></s:fielderror> // 這個(gè)是你想看到的字段的校驗(yàn)錯(cuò)誤信息
在 struts2.5中
xml的頭必須這樣寫
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
以前是
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
還使用以前的話會(huì)導(dǎo)致以下錯(cuò)誤
Local DTD is missing for publicID
完整的xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name = "cheakModel.user_name">
<field-validator type = "requiredstring">
<param name = "trim">false</param>
<message>space in username</message>
</field-validator>
</field>
</validators>