• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to show error messages on the form that are coming from the constructors

 
Dhamayanthi Karuppanan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form which is bound to a managed bean (say:apply). In the bean constructor, Im talking to DB and getting some data to the form based on some conditions. The problem is, if i get into some error, im trying to show it in the form. I have add message to the form, in which case, the form is null. I hope ,as this is in constructor. Could any one please suggest me a way to do this. I hope i explained my problem in understandable way.

Appreciate your time and help.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work very well to do database operations in a JSF backing bean constructor, I'm sorry to say. For one thing, JSF backing beans are created using a no-arguments constructor, so if you want a professional-grade data object where the database parameters and search arguments aren't hard-coded into the bean, you're out of luck, since the property injectors don't get called uniitl AFTER the constructor has finished.

One solution is to use the @PostConstruct annotation, instead, although not all JSF implementations honor that.

For things like dropdown menus, however, I just defer the database operation until the first reference of the items that require database lookup. Like so:


An advantage of this scheme is that I can request a refresh of the list by simply setting "this.menuList" to null.
 
Dhamayanthi Karuppanan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand this. but still I have some doubts: I may not designed in great way.
The scenario is like this:
I get request parameter "uid" and I pass this DB to get whether this uid is a supervisor or not. and based on the result i show one link, else i will not show it.
the jsp page is below:

the corresponding managed bean for the above page:


The problem I explained the post is this. If i get some error from DB, the setErrorMessage will try to log in the facesmessage. But everything is in constructor, the form has not been created.

I realize I have not designed it in proper way. Any help.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Backing Bean (Model) is constructed by the JSF Controller as it builds the View, so yes, there is a "form" (View) being assembled. In JSF, the View controls the code, not the other way around.

However, like I said, database operations in constructors are awkward at best.

A more JSF-friendly way of doing what you want is to attach the operation to the actual item being controlled. Like so:

Back this view component with the following Model logic in myBean:


The caching of the userLevel variable (indicated by "securityChecked") is important, since getter methods are often called multiple times on a single page request, and at best, the multiple database hits would be a big (and unnecessary) slowdown.
 
Dhamayanthi Karuppanan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your kind reply.

I will do the coding in same way as you explained. Looks like long way to go for me in JSF.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic