从 Action 名称获取 URL:Struts 2

发布于 2024-12-01 06:57:16 字数 1222 浏览 0 评论 0原文

在 struts 2 中,有一个 struts 标签,您可以在其中指定操作名称,并为您提供该操作的 url:

<s:url action="action_name" />

我已经寻找了一段时间,看看是否可以在 Struts2 操作/拦截器中执行此操作。我找到了与这个 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,但我想知道是否有一种方法可以获取当前的 ActionMapper,而不是创建一个新的。我快速谷歌了一下,但找不到任何东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2024-12-08 06:57:16

嗯,这是 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();

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

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.

暗地喜欢 2024-12-08 06:57:16

以下是我获取当前 ActionMapper 而不是创建新的 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);
    }

    // ...
}

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

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文