繁体   English   中英

从Action名称获取URL:Struts 2

[英]Get a URL from an Action name: Struts 2

在struts 2中有一个struts标记,您可以在其中指定一个操作名称,它会为您提供该操作的URL:

<s:url action="action_name" />

我一直在寻找一段时间来看看是否可以在Struts2 Action / Interceptor中执行此操作。 我发现与我认为的这个struts标签有关的类( org.apache.struts2.components.URL )但是无法弄清楚如何使用它。

这是我得到的,但它可能不是如何使用它(如果它可能的话)但我之后调用的任何方法只给我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>);

}

希望这可以做到,因为它可以节省很多麻烦!

谢谢。

编辑

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

这段代码确实有效并给了我一个重定向URL,但我想知道是否有办法获得CURRENT ActionMapper而不是创建一个新的。 我做了一个快速的谷歌,但找不到任何东西。

那么这就是struts2中创建动作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);
    }

现在的问题是我们如何为此获得各种价值

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

类似的其他值可以从ActionIvocation中找到这只是一个想法,我自己没有应用它。希望它可以帮助你。

这是我如何获得CURRENT ActionMapper而不是创建一个新的:

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

    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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