Get url from action name: Struts 2

In struts 2 there is a struts tag where you can specify the name of the action, and it gives a URL to this action:

<s:url action="action_name" /> 

I searched for a while to find out if this is possible in the Struts2 Action / Interceptor. I found a class that refers to this struts tag, I think ( org.apache.struts2.components.URL ), but cannot figure out how to use it.

This is, as I understand it, but it may not be how to use it (if at all possible), but any method that I call after that just gives me NullPointerExceptions .:

 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"); //eg url.start(<with stringwriter>); } 

Hoping this can be done, as it would save a lot of pipe!

Thanks.

EDIT

 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); 

This code really works and gives me the redirect URL, but I was wondering if there is a way to get the CURRENT ActionMapper instead of creating a new one. I did a quick google but can't find anything.

+4
source share
2 answers

Well, this is a method in the component class inside struts2 that creates the action url

 protected String determineActionURL(String action, String namespace, String method, HttpServletRequest req, HttpServletResponse res, Map parameters, String scheme, boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort, boolean escapeAmp) { String finalAction = findString(action); String finalMethod = method == null ? null : findString(method); String finalNamespace = determineNamespace(namespace, getStack(), req); ActionMapping mapping = new ActionMapping(finalAction, finalNamespace, finalMethod, parameters); String uri = actionMapper.getUriFromActionMapping(mapping); return UrlHelper.buildUrl(uri, req, res, parameters, scheme, includeContext, encodeResult, forceAddSchemeHostAndPort, escapeAmp); } 

Now the question is, how can we get different values โ€‹โ€‹for this

 action=invocation.getAction(); namespace=invocation.getProxy().getNamespace(); methos= invocation.getProxy().getMethod(); 

similar other values โ€‹โ€‹can be learned from ActionIvocation. This is just an idea, and I did not apply it myself. Hope this helps you.

+4
source

This is how I get CURRENT ActionMapper and not create a new one:

 import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.dispatcher.mapper.ActionMapper; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.inject.Inject; @Namespace("MyNamespace") public class MyAction extends ActionSupport { private ActionMapper actionMapper; private UrlHelper urlHelper; @Inject public void setActionMapper(ActionMapper mapper) { this.actionMapper = mapper; } @Inject public void setUrlHelper(UrlHelper urlHelper) { this.urlHelper = urlHelper; } private String getAbsoluteUrl(String actionName, String namespace) { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ActionContext context = ActionContext.getContext(); ActionInvocation invocation = context.getActionInvocation(); URL url = new URL(invocation.getStack(), request, response); url.setActionMapper(actionMapper); url.setUrlHelper(urlHelper); return url.getUrlProvider().determineActionURL( // actionName, // namespace, // "" , /* Method name */ request, response, // request.getParameterMap(), "http", // true, true, true, false); } // ... } 
+3
source

Source: https://habr.com/ru/post/1368958/


All Articles