java 从 Action 名称获取 URL:Struts 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7191144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a URL from an Action name: Struts 2
提问by noShowP
In struts 2 there is a struts tag where you can specify an action name and it gives you the url to that action:
在 struts 2 中有一个 struts 标签,您可以在其中指定一个动作名称,它为您提供该动作的网址:
<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.
我一直在寻找一段时间,看看是否可以在 Struts2 Action/Interceptor 中做到这一点。我找到了我认为与这个 struts 标签相关的类 ( org.apache.struts2.components.URL
) 但不知道如何使用它。
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.:
这是我得到的,但它可能不是如何使用它(如果可能的话)但是我在此之后调用的任何方法只会给我 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.
这段代码确实有效,并为我提供了一个重定向 URL,但我想知道是否有办法获取 CURRENT ActionMapper 而不是创建一个新的。我已经做了一个快速的谷歌,但找不到任何东西。
采纳答案by Umesh Awasthi
Well this is the method in the component class inside struts2 which is creating action URL
好吧,这是 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);
}
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.
类似的其他值可以从 ActionIvocation 中找到这只是一个想法,我自己没有应用它。希望它可以帮助你。
回答by Stephan
Here is how I get the CURRENT ActionMapper rather than create a new one:
下面是我如何获得 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);
}
// ...
}