从 Action 名称获取 URL:Struts 2
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这是 struts2 中组件类中的方法,它正在创建操作 URL,
现在的问题是我们如何获取此类
似的其他值的各种值,可以从 ActionI Vocation 中找到
这只是一个想法,我自己还没有应用过。希望它可以帮助你。
Well this is the method in the component class inside struts2 which is creating action URL
now the question is how we can get various values for this
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.
以下是我获取当前 ActionMapper 而不是创建新的 ActionMapper 的方法:
Here is how I get the CURRENT ActionMapper rather than create a new one: