• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I handle "lower level" exception in JSF?

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have come across some code where it attempts to save an entity to a database, but before it does it validates that the name of the entity is unique. If it is not unique it throws a runtime exception. This results in the ugly default exception web page being displayed. Is there any way to propagate this back to the JSF page where the user enters and clicks the form button to save the entity? The page already handles some error cases such as "field required" using the h:inputText's 'required' attribute. Need something more for name validation.

Thanks,
Alan
 
Ranch Hand
Posts: 109
1
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to do exactly the same in my project, see if a user already exists before persisting to the DB. What I've read online is that you shouldn't let it throw an exception and then handle it. You should ensure in the form that the credentials are unique.

So, you can have a method (in the form's backing bean) that checks the DB if the credentials already exists; if they exist, display a message to the user to change the credentials, if not persist the new user.
 
Alan Smith
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vasilis Souvatzis wrote:I wanted to do exactly the same in my project, see if a user already exists before persisting to the DB. What I've read online is that you shouldn't let it throw an exception and then handle it. You should ensure in the form that the credentials are unique.

So, you can have a method (in the form's backing bean) that checks the DB if the credentials already exists; if they exist, display a message to the user to change the credentials, if not persist the new user.



The exception is handled in such a way that it redirects to a generic "Application Exception" page, which links the user back to the home screen, so it isn't very intuitive (or graceful) as to what the problem was that caused it (a duplicate name). The validation for the entity is carried out in a remote EJB, so not sure how to link it back to the JSF page. The current mechanism seems to be designed with this in mind. Thanks for the comment anyway.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSF Validator mechanism isn't a full semantic validator, so it cannot handle things like the attempt to save a non-unique row to a database. For that you need action method code. That is, add code in your action method to check before attempting to save (or alternatively, detect and report save failures).

The easiest way to handle that is to either add an error message display element to your "save" webpage or to employ the JSF error message function to do the same. You simply set the message text to an appropriate message then return null from the action method. A null return keeps the action from navigating to a new web page. It simply redisplays the current page with the updated messaged text on it.

If you use JSF's built-in message manager, I recommend keeping the actual JSF-specific code outside of your backing bean. I generally have a special "JSFUtils" class I create so that instead of mucking around with JSF internals in my business logic I can simply invoke JSFUtils.addErrorMessage() and let it worry about the platform-specific stuff.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Smith wrote:I have come across some code where it attempts to save an entity to a database, but before it does it validates that the name of the entity is unique. If it is not unique it throws a runtime exception. This results in the ugly default exception web page being displayed. Is there any way to propagate this back to the JSF page where the user enters and clicks the form button to save the entity?


I'm certainly no JSF expert, but this strikes me as a basic "tell, dont ask" problem - ie, the page is treating this as a procedure, rather than a homogeneous action (or business process).

One possibility might be to change the way it presents the data and how the database stores it.

For example: You could present the user with a form that ONLY allows them to enter the "name", and then with a second one that allows them to either add or change other information based on whether that name exists. In the database, all you would then need is to add some sort of "status" column that says whether the 'entity' is "complete" or not: While you're in the process of entering data for the first time, it's "incomplete"; as soon as they've hit "save" it gets set to "complete" (although some might argue that a solution like that isn't fully normalized).

There are probably several ways to do it; but from what I've read, I'd say that:
(a) Your JSF page isn't treating this problem as a business process.
(b) A comprehensive solution is not going to be achieved by JSF alone.

But like I say, I'm no JSF expert... :wink:

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic