Java 如何在 Struts 2 中进行动态 URL 重定向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/173846/
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
How to do dynamic URL redirects in Struts 2?
提问by Sietse
I'm trying to have my Struts2 app redirect to a generated URL. In this case, I want the URL to use the current date, or a date I looked up in a database. So /section/document
becomes /section/document/2008-10-06
我正在尝试将我的 Struts2 应用程序重定向到生成的 URL。在这种情况下,我希望 URL 使用当前日期,或者我在数据库中查找的日期。于是/section/document
变成/section/document/2008-10-06
What's the best way to do this?
做到这一点的最佳方法是什么?
采纳答案by Johnny Wey
Here's how we do it:
这是我们如何做到的:
In Struts.xml, have a dynamic result such as:
在 Struts.xml 中,有一个动态结果,例如:
<result name="redirect" type="redirect">${url}</result>
In the action:
在行动中:
private String url;
public String getUrl()
{
return url;
}
public String execute()
{
[other stuff to setup your date]
url = "/section/document" + date;
return "redirect";
}
You can actually use this same technology to set dynamic values for any variable in your struts.xml using OGNL. We've created all sorts of dynamic results including stuff like RESTful links. Cool stuff.
实际上,您可以使用相同的技术使用 OGNL 为 struts.xml 中的任何变量设置动态值。我们创建了各种动态结果,包括 RESTful 链接之类的东西。很酷的东西。
回答by Sietse
I ended up subclassing Struts' ServletRedirectResult
and overriding it's doExecute()
method to do my logic before calling super.doExecute()
. it looks like this:
我最终继承了 Struts'ServletRedirectResult
并doExecute()
在调用super.doExecute()
. 它看起来像这样:
public class AppendRedirectionResult extends ServletRedirectResult {
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
@Override
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
String date = df.format(new Date());
String loc = "/section/document/"+date;
super.doExecute(loc, invocation);
}
}
I'm not sure if this is the best way to do it, but it works.
我不确定这是否是最好的方法,但它有效。
回答by Ivan Morales
One can also use annotations
and the Convention plug-in to avoid repetitive configuration in struts.xml:
还可以使用annotations
Convention插件来避免struts.xml中的重复配置:
@Result(location="${url}", type="redirect")
The ${url} means "use the value of the getUrl method"
${url} 表示“使用 getUrl 方法的值”
回答by hari
If anyone wants to redirect directly in ActionClass
:
如果有人想直接重定向到ActionClass
:
public class RedirecActionExample extends ActionSupport {
HttpServletResponse response=(HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
url="http://localhost:8080/SpRoom-1.0-SNAPSHOT/"+date;
response.sendRedirect(url);
return super.execute();
}
Edit: Added a missing quote.
编辑:添加了一个缺失的报价。
回答by tiwari.vikash
You can redirect to another action using annotation -
您可以使用注释重定向到另一个操作 -
@Result(
name = "resultName",
type = "redirectAction",
params = { "actionName", "XYZAction" }
)
回答by Aaron
One can redirect directly from an interceptor without regard to which action is involved.
可以直接从拦截器重定向,而无需考虑涉及哪个操作。
In struts.xml
在 struts.xml 中
<global-results>
<result name="redir" type="redirect">${#request.redirUrl}</result>
</global-results>
In Interceptor
在拦截器中
@Override
public String intercept(ActionInvocation ai) throws Exception
{
final ActionContext context = ai.getInvocationContext();
HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);
request.setAttribute("redirUrl", "http://the.new.target.org");
return "redir";
}