1. 程式人生 > >Struts2理解——轉發和重定向

Struts2理解——轉發和重定向

 轉發和重定向設定:         <action name="deptAction" class="com.syaccp.erp.action.DeptAction">             <result name="success">/WEB-INF/jsp/basic/dept_list.jsp</result>             <result name="editView">/WEB-INF/jsp/basic/dept_edit.jsp</result> </action>      上例action中,success對應的檢視是通過預設的轉發(dispatch)跳轉的。editView作為增刪改的一部分,應該通過重定向來跳轉頁面,這樣必須顯式宣告type=redirect,來達到重定向的效果。這時editView的內容改為action中一個方法更合適。如:
<action name="deptAction" class="com.syaccp.erp.action.DeptAction">             <result name="success">/WEB-INF/jsp/basic/dept_list.jsp</result>             <result name="editView" type="redirect">deptAction!select.action</result> </action>   這裡在執行edit方法後返回editView字串,將會再執行select方法,跟DeptEditServlet裡response.sendRedirect("DeptListServlet")類似
上例只是重定向同一個Action類中的其他方法,開發中可能還需要重定向到其他Action類中,這時就需要用到type屬性的另一個值:redirectAction:  <action name="deptAction" class="com.syaccp.erp.action.DeptAction">             <result name="success">/WEB-INF/jsp/basic/dept_list.jsp</result>             <result name="editView" type="redirect"
>
deptAction!select.action</result>
<result name="index" type="redirectAction">indexAction.action</result> </action>  上例中,如果deptAction中某個方法返回字串為index,則將跳轉到indexAction去,執行indexAction的execute方法。 如果indexAction在其他包裡面,則前面應加上包名,例:index/indexAction