简体   繁体   中英

Get a URL from an Action name: Struts 2

In struts 2 there is a struts tag where you can specify an action name and it gives you the url to that action:

<s:url action="action_name" />

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 ( org.apache.struts2.components.URL ) but can't figure out how to use it.

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.:

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(<with stringwriter>);

}

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

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 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.

Well this is the method in the component class inside struts2 which is creating 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 we can get various values for this

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

similar other values can be find out from ActionIvocation This is just an idea and i have not applied it myself.Hope it might help you.

Here is how I get the CURRENT ActionMapper rather than 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);
    }

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM