Struts 2 扩展名已更改,外部时重定向 link .action
Struts 2 extension changed, redirect when external link .action
使用以下 struts.xml 设置,将操作扩展名从 .action
更改为 .html
成功。
<constant name="struts.action.extension" value="html"/>
但是,来自 Google 搜索结果或其他外部链接的旧链接仍然指向 .action
url,点击时总是重定向到找不到页面错误。
无论如何,我可以将那些 .action
url 重定向到最新的 .html
链接吗?
这是将 .action url 替换为 .html 的拦截器代码。
不要忘记在 struts.xml 中声明添加的拦截器。
我希望它对其他正在寻找它的人有所帮助。 ;)
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
StringBuffer url = request.getRequestURL();
if(url != null && url.indexOf(".action") > 0) {
String fullUrl = url.toString().replace(".action", ".html");
String queryString = request.getQueryString();
fullUrl += (queryString == null ? "" : ("?" + queryString));
// return new url with .html
response.sendRedirect(fullUrl);
}
return actioninvocation.invoke();
}
使用以下 struts.xml 设置,将操作扩展名从 .action
更改为 .html
成功。
<constant name="struts.action.extension" value="html"/>
但是,来自 Google 搜索结果或其他外部链接的旧链接仍然指向 .action
url,点击时总是重定向到找不到页面错误。
无论如何,我可以将那些 .action
url 重定向到最新的 .html
链接吗?
这是将 .action url 替换为 .html 的拦截器代码。 不要忘记在 struts.xml 中声明添加的拦截器。 我希望它对其他正在寻找它的人有所帮助。 ;)
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
StringBuffer url = request.getRequestURL();
if(url != null && url.indexOf(".action") > 0) {
String fullUrl = url.toString().replace(".action", ".html");
String queryString = request.getQueryString();
fullUrl += (queryString == null ? "" : ("?" + queryString));
// return new url with .html
response.sendRedirect(fullUrl);
}
return actioninvocation.invoke();
}