近日突然发现struts1 的redirect有很大的限制,它只能redirect到APP domain下的某个URL,超出了domain的resource是无法访问的(如web server上的html)!
例如:
可以redirect 到以下URL:
但是无法redirect 到超出APP的URL:
具体见下面代码:
而且注意,如果不指定具体的action type,redirect是不起作用的!
例如下面的action例子,redirect不工作,因为没有指定具体的type:
要让redirect工作,应该像下面这样配置:
具体参见RequestProcessor的代码。
例如:
可以redirect 到以下URL:
http://cuishen.iteye.com/APP/test.jsp
但是无法redirect 到超出APP的URL:
http://cuishen.iteye.com/test.html
具体见下面代码:
@see org.apache.struts.action.RequestProcessor
/* 447*/ if(forward.getRedirect())
{
//斜杠开头的URL会自动加上APP名的
/* 449*/ if(uri.startsWith("/"))
/* 450*/ uri = request.getContextPath() + uri;
/* 452*/ response.sendRedirect(response.encodeRedirectURL(uri));
} else
{
/* 455*/ doForward(uri, request, response);
}
而且注意,如果不指定具体的action type,redirect是不起作用的!
例如下面的action例子,redirect不工作,因为没有指定具体的type:
<action path="/user/ErrorPage" forward="/WEB-INF/jsp/ServerError.jsp" redirect="true"/>
要让redirect工作,应该像下面这样配置:
<action path="/user/ErrorPage" type="xxx.xxx.XxxAction">
<forward name="success" path="/ServerError.jsp" redirect="true"/>
</action>
具体参见RequestProcessor的代码。
本文深入探讨了 Struts1 的 redirect 功能存在的局限性,即只能 redirect 到 APPdomain 下的 URL,无法访问超出 domain 资源。通过分析 RequestProcessor 代码,解释了这一行为背后的原理,并提供了配置示例来实现跨域 redirect 功能。此外,文章还强调了在不指定具体 actiontype 时,redirect 功能可能失效,并给出了正确的配置方式。
1473

被折叠的 条评论
为什么被折叠?



