<p>I'm using <b>Struts 2</b>.</p>

<p>I'd like to return from an Action to the page which invoked it.</p>

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

<p>Here's the relevant <strong>struts.xml</strong> fragment:
<br/></p>

<pre>
&lt;action
   name="Visual"
   class="it.___.web.actions.VisualizationAction"&gt;
   &lt;result name="home"&gt;/pages/home.jsp&lt;/result&gt;
&lt;/action&gt;
</pre>

<p>Of course my <code>VisualizationAction.execute()</code> returns <strong>home</strong>.</p>

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

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

<p>T.I.A.</p>

[{'answer_id': 45595, 'author': 'nikhilbelsare', 'author_id': 4705, 'author_profile': 'https://Stackoverflow.com/users/4705', 'pm_score': 1, 'selected': False, 'text': '<pre><code>return INPUT;\n</code></pre>\n\n<p>will do the trick. INPUT constant is defined in Action interface itself. It indicates that action needs more input.</p>\n\n<p>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.</p>\n'}, {'answer_id': 202693, 'author': 'Vincent Ramdhanie', 'author_id': 27439, 'author_profile': 'https://Stackoverflow.com/users/27439', 'pm_score': 3, 'selected': True, 'text': '<p>You can use  a dynamic result in struts.xml. For instance:</p>\n\n<pre><code>&lt;action\n   name="Visual"\n   class="it.___.web.actions.VisualizationAction"&gt;\n   &lt;result name="next"&gt;${next}&lt;/result&gt;\n&lt;/action&gt;\n</code></pre>\n\n<p>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.</p>\n\n<p>There is a nicer explanation on this post: <a href="https://stackoverflow.com/questions/173846/struts2-how-to-do-dynamic-url-redirects">Stack Overflow</a></p>\n'}, {'answer_id': 229951, 'author': 'Pavel Rodionov', 'author_id': 29487, 'author_profile': 'https://Stackoverflow.com/users/29487', 'pm_score': 0, 'selected': False, 'text': '<p>I prefer the way when you navigating users by particular actions. </p>\n\n<p><a href="http://domain.com/myAction.action" rel="nofollow noreferrer">http://domain.com/myAction.action</a></p>\n\n<p>You could use some parameter as indicator, that you want to change current design:\ni.e.</p>\n\n<p><a href="http://domain.com/myAction.action?changeDesign=silver_theme" rel="nofollow noreferrer">http://domain.com/myAction.action?changeDesign=silver_theme</a></p>\n\n<p>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.</p>\n'}, {'answer_id': 447845, 'author': 'pierdeux', 'author_id': 53747, 'author_profile': 'https://Stackoverflow.com/users/53747', 'pm_score': 1, 'selected': False, 'text': '<p>My solution would involve one interface and one interceptor.  You implement the following interface for all actions <strong>to which</strong> you are likely to want to redirect:</p>\n\n<pre><code>public interface TargetAware {\n  public String getTarget();\n  public void setTarget(String target);\n}\n</code></pre>\n\n<p>The interceptor simply ensures that the target is set, if required:</p>\n\n<pre><code>public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {\n   public String doIntercept(ActionInvocation invocation) {\n      Object action = invocation.getAction();\n      HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);\n      if (action instanceof TargetAware) {\n         TargetAware targetAwareAction = (TargetAware) action;\n         if (targetAwareAction.getTarget() == null)\n            targetAwareAction.setTarget(getCurrentUri(request));\n      }     \n      return invocation.invoke();\n   }\n\n   // I\'m sure we can find a better implementation of this...\n   private static String getCurrentUri(HttpServletRequest request) {\n      String uri = request.getRequestURI();\n      String queryString = request.getQueryString();\n      if (queryString != null &amp;&amp; !queryString.equals(""))\n         uri += "?" + queryString;\n      return uri;\n   }\n\n   public void init() { /* do nothing */ }\n   public void destroy() { /* do nothing */ }\n}\n</code></pre>\n\n<p>From then on, once these two bits are in place and your actions implement the <code>TargetAware</code> interface (if you expect to have to redirect to them), then you have access to a <code>target</code> parameter in your JSPs whenever you need it.  Pass that parameter on to your <code>VisualizationAction</code> (which might as well implement also the <code>TargetAware</code> interface!), and on <code>SUCCESS</code>, redirect as explained by Vincent Ramdhanie:</p>\n\n<pre><code>&lt;action name="Visual" class="it.___.web.actions.VisualizationAction"&gt;\n   &lt;result type="redirect"&gt;\n      &lt;param name="location"&gt;${target}&lt;/param&gt;\n      &lt;param name="parse"&gt;true&lt;/param&gt;\n   &lt;/result&gt;\n&lt;/action&gt;\n</code></pre>\n\n<p>I did not try every single detail of this strategy.  In particular, beware of the notation surrounding the <code>redirect</code> result type (depending on your specific version of Struts2: 2.0.x and 2.1.x may differ on this...).</p>\n'}, {'answer_id': 447914, 'author': 'JuanDeLosMuertos', 'author_id': 39339, 'author_profile': 'https://Stackoverflow.com/users/39339', 'pm_score': 0, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>&lt;action name="Visual" class="it.___.web.actions.VisualizationAction"&gt;\n   &lt;result name="input"&gt;yourJspPage.jsp&lt;/result&gt;\n&lt;/action&gt;\n</code></pre>\n\n<p>this will lead you to the page you want.\nThis should work, I\'ve been working on struts2 along 2 months</p>\n'}]
['https://Stackoverflow.com/questions/45424', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4690/']