|
struts可以繼承ActionSupport類,也可以不繼承,繼承的好處簡(jiǎn)單來(lái)說就是更方便實(shí)現(xiàn)驗(yàn)證,國(guó)際化等功能,與struts2的功能結(jié)合緊密,方便我們開發(fā)。
ActionSupport類的作用:
此類實(shí)現(xiàn)了很多實(shí)用的接口,提供了很多默認(rèn)的方法,這些默認(rèn)方法包括國(guó)際化信息,默認(rèn)的處理用戶請(qǐng)求的方法等,可以大大簡(jiǎn)化action的開發(fā),在繼承ActionSupport的情況下,必須有無(wú)參構(gòu)造函數(shù)。
下面用在struts2框架搭建完成的基礎(chǔ)上,用 用戶請(qǐng)求的例子來(lái)實(shí)現(xiàn)ActionSupport類:
1.創(chuàng)建視圖層兩個(gè)頁(yè)面index.jsp和welcome.jsp頁(yè)面,下面只展示index.jsp頁(yè)面,welcome.jsp頁(yè)面的代碼自己簡(jiǎn)單寫一下看一下效果就行。這個(gè)jsp頁(yè)面的<s:fielderror/>標(biāo)簽會(huì)在相應(yīng)的字段處輸出錯(cuò)誤信息。
<%@ page language="java" contentType="text/html; charset=utf-8" <%@ taglib prefix="s" uri="/struts-tags"%> <meta http-equiv="Content-Type" content="text/html; utf-8"> <title>Insert title here</title> <form action="helloworldAction" method="post"> <input type="hidden" name="submitFlag" value="login"/> <font color=red><s:fielderror fieldName="account"/></font> 賬號(hào):<input type="text" name="account"> <font color=red><s:fielderror fieldName="password"/></font> 密碼:<input type="password" name="password"> <input type="submit" value="提交">
2.實(shí)現(xiàn)action類封裝HTTP請(qǐng)求參數(shù),類里面應(yīng)該包含與請(qǐng)求參數(shù)對(duì)應(yīng)的屬性,并為屬性提供get,set方法,再說一次,在繼承ActionSupport的情況下,必須有無(wú)參構(gòu)造函數(shù)。
validate方法內(nèi)部,對(duì)請(qǐng)求傳遞過來(lái)的數(shù)據(jù)進(jìn)行校驗(yàn),而且我們也能看出來(lái)同一個(gè)數(shù)據(jù)可以進(jìn)行多方面的驗(yàn)證,如果不滿足要求,內(nèi)容將會(huì)在頁(yè)面上直接顯示。里面重寫了 execute() throws Exception方法,返回字符串。
import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport{ private String submitFlag; public String execute() throws Exception { if(account==null || account.trim().length()==0){ this.addFieldError("account", "賬號(hào)不可以為空"); if(password==null || password.trim().length()==0){ this.addFieldError("password", "密碼不可以為空"); if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){ this.addFieldError("password", "密碼長(zhǎng)度至少為6位"); public void businessExecute(){ System.out.println("用戶輸入的參數(shù)為==="+"account="+account+",password="+password+",submitFlag="+submitFlag); public String getAccount() { public void setAccount(String account) { public String getPassword() { public void setPassword(String password) { this.password = password; public String getSubmitFlag() { public void setSubmitFlag(String submitFlag) { this.submitFlag = submitFlag;
3.從上面我們也可以看出來(lái),validate方法是沒有返回值的,如果驗(yàn)證不成功的話,錯(cuò)誤信息該怎么在頁(yè)面上顯示出來(lái)呢?我們需要在struts.xml中的Action配置里面,添加一個(gè)名稱為input的result配置,沒有通過驗(yàn)證,那么會(huì)自動(dòng)跳轉(zhuǎn)回到該action中名稱為input的result所配置的頁(yè)面。
<?xml version="1.0" encoding="UTF-8"?> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts./dtds/struts-2.0.dtd"> <package name="helloworld" extends="struts-default"> <action name="helloworldAction" class="com.hnpi.action.RegisterAction"> <result name="toWelcome">/welcome.jsp</result> <result name="input">/index.jsp</result>
下面我們來(lái)看一下代碼效果圖:

這個(gè)例子可以看出來(lái),validate方法會(huì)先于execute方法被執(zhí)行,只有validate方法執(zhí)行后,又沒有發(fā)現(xiàn)驗(yàn)證錯(cuò)誤的時(shí)候,才會(huì)運(yùn)行execute方法,否則會(huì)自動(dòng)跳轉(zhuǎn)到你所配置的input所對(duì)應(yīng)的頁(yè)面。
|