1. 程式人生 > >struts2如何繼承ActionSupport類?

struts2如何繼承ActionSupport類?

在Struts2中,Action可以不實現任何特殊的介面或者繼承特殊的類,僅僅是一個POJO就可以;也可以實現Xwork2中的Action介面;但是由於Xwork的Action介面非常簡單,為程式設計師提供的幫助有限,因此,在實際開發中,會更多的使用繼承ActionSupport類來實現Action的方式,如下所示:

import com.opensymphony.xwork2.ActionSupport;  
public class HelloWorldAction extends ActionSupport {  

} 

ActionSupport類本身實現了Action介面,所以繼承ActionSupport類就相當於實現了Action介面。除此之外,ActionSupport類還實現了其它幾個介面,來為程式設計師提供更多使用的功能,這些介面和Struts2的一些其他特性相結合,可以實現基本的資料驗證功能和國際化。介面如下所示:

com.opensymphony.xwork2.Validateable;  //提供validate()方法來為Action增加驗證的功能
com.opensymphony.xwork2.Validateaware; //提供方法來儲存和恢復action或field級的錯誤資訊
com.opensymphony.xwork2.TextProvider;  //提供獲取本地資訊文字的功能
com.opensymphony.xwork2.LocaleProvider;//提供getLocale()方法來獲取本地訊息

要實現資料驗證的功能,只需要在Action類中覆蓋實現validate方法即可;在validate方法內部,對請求傳遞過來的資料進行校驗,如果不滿足要求,那麼新增例外資訊到父類用於存放例外的集合中。示例程式碼如下:

package cn.javass.hello.struts2impl.action;  

import com.opensymphony.xwork2.ActionSupport;
  
public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    public void validate(){  
        if(account==null || account.trim().length()==0){  
            this.addFieldError("account", "賬號不可以為空");  
        }  
        if(password==null || password.trim().length()==0){  
            this.addFieldError("password", "密碼不可以為空");  
        }
        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
            this.addFieldError("password", "密碼長度至少為6位");  
        }  
    }  
    /** 
     * 示例方法,表示可以執行業務邏輯處理的方法, 
     */  
    public void businessExecute(){  
        System.out.println("使用者輸入的引數為==="+"account="+account+",password="+password+",submitFlag="+submitFlag);  
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSubmitFlag() {
        return submitFlag;
    }
    public void setSubmitFlag(String submitFlag) {
        this.submitFlag = submitFlag;
    }  
    
}  

從上面的示例可以看出,在validate方法中,可以對使用者請求中傳遞過來的資料進行驗證,同一個資料可以進行多方面的驗證。如果驗證結果是資料不正確,那麼就使用父類提供的addFieldError方法來新增驗證的錯誤訊息。addFieldError方法有兩個引數,前面的是訊息的key值,後面是具體的訊息。

細心的你肯定發現了,validate方法是沒有返回值的,那麼當驗證後,如果有資料沒有通過驗證,該返回到什麼頁面呢?這就需要在struts.xml中的Action配置裡面,新增一個名稱為input的result配置,也就是說,如果validate方法中,有資料沒有通過驗證,那麼會自動跳轉回到該action中名稱為input的result所配置的頁面。示例如下:
<struts>  
    <constant name="struts.devMode" value="true" />        <!-- 設定了程式的執行模式 -->
    <constant name="struts.locale" value="zh_CN"/>         <!-- 設定程式執行所使用的locale -->
    <constant name="struts.i18n.encoding" value="utf-8"/>  <!-- 設定程式執行時用的編碼方式 -->
    <!-- 正確設定後面兩個引數,就可以解決Struts2的中文問題了。 -->
    <package name="helloworld"  extends="struts-default">  
        <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">  
            <result name="toWelcome">/s2impl/welcome.jsp</result> 
             <result name="input">/s2impl/login.jsp</result>   
        </action>  
    </package> 
</struts>

當輸入資訊不滿足條件的時候,將錯誤資訊顯示在前臺頁面上,程式碼如下:

<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; utf-8">  
<title>Insert title here</title>  
<style type="text/css">
ul,li {
    list-style-type:none;
    margin:0px;
    float:left;
}
</style>
</head>  
<body>  
<form action="/struts2Deepen2/helloworldAction.action" method="post"> 
    <input type="hidden" name="submitFlag" value="login"/>  
    <div> 
        <font color=red><s:fielderror fieldName="account"/></font>
        <br/>
          賬號:<input type="text" name="account">
    </div>
    <div>
        <font color=red><s:fielderror fieldName="password"/></font>
        <br/>
            密碼:<input type="password" name="password">
    </div>
    <input type="submit" value="提交">  
</form>  
</body>  
</html>

在JSP頁面中利用<s:fielderror/>標籤在相應的欄位處輸出錯誤資訊。但是,在實際開發中,<s:fielderror/>它會輸出全部的錯誤資訊內容。而如果想選擇性地輸出指定錯誤資訊。我們可以使用如下程式碼解決:

<s:fielderror>  
  <s:param>username</s:param> <!--顯示指定的 username欄位的 錯誤訊息-->  
<s:fielderror/>

<!-- 方法二 -->
<s:fielderror fieldName="username"/> <!--顯示指定的 username欄位的 錯誤訊息--> 

通過這個示例,你會發現,validate方法會先於execute方法被執行,只有validate方法執行後,又沒有發現驗證錯誤的時候,才會執行execute方法,否則會自動跳轉到你所配置的input所對應的頁面。
 2.訪問本地資訊

在上面的示例中,你會發現在validate方法中,新增驗證錯誤訊息的時候,是採用的硬編碼方式,也就是直接寫死的字串,這是很不好的:
  ① 不容易修改,比如要改變訊息的內容,還得重新修改和編譯類;
  ② 不利於國際化,如果要把中文的資訊變換成英文的呢,同樣要重新修改和編譯類。
  可以通過訪問本地資訊的方式,把這些錯誤訊息放置到Action類外部的配置檔案中,在Action類內部只需要按照這些訊息的key值去獲取訊息即可。這樣一來,當訊息發生變化的時候,只需要修改這個訊息的配置檔案即可。
1.先來建立訊息的配置檔案,在Action類的路徑下建立一個同名的properties檔案,也就是檔名為HelloWorldAction.properties,然後在裡面按照key=value的格式,新增要使用的錯誤訊息。示例如下:

k1=\u5E10\u53F7\u4E0D\u5141\u8BB8\u4E3A\u7A7A
k2=\u5BC6\u7801\u4E0D\u5141\u8BB8\u4E3A\u7A7A
k3=\u5BC6\u7801\u957F\u5EA6\u5FC5\u987B\u57286\u4F4D\u4EE5\u4E0A
``           
            2.Action裡面,就修改validate方法,原來是直接寫的中文字串,現在應該修改成從配置檔案中獲取資訊了, 示例如下:
        

 public void validate(){  
        if(account==null || account.trim().length()==0){  
            this.addFieldError("account", this.getText("k1"));  
        }  
        if(password==null || password.trim().length()==0){  
            this.addFieldError("password", this.getText("k2"));  
        }
        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
            this.addFieldError("password", this.getText("k3"));  
        }  
    }

效果圖如下所示:

在這裡插入圖片描述