Puneet Joshi
Puneet Joshi

Reputation: 91

How to pass the parameter from one action to other

hi I am new in Struts2 and want to send parameter from one action to other on redirection

My form is like

<s:form action="saveComment">
                            <s:push value="ai">
                                <s:hidden name="id"/>
                                <table cellpadding="5px">
                                    <tr><td><s:textarea name="description" rows="5" cols="60" theme="simple" />
                                        </td>
                                        <td> <s:submit type="image" src="images/sbt.gif"  >
                                             </s:submit>
                                        </td></tr>

                                </table>
                            </s:push>
                        </s:form>

and my struts.xml file is like

 <action name="saveComment" method="saveComment" class="com.weaverants.web.AIAction">

        <result name="success" type="redirect">
            <param name="actionName">displayAI</param>
            <param name="aiId">${aiId}</param>
        </result>

    </action>


   <action name="displayAI" method="displayAI"  class="com.weaverants.web.AIAction">
        <result name="success" >/display_ai.jsp</result>
    </action>

Upvotes: 3

Views: 17833

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

You are already passing the parameters in your saveComment all you need to declare result type as redirectAction specifying the action name as the redirectAction and any other needed parameters. like

<action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirectAction">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
         <param name="empty"></param>

      </result>
   </action> 

the redirected URL generated will be

 /genReport/generateReport.action?reportType=pie&width=100&height=100

On the other hand using result type as redirect means response is told to redirect the browser to the specified location (a new request from the client).In this case also action instance, action errors, field errors, etc that was just executed is lost and no longer available and only way to pass parameters is by URL or through session

Same code as stated for redirectAction can be used

difference between Redirect and actionRedirect is the the first one will use HttpServletResponse#sendRedirect(String) sendRedirect while the later one will be taken care by the struts2 framework by ActionMapper provided by the ActionMapperFactory as is much better to use than Redirect result.

For passing data beside the URL pattern for these result type you can use

  1. Session to persist data between Action redirect.
  2. ScopeInterceptor.

Upvotes: 3

coding_idiot
coding_idiot

Reputation: 13734

You can also try using the chain result instead of redirectAction .All the parameters from first action will be passed to the second action but you do need to have the getters and setters of parameters in the second action. It would look something like this:

<result type="chain">
<param name="actionName">home</param>
<param name="namespace">/secure</param>
</result>

Upvotes: 1

Related Questions