【struts2】繼承ActionSupport類


  在Struts2中,Action可以不實現任何特殊的接口或者繼承特殊的類,僅僅是一個POJO(Plain Old Java Object,簡單的Java對象)就可以;也可以實現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()方法來獲取本地消息

  1)基本的數據驗證

  • 要實現數據驗證的功能,只需要在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;
    }  
    
}  
View Code

  從上面的示例可以看出,在validate方法中,可以對用戶請求中傳遞過來的數據進行驗證,同一個數據可以進行多方面的驗證。

  如果驗證結果是數據不正確,那么就使用父類提供的addFieldError方法來添加驗證的錯誤消息。addFieldError方法有兩個參數,前面的是消息的key值,后面是具體的消息。

  • 細心的你肯定發現了,validate方法是沒有返回值的,那么當驗證后,如果有數據沒有通過驗證,該返回到什么頁面呢?這就需要在struts.xml中的Action配置里面,添加一個名稱為input的result配置,也就是說,如果validate方法中,有數據沒有通過驗證,那么會自動跳轉回到該action中名稱為input的result所配置的頁面。示例如下:
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
<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> 
View Code
  • 當輸入信息不滿足條件的時候,將錯誤信息顯示在前台頁面上,代碼如下:
<%@ 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>  
View Code

  在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值去獲取消息即可。這樣一來,當消息發生變化的時候,只需要修改這個消息的配置文件即可。

  • 先來建立消息的配置文件,在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

  可能你會覺得很奇怪,這都是些什么呀?其實是把中文的消息轉換成了相應的unicode編碼,比如k1后面的value值,其實就是“帳號不允許為空”的unicode編碼。只有這樣,在程序里面讀取到這些值的時候才會正確顯示中文。有很多工具可以把中文轉換成unicode編碼,比如,native2ascii工具就可以實現。

  • 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"));  
        }  
    }  

效果圖如下所示:

  參考資料:

  1)http://www.iteye.com/topic/1124526

  2)http://blog.csdn.net/csscnu/article/details/7877792


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM