我看了下我以前的代码,以前用的是
[code="java"]RequestDispatcher rd = getServletContext().getRequestDispatcher("/seconddurl.do?m=index");
rd.forward(request,response);[/code]
ServletContext的getRequestDispatcher()其实和ServletRequest的getRequestDispatcher()有点区别的。
[code="java"]
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher.
Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at the specified path
See Also:
RequestDispatcher, getContext(java.lang.String)
[/code]
ServletRequest的相关API
[code="java"]
getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.
Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at the specified path
See Also:
RequestDispatcher, ServletContext.getRequestDispatcher(java.lang.String)
[/code]
[color=red]
The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.[/color]
所以request.getRequestDispatcher("index/indexWebAction!indexFrame.action").forward(request,response);
和
request.getRequestDispatcher("/index/indexWebAction!indexFrame.action").forward(request,response);
效果是一样的,因为request.getRequestDispatcher()这方法可以处理相对路径自动加上工程根路径,昨天我测试了下,确实是的,但是forward却失败了。
这点很吃惊,后来比较了下,以前项目用的是struts1,现在用的struts2。
查看web.xml发现:struts2有个
[code="xml"]
struts
org.apache.struts2.dispatcher.FilterDispatcher
[/code]
呵呵,着就难怪了。因为第一个filter执行后,没有进入struts2,所以没进action.
在servlet规范2.4以后,配置filter时有一个dispatcher的元素,修改配置如下
[code="java"]
publichome
/*
struts-cleanup
*.do
FORWARD
REQUEST
[/code]
提示:在不配置的情况下,默认是REQUEST,就是说只有REQUEST才会被STRUTS2的FILTER拦截,而FORWARD过来的请求是会被忽略的,这样我们的FORWARD当然不成功了!
关于这个属性你可以访问
[url]http://www.360doc.com/content/070903/11/39865_715414.html[/url]
改成上面的配置后,我再一测试,果然,FORWARD成功!原来是STRUTS2的问题,呵呵,估计你直接FORWARD一个HTML或者JSP肯定是可以的。