Struts框架:ActionForward詳細介紹


首先,什么是"ActionForward"?

  ActionForward是 Struts1的核心類之一,其 基類僅有4個屬性:name / path / redirect / classname。在基於Struts1的Web應用程序開發過程中,Action操作完畢后程序會通過Struts1的配置文件 struts-config.xml鏈接到指定的ActionForward,傳到Struts1的核心類 ActionServlet,ActionServlet使用 ActionForward提供的路徑,將控制傳遞給下一個步驟。ActionForward控制接下來程序的走向。ActionForward代表一個應用的URI,它包括路徑和參數,例如:path=“/modify.do?method=edit&id=10”
  ActionForward的參數除了在struts-config.xml和頁面中設置外,還可以通過在Action類中添加參數,或重新在Action中創建一個ActionForward。
然后,ActionForward的“作用”是什么?
  封裝轉發路徑,通俗點說就是說完成頁面的跳轉和轉向。那它既然是轉向,到底是轉發還是重定向呢?默認的情況下,actionForward采用的是轉發的方式進行頁面跳轉的。
  我在這里再啰嗦一下吧,也給自己加深下印象,說一下轉發和重定向的區別。最大的區別就是轉發的時候,頁面的url地址不變,而重定向的時候頁面的url地址會發生變化。簡單說明一下原因,因為轉發的時候是采用的一個request(請求),既然頁面跳轉前后是同一個request,頁面url當然不會變了;而重定向采用的是2個request,頁面跳轉前后的url當然會不同了。對轉發與重定向還有問題或者想要詳細了解的請點擊: http://www.cnblogs.com/shenliang123/archive/2011/10/27/2226892.html
  好,說到這,大家會問,既然actionForward跳轉的方式默認的是轉發,那如果我非要用重定向的方式,該如何設置呢?恩,這很簡單,大家都在struts-config.xml坐過actionForward的配置吧,比如這句<forward name="login" path="/login.jsp" redirect="true"/>

  其實通常我們都沒寫redirect屬性,而是采取的是它的默認值false,表示的是轉發,如果設置為true,即為重定向。

  下面再說一下全局和局部ActionForward的概念。在編程的時候,我們有時候會遇到這種情況,當增加一條記錄成功時,跳轉到成功頁面,在struts-config.xml我們做對“增加”這個action加一個<forward name="success" path="/login_success.jsp"/>;在修改一條記錄成功時,我們也會跳轉到成功頁面,在struts-config.xml我們做對“修改”這個action加一個<forward name="success" path="/login_success.jsp"/>;而同樣當刪除一條記錄成功時,跳轉到成功頁面,在struts-config.xml我們又對“刪除”這個action加一個同樣的<forward name="success" path="/login_success.jsp"/>;相信大家會對同樣的代碼重復的寫干到反感吧,那有沒有辦法解決呢?有,那就是全局ActionForward,針對上面這種情況,我們在<action-mappings>標簽的上面加上

1 <global-forwards>  
2     <forward name="success" path="/login_success.jsp"/>  
3 </global-forwards> 

  上面就是對全局ActionForward的一種配置,而我們之前寫的那些就是局部ActionForward。那么如果局部ActionForward和全局ActionForward同時出現,到底是按照哪個配置進行頁面跳轉呢?規則很簡單,采用的是就近原則,就是說如果有局部ActionForward,就按照局部ActionForward就行跳轉,如果沒有就按照全局的跳轉。

  相信有的朋友會說,我怎么不早說,ActionForward還可以設置重定向的跳轉方式,然后他還不想對原來的struts-config.xml文件進行修改,想在寫的Java文件的action中改,如下

1 ActionForward af = mapping.findForward("login");  
2 af.setRedirect(false); 

  這樣當然也是可以的啦,不過記住要重啟服務器,因為struts-config.xml文件不允許動態修改。

  我還想說明一點的是,我們也可以不依賴struts的ActionForward進行轉向,不要學了一樣,忘了之前學的,之前我們都是通過response進行轉向的,在struts中,我們當然也可以,在action中覆寫execute方法,如下:

 1 @Override  
 2     public ActionForward execute(ActionMapping mapping, ActionForm form,  
 3             HttpServletRequest request, HttpServletResponse response)  
 4             throws Exception {  
 5           
 6             //重定向  
 7             response.sendRedirect(request.getContextPath() + "/login.jsp");  
 8             return null;  
9
    }  

  注意:return null是必須的。

  下面在介紹下動態ActionForward,動態的ActionForward是可以運行期修改的。有這么個場景:頁面有一個輸入域,我輸入1的時候,跳轉到1的頁面,輸入2跳轉到2的頁面,我們利用之前學過的知識來實現下,先配置下XML:

 1 <action-mappings>
 2     ······
 3     <action path="/dynaactionforward"  
 4             type="com.bjsxt.struts.DynaActionForwardTestAction" >  
 5   
 6         <forward name="page1" path="/page1.jsp"/>  
 7         <forward name="page2" path="/page2.jsp"/>  
 8       
 9     </action>  
10 </action-mappings>  

  Jsp頁面如下:

1 <form action="dynaactionforward.do" method="post">  
2         頁面:<input type="text" name="page"><br>  
3         <input type="submit" value="提交">   
4 </form>  

  Java action如下:

 1 public class DynaActionForwardTestAction extends Action {  
 2     @Override  
 3     public ActionForward execute(ActionMapping mapping, ActionForm form,  
 4             HttpServletRequest request, HttpServletResponse response)  
 5             throws Exception {  
 6           
 7         String page = request.getParameter("page");  
 8         ActionForward af = null;  
 9         if ("1".equals(page)) {  
10             af = mapping.findForward("page1");  
11         }else if ("2".equals(page)) {  
12             af = mapping.findForward("page2");  
13         }  
14         return af;  
15       
16     }  
17

  相信這對大家來說是小菜一碟啦,但是如果我想在此基礎上實現當我輸入3,跳轉到3的頁面,輸入4,5,。。。以此類推下去,怎么辦呢,如果還是采用這種方式,很麻煩吧,改動的太多啦。此時我們可以考慮采用動態ActionForward,說白了就是我們自己構造一個ActionForward,通過new的方式,看一下改后的action

 1 public class DynaActionForwardTestAction extends Action {  
 2     @Override  
 3     public ActionForward execute(ActionMapping mapping, ActionForm form,  
 4             HttpServletRequest request, HttpServletResponse response)  
 5             throws Exception {  
 6           
 7         String page = request.getParameter("page");  
 8         ActionForward af = new ActionForward();  
 9         af.setPath("/page" + page + ".jsp?name=Tom");  
10         return af;  
11     }  
12

  之后,再把XML中的<forward name="page1" path="/page1.jsp"/>,<forward name="page2" path="/page2.jsp"/>刪除即可。大家還會發現動態ActionForward還有個好處,就是可以跟參數,此例中傳遞了name=tom的參數。

  本文非原創,引用於:http://blog.csdn.net/fengg5241/article/details/6539641

  支持原創!


免責聲明!

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



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