File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes customizing messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "customizing messages" Watch "customizing messages" New topic

customizing messages

Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 199
I know that there are a number of tutorials on how to customize or internationalize error (or other kind) messages. But if you have to really customize them then there is (AFAIK) no way of doing it. Or is there?

-new JSF 2.0 project
-currently using MyFaces but will change it to some other implementation if it solves this
-trying to make a template to all the pages
-the UI is predefined and I am not allowed to make any changes to it

Specifically the error messages need to go to the bottom of the page in a area that is only visible if there are error messages. How do I accomplish that? The only tags that I know that handle the error messages are h:message and h:messages. The h:message is not suitable since using it requires me knowing the field ids of the page (for the "for" attribute) and since I am programming a general solution (to all pages) this is not possible. The h:messages may be the solution since it just might yield to my needs. The tag produces either a html list or a html table and I can use CSS to modify those to look like what i want. But the "error message" area itself should not be visible if there are no messages so how do I know that? Is there some (JSF standard defined or some specific implementation defined) way of knowing if there are errors on page? Or messages at all for that matter? The optimal thing would be if I could access the error messages directly and display them at which ever way I wanted but the minimal thing for this to work I need at least to know if there are messages or not. The only way I can think of getting this to work is to make the area on every single page but as a hidden div and using JavaScript I can then make it visible if there is something to show there. But this is such a lame hack that I do not want to go there.

And that is not all. I also have to display messages (error or success messages) originating from the receiving side of the code in the "error message" area or "success message" area. Is there a way to make error/success messages in code that the h:messages tag takes care of? If not, then I have to make that messaging system as a custom solution in addition to the normal validating procedure.

If all else fails I can make the whole procedure (validating input and messages coming from code) a custom solution that does not use the normal JSF validation at all. But since I am at the beginning of this project I need to know for sure that that is the only way to make this work. In the future I am making more projects using JSF (and this predefened UI I just described) and it would be so awesome to get a working solution now than to have to fight with this thing endless amount of days in the future.
Carlos Jorge Tavares Ferreira

Joined: Dec 10, 2007
Posts: 17

You can iterate FacesMessages via FacesContext getMessages() method. And you can know the maximum severity by using the getMaximumSeverity() method.

Then it's easy to create a custom component to display the messages the way you want.

Hope this is helpful.

Kind regards,

Carlos Ferreira
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 199
Thank you!
I am a new JSF user and it shows. Of course the most obvious place to look for an answer is the main object of the whole thing...

The getMessageList() method seems to be right one to use with EL and even addMessage() method is available and so on...
Don't get me started about those stupid light bulbs.
subject: customizing messages