3

I was trying to forward a request to a JSF page:

request.getRequestDispatcher(redirectURI).forward(request, response);

I have a proc.xhtml under pages.

If I set:

redirectURI = "pages/proc.xhtml";

and it works fine.

However if I use the absolute URL including the context path:

redirectURI = "/context/pages/proc.xhtml";

It does not work and give me this exception:

com.sun.faces.context.FacesFileNotFoundException: /context/pages/proc.xhtml Not Found in ExternalContext as a Resource.

(and yes I set the Faces servlet URL pattern to be *.xhtml already)

SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Have you tried `redirectURI=/pages/proc.jsf` What is the output you get for this? – Vikas V Jul 23 '13 at 06:40
  • @VikasV I set `redirectURI=/pages/proc.xhtml` and it works... Not sure why adding the context path in the beginning will not work because that is more intuitive and conforming to URL rules... – SwiftMango Jul 23 '13 at 06:44
  • Perhaps you should edit your question and put a `/` in `redirectURI = "pages/proc.xhtml"`. It confuses. – Vikas V Jul 23 '13 at 06:47
  • @VikasV both `pages/proc.xhtml` and `/pages/proc.xhtml` works. It just does not work when I use `/context/pages/proc.xhtml`. – SwiftMango Jul 23 '13 at 06:55

1 Answers1

6

The RequestDispatcher#forward() takes a path relative to the context root. So, essentially you're trying to forward to /context/context/pages/proc.xhtml which obviously doesn't exist. You need /pages/proc.xhtml if you want to make it absolutely relative to the context root instead of to the current request URI.

redirectURI = "/pages/proc.xhtml";

Or, as the in this context strange variable name redirectURI indicates, if you actually intend to fire a real redirect (and thus reflect the URL change in the browser's address bar), then you should be using HttpServletResponse#sendRedirect() instead which indeed takes a path relative to the current request URI (and thus you should include the context path when you want to start with /).

redirectURI = request.getContextPath() + "/pages/proc.xhtml";
response.sendRedirect(redirectURI);

Otherwise better rename that variable to forwardURI or so.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555