Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Cannot navigate to an error page when ValidatorException is thrown  RSS feed

 
Ranch Hand
Posts: 1723
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I found an interesting example. When a string cannot be validated, the container does not navigate to the error page.






But the screen outputs the validation message "Name cannot contain underscores".
 
Saloon Keeper
Posts: 20505
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because ValidationExceptions are caught by the Controller and rendered as FacesMessages into the FacesContext messages list. They never make it up to the level where the container can catch them and use them for navigation purposes.

It is expected that validation error messages would either be displayed near the control containing invalid data or in a region of the View dedicated to message display. Invalid data is expected to be corrected and the form then resubmitted.
 
Himai Minh
Ranch Hand
Posts: 1723
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tim.
I tried this:

//In web.xml



But I still don't get the page navigation. I saw a pop up message from the browser telling me that I got a runtime exception.
 
Tim Holloway
Saloon Keeper
Posts: 20505
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FacesServlet ate it.

One thing I learned early about JSF is not to try and throw non-JSF exceptions and to be aware that JSF exceptions have specific JSF-internal targets. Besides what you've already tried, that means don't expect to get far throwing FacesException (it's the base class for more specific exceptions, including ValidatorException).

You really cannot force navigation off a form from anything but an action method or a link, and the action method doesn't need an exception to select a target View, since the target view name is what's returned by the action method.

JSF never intended the convertors, validators, listeners or mutators to navigate.
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

... But I still don't get the page navigation. I saw a pop up message from the browser telling me that I got a runtime exception.



It is possible that the JSF application is not able to locate the invalid.xhtml. Try to place the invalid.xhtml in the same folder as the index.xhtml file (the JSF page with command button). Then configure the error page as follows:

 
Tim Holloway
Saloon Keeper
Posts: 20505
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:
It is possible that the JSF application is not able to locate the invalid.xhtml. [/code]



That's not how it works. The <error-page> directive isn't handled by the application. It's handled by the server. It happens when a web application throws an exception but the application code does not catch it, so the server does. The server then looks for the error-page definition and renders it. If there is no error-page defined, then the server constructs an error page and returns a 503 response code.

But JSF was actually designed for more than just HTML and more than just web applications, despite the fact that actual usage has been almost exclusively in those environments. Because of that, exceptions are not allowed to be thrown out to the server, but are instead are caught by the JSF top-level Controller/dispatcher. Which for JSF web applications is the FacesServlet.

All JSF application code runs under the FacesServlet. Thus, any Exception thrown from JSF application code must percolate back up through the FacesServet, which, as I said earlier, catches and handles the exceptions itself. Even if it wanted to, the FacesServlet cannot route errors to the web.xml error-page, because J2EE applications cannot read the digested web.xml - it's an internal part of the server, not the application.

The only case where an error-page gets dispatched in a JSF app is if the FacesServlet itself throws an Exception, and it's not going to intentionally do that.
 
Himai Minh
Ranch Hand
Posts: 1723
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone. It works in this way based on the example in Core JSF book. Thanks for your help.

//In web.xml. The runtime exception is wrapped by ServletException and the container handles it.




invalid.xhtml
 
Tim Holloway
Saloon Keeper
Posts: 20505
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevertheless, I still don't recommend throwing yourself out of JSF in order to navigate JSF. There are simpler and cleaner ways.

Also be very careful when using web.xml to point to JSF View Templates. For some container-dispatched Views, the container may execute the target directly, bypassing the FacesServlet and thus losing JSF functionality. From my experience, this may vary depending on the brand and version of your container, but to be safe, I only make web.xml target HTML and JSP View definitions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!