본문 바로가기

카테고리 없음

Struts 2 Redirect

Struts 2 Redirect Action-Result

from: http://struts.apache.org/2.x/docs/redirect-action-result.html

This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the ServletRedirectResult because it does not require you to encode the URL patterns processed by the ActionMapper in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

Parameters

  • actionName (default) - the name of the action that will be redirect to
  • namespace - used to determine which namespace the action is in that we're redirecting to . If namespace is null, this defaults to the current namespace


struts.xml

<package name="public" extends="struts-default">
    <action name="login" class="...">
        <!-- Redirect to another namespace -->
        <result type="redirect-action">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>
<package name="secure" extends="struts-default" namespace="/secure">
    <-- Redirect to an action in the same namespace -->
    <action name="dashboard" class="...">
<!--액션이 실행된 후, 아래의 페이지로 포워딩된다-->


        <result>dashboard.jsp</result>
<!--액션이 실행되고 execute()가 ERROR를 리턴하면 다른 액션으로 Redirect 된다-->
        <result name="error" type="redirect-action">error</result>
    </action>
    <action name="error" class="...">
        <result>error.jsp</result>
    </action>
</package>
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.action?reportType=pie&width=100&height=100
   -->

   <action name="gatherReportInfo" class="...">
<!-- execute()에서 showReportResult가 리턴되면 다음 내용으로 Redirect된다 -->
      <result name="showReportResult" type="redirect-action">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
<!--Redirect하면서 파라미터를 전달하면 request.getParameter()으로 받을 수 있다-->
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
      </result>
   </action>
</package>