从 Action 名称获取 URL:Struts 2 [英] Get a URL from an Action name: Struts 2

查看:45
本文介绍了从 Action 名称获取 URL:Struts 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 struts 2 中有一个 struts 标签,您可以在其中指定一个动作名称,它为您提供该动作的网址:

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" />

我一直在寻找一段时间,看看是否可以在 Struts2 操作/拦截器中执行此操作.我找到了与这个 struts 标签相关的类(org.apache.struts2.components.URL),但不知道如何使用它.

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.

这是我得到的,但它可能不是如何使用它(如果可能的话)但是我在此之后调用的任何方法只会给我 NullPointerExceptions.:

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!

谢谢.

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 而不是创建一个新的.我已经做了一个快速的谷歌,但找不到任何东西.

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.

推荐答案

嗯,这是struts2内部组件类中创建action URL的方法

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

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

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.

这篇关于从 Action 名称获取 URL:Struts 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆