SwiftMango
SwiftMango

Reputation: 15294

Cannot forward to JSF page using URL with context path

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)

Upvotes: 3

Views: 3773

Answers (1)

BalusC
BalusC

Reputation: 1108982

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:

Upvotes: 6

Related Questions