我试图将请求转发到JSF页面:
request.getRequestDispatcher(redirectURI).forward(request, response);
Run Code Online (Sandbox Code Playgroud)
我有一个proc.xhtml下pages.
如果我设置:
redirectURI = "pages/proc.xhtml";
Run Code Online (Sandbox Code Playgroud)
它工作正常.
但是,如果我使用包含上下文路径的绝对URL:
redirectURI = "/context/pages/proc.xhtml";
Run Code Online (Sandbox Code Playgroud)
它不起作用,并给我这个例外:
com.sun.faces.context.FacesFileNotFoundException: /context/pages/proc.xhtml Not Found in ExternalContext as a Resource.
Run Code Online (Sandbox Code Playgroud)
(是的,我将Faces servlet URL模式设置为*.xhtml)
的RequestDispatcher#forward()需要相对于上下文根的路径.所以,基本上你正试图转向/context/context/pages/proc.xhtml显然不存在的东西.你需要/pages/proc.xhtml,如果你想让它绝对相对于上下文根,而不是当前请求的URI.
redirectURI = "/pages/proc.xhtml";
Run Code Online (Sandbox Code Playgroud)
或者,正如在此上下文中的奇怪变量名称redirectURI所示,如果您实际上打算触发一个真正的重定向(从而反映浏览器地址栏中的URL更改),那么您应该使用HttpServletResponse#sendRedirect()而不是确实采用相对于当前的路径请求URI(因此,您应该在开始时包含上下文路径/).
redirectURI = request.getContextPath() + "/pages/proc.xhtml";
response.sendRedirect(redirectURI);
Run Code Online (Sandbox Code Playgroud)
否则最好将该变量重命名为forwardURI左右.