Note that there are some explanatory texts on larger screens.

plurals
  1. POGet a URL from an Action name: Struts 2
    text
    copied!<p>In struts 2 there is a struts tag where you can specify an action name and it gives you the url to that action:</p> <pre><code>&lt;s:url action="action_name" /&gt; </code></pre> <p>I've been looking for a while now to see if it is possible to do this in an Struts2 Action/Interceptor. I found the class that relates to this struts tag I think (<code>org.apache.struts2.components.URL</code>) but can't figure out how to use it.</p> <p>This is as far as I got but it might not be how to use it (if its possible at all) but any method I call after this just gives me NullPointerExceptions.:</p> <pre><code>public String intercept(ActionInvocation ai) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); URL url = new URL(ai.getStack(), request, response); url.setAction("login"); //e.g. url.start(&lt;with stringwriter&gt;); } </code></pre> <p>Hoping this can be done as it would save a lot of troube!</p> <p>Thanks.</p> <h2>EDIT</h2> <pre><code>URL url = new URL(invocation.getStack(), request, response); url.setActionMapper(new DefaultActionMapper()); String redirectUrl = url.getUrlProvider().determineActionURL("action_name", invocation.getProxy().getNamespace(), invocation.getProxy().getMethod(), request, response, request.getParameterMap(), "http", true, true, false, false); </code></pre> <p>This code does work and gives me a redirect URL but I was wondering if there was a way to get the CURRENT ActionMapper rather than create a new one. I've done a quick google but can't find anything.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload