1. 程式人生 > >Struts2通用的action 配置

Struts2通用的action 配置

struts.xml   配置一個<action/>, 如下:

Xml程式碼  收藏程式碼
  1. <!-- 通用配置     結合了spring,在spring的bean名稱 是registerAction-->
  2. <actionname="*_*"class="{1}Action"method="{2}">
  3.         <resultname="success">/{1}/success.jsp</result>
  4.     <resultname="input">/{1}/input.jsp</result>
  5.     <result
    name="error">/{1}/error.jsp</result>
  6.     <resultname="none">/{1}/none.jsp</result>
  7.     <resultname="login">/login/index.jsp</result>
  8.     <resultname="{2}">/{1}/{2}.jsp</result>
  9.     <resultname="forwardJsp">/{1}/${forwardJsp}</result>
  10.     <resultname="json"type
    ="json"></result>
  11. </action>

<result name="forwardJsp">/{1}/${forwardJsp}</result>    中的forwardJsp是哪來的? 請看下面的Action(部分)

類名是RegisterAction , 在spring的bean名稱是registerAction

Java程式碼  收藏程式碼
  1. protected String        forwardJsp          = "result.jsp"//自定義的轉發到result.jsp
  2. public String getForwardJsp()  
  3. {  
  4.     return forwardJsp;  
  5. }  
  6. -----------------  上面的程式碼 寫在ActionBase 類中-------------------  
  7. public String pre()  
  8. {  
  9.     //return INPUT;    //INPUT是Action介面中定義的  , 轉發到 /register/input.jsp
  10.     //return "pre" ;    //"pre"是方法名 , 轉發到 /register/pre.jsp
  11.     forwardJsp = "ss.jsp";  //自定義轉發到哪個JSP檔案
  12.     return"forwardJsp";  
  13. }  
 

請求 url 為: http://localhost/register_pre.action 時

會執行pre()方法

轉發到 /register/ss.jsp 檔案