我试图将请求转发到 JSF 页面:
request.getRequestDispatcher(redirectURI).forward(request, response);
我在 pages
下有一个 proc.xhtml
。
如果我设置:
redirectURI = "pages/proc.xhtml";
而且效果很好。
但是,如果我使用包含上下文路径的绝对 URL:
redirectURI = "/context/pages/proc.xhtml";
它不起作用并给我这个异常(exception):
com.sun.faces.context.FacesFileNotFoundException: /context/pages/proc.xhtml Not Found in ExternalContext as a Resource.
(是的,我已经将 Faces servlet URL 模式设置为 *.xhtml
)
最佳答案
RequestDispatcher#forward()
采用相对于上下文根的路径。因此,本质上,您是在尝试转发到显然不存在的 /context/context/pages/proc.xhtml
。如果你想让它绝对相对于上下文根而不是当前请求 URI,你需要 /pages/proc.xhtml
。
redirectURI = "/pages/proc.xhtml";
或者,正如在此上下文中奇怪的变量名 redirectURI
所指示的,如果您实际上打算触发一个真正的重定向(并因此反射(reflect)浏览器地址栏中的 URL 更改),那么您应该使用 HttpServletResponse#sendRedirect()
相反,它确实采用了相对于当前请求 URI 的路径(因此,当您想以 /
开头时,您应该包含上下文路径)。
redirectURI = request.getContextPath() + "/pages/proc.xhtml";
response.sendRedirect(redirectURI);
否则最好将该变量重命名为 forwardURI
左右。
另见:
关于jsf - 无法使用带有上下文路径的 URL 转发到 JSF 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803012/