Vita Rara: A Life Uncommon

Post and Redirect in Struts 2


Categories: | |

Redirecting a user of a web application after submitting an HTTP POST is a common pattern. A common use case is to redirect to a display page after a user has created something, such as a purchase order or sales order.

This post and redirect pattern is supported by Struts 2. The means of doing this wasn't obvious to me, but some reading and searching turned up the answer. There are some examples around for doing a redirect using static parameters, but that doesn't help when you want to redirect someone say to display the order they just placed on your website.

Redirects in Struts 2 can use static or dynamic parameters. Here's a simple static redirect:

<action name="createSalesOrderConfirmation" class="sales.CreateSalesOrderAction">
    <result name="redirect" type="redirect-action">
        <param name="actionName">displaySalesOrder</param>
        <param name="namespace">/order/sales</param>
    </result>
</action>

This is fairly straight forward. The type of the result is "redirect-action". You set parameters on the result using the param tags. In this case you set the actionName to displaySalesOrder, and the namespace to "/order/sales".

Dynamic Parameters for Redirects

Struts 2 supports passing dynamic parameters to a redirect, such as the id of a newly created entity, so you can display it.

The action-redirect result takes several parameters to help with this, which will not be parsed:

  • actionName
  • namespace
  • method
  • encode
  • parse
  • location
  • prependServletContext

To pass dynamic parameters to a redirect-action result you at a minimum you need to set "parse" to true. As follows:

<action name="createSalesOrderConfirmation" class="sales.CreateSalesOrderAction">
    <result name="redirect" type="redirect-action">
        <param name="actionName">displaySalesOrder</param>
        <param name="namespace">/order/sales</param>
        <param name="parse">true</param>
        <param name="id">${order.id}</param>
    </result>
</action>

In this redirect-action result we've added two parameters using param tags. The parse parameter tells Struts 2 to parse any additional parameters using the OGNL expression language. The "id" parameter tells Struts 2 to add this as a parameter to the next action by evaluating the OGNL statement against the current value stack.

In this case my current action, not the one I'm forward to, has an order property, which has an id property. This statement will add the value of this property to the redirect resulting in a forwarding URL like this:

http://www.mydomain.com/mycontext/order/sales/displaySalesOrder.action?id=123

You can set additional parameters on the redirect, and have them encoded using the "encode" parameter, or set the method using the "method" parameter.

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2
  • Allowed HTML tags: <a> <img> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h1> <h2> <h3>
  • Lines and paragraphs break automatically.
More information about formatting options