mheppler9d
mheppler9d

Reputation: 169

PrettyFaces 404 page without URL redirect

In our app with PrettyFaces 2.0.12.Final, we have our redirects set up in pretty-config.xml.

<url-mapping id="foo">
    <pattern value="/foo/#{alias}" />
    <view-id value="/foo.xhtml" />
</url-mapping>

We have a custom 404 page set up in our web.xml.

<error-page>
    <error-code>404</error-code>
    <location>/404.xhtml</location>
</error-page>

When a user receives a 404 Not Found error, for a foo "alias" that doesn't exist, they're redirected to "/404.xhtml", and the browser URL bar does not retain the offending address.

Is there a way maintain the URL of "/foo/aliasdoesnotexist" in the browser URL bar and still display the 404 page?

Upvotes: 2

Views: 911

Answers (1)

Lincoln
Lincoln

Reputation: 3191

One way to handle this scenario is to handle the Exception in your application and perform an internal forward to the error page:

You can set up a PrettyFaces mapping for the 404 page:

<url-mapping id="foo">
    <pattern value="/foo/#{alias}" />
    <view-id value="/foo.xhtml" />
</url-mapping>

Then in your application code:

catch ( Exception e )
{
    String contextName = FacesContext.getCurrentInstance().getExternalContext().getContextName();
    FacesContext.getCurrentInstance().getExternalContext().dispatch(contextName + "/404");
}

If you want to catch the exception globally, you'll need to create a servlet filter to do this. See the following topic for how to create a filter that catches all exceptions:

how do I catch errors globally, log them and show user an error page in J2EE app

Hope this helps!

~Lincoln

Upvotes: 1

Related Questions