1. 程式人生 > >Struts2框架中為什麼要繼承ActionSupport類,以及實現過程

Struts2框架中為什麼要繼承ActionSupport類,以及實現過程

   struts可以繼承ActionSupport類,也可以不繼承,繼承的好處簡單來說就是更方便實現驗證,國際化等功能,與struts2的功能結合緊密,方便我們開發。

ActionSupport類的作用:

     此類實現了很多實用的介面,提供了很多預設的方法,這些預設方法包括國際化資訊,預設的處理使用者請求的方法等,可以大大簡化action的開發,在繼承ActionSupport的情況下,必須有無參建構函式

下面用在struts2框架搭建完成的基礎上,用 使用者請求的例子來實現ActionSupport類:

1.建立檢視層兩個頁面index.jsp和welcome.jsp頁面,下面只展示index.jsp頁面,welcome.jsp頁面的程式碼自己簡單寫一下看一下效果就行。這個jsp頁面的<s:fielderror/>

標籤會在相應的欄位處輸出錯誤資訊。

<%@ 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="helloworldAction" 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>

2.實現action類封裝HTTP請求引數,類裡面應該包含與請求引數對應的屬性,併為屬性提供get,set方法,再說一次,在繼承ActionSupport的情況下,必須有無參建構函式。

validate方法內部,對請求傳遞過來的資料進行校驗,而且我們也能看出來同一個資料可以進行多方面的驗證,如果不滿足要求,內容將會在頁面上直接顯示。裡面重寫了 execute() throws Exception方法,返回字串。

package com.hnpi.action;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction 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;
		}  
	    	    

}

3.從上面我們也可以看出來,validate方法是沒有返回值的,如果驗證不成功的話,錯誤資訊該怎麼在頁面上顯示出來呢?我們需要在struts.xml中的Action配置裡面,新增一個名稱為input的result配置,沒有通過驗證,那麼會自動跳轉回到該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>
	
    <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>   
        </action>  
    </package>  
    
 </struts> 

下面我們來看一下程式碼效果圖:

這個例子可以看出來,validate方法會先於execute方法被執行,只有validate方法執行後,又沒有發現驗證錯誤的時候,才會執行execute方法,否則會自動跳轉到你所配置的input所對應的頁面。