Struts 2: return to calling page

I'm using Struts 2.

I'd like to return from an Action to the page which invoked it.

Say I'm in page x.jsp, I invoke Visual action to change CSS preferences in the session; I want to return to x.jsp rather than to a fixed page (i.e. home.jsp)

Here's the relevant struts.xml fragment:

   <result name="home">/pages/home.jsp</result>

Of course my VisualizationAction.execute() returns home.

Is there any "magic" constant (like, say, INPUT_PAGE) that I may return to do the trick?

Must I use a more involved method (i.e. extracting the request page and forwarding to it)?



You can use a dynamic result in struts.xml. For instance:

   <result name="next">${next}</result>

Then in your action, you create a field called next. So to invoke the action you will pass the name of the page that you want to forward to next. The action then returns "next" and struts will know which page to go to.

There is a nicer explanation on this post: Stack Overflow

return INPUT;

will do the trick. INPUT constant is defined in Action interface itself. It indicates that action needs more input.

By calling page if you meant the page that took you to the action input page, then your input page will have to store HTTP header "Referer" in the request scope for the Action.

My solution would involve one interface and one interceptor. You implement the following interface for all actions to which you are likely to want to redirect:

public interface TargetAware {
  public String getTarget();
  public void setTarget(String target);

The interceptor simply ensures that the target is set, if required:

public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {
   public String doIntercept(ActionInvocation invocation) {
      Object action = invocation.getAction();
      HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
      if (action instanceof TargetAware) {
         TargetAware targetAwareAction = (TargetAware) action;
         if (targetAwareAction.getTarget() == null)
      return invocation.invoke();

   // I'm sure we can find a better implementation of this...
   private static String getCurrentUri(HttpServletRequest request) {
      String uri = request.getRequestURI();
      String queryString = request.getQueryString();
      if (queryString != null && !queryString.equals(""))
         uri += "?" + queryString;
      return uri;

   public void init() { /* do nothing */ }
   public void destroy() { /* do nothing */ }

From then on, once these two bits are in place and your actions implement the TargetAware interface (if you expect to have to redirect to them), then you have access to a target parameter in your JSPs whenever you need it. Pass that parameter on to your VisualizationAction (which might as well implement also the TargetAware interface!), and on SUCCESS, redirect as explained by Vincent Ramdhanie:

<action name="Visual" class="it.___.web.actions.VisualizationAction">
   <result type="redirect">
      <param name="location">${target}</param>
      <param name="parse">true</param>

I did not try every single detail of this strategy. In particular, beware of the notation surrounding the redirect result type (depending on your specific version of Struts2: 2.0.x and 2.1.x may differ on this...).

I prefer the way when you navigating users by particular actions.

You could use some parameter as indicator, that you want to change current design: i.e.

So then, you write some struts 2 interceptor, which logic is to check the presence of such parameter 'changeDesign', and this interceptor will do nessesary work of changing design and will control workflow. With interceptor you decouple your actions from crosscutting logic.

ok, in your class it.___.web.actions.VisualizationAction, you must return a string value containing INPUT, then, on struts.xml you have to set something like this:

<action name="Visual" class="it.___.web.actions.VisualizationAction">
   <result name="input">yourJspPage.jsp</result>

this will lead you to the page you want. This should work, I've been working on struts2 along 2 months

Need Your Help

Database Publishing Using Web Deploy

sql-server visual-studio-2013

I've had success in using Web Deploy to publish my web page, however the database (schema and data) are not being published at all.