I am using JSF Myfaces Impl 1.2 without tomahawk and other libs :
I am using different styles + images to show JSF Error messages, find below a sample.
<h:panelGroup rendered="${adminBean.showErrorIcon==2}">
<table width="375" align="center" class="InfoMsg" border="1"
cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="375" align="center" class="InfoMsg" border="0">
<tr>
<td width="50"><img src="static/images/info_icon.gif"
width="40" height="40" border="0" /></td>
<td width="325" align="left"><h:messages layout="table"
errorClass="InfoMsg" /></td>
</tr>
</table>
</td>
</tr>
</table>
Based on the int variable of the Backing Bean , I am displaying a diff image and the corresponding FacesMessage(s) in the screen – only 2 cases – error or an information.
I am using the below code to set the variable of the Backing Bean
//Checking if there are messages!
log.debug("Checking if there are messages to be shown ]");
if(getShowErrorIcon()==99){//Set only if the value is still the default :
log.debug("getShowErrorIcon was DEFAULT - Changing it ]");
Iterator<FacesMessage> messages = FacesContext.getCurrentInstance().getMessages();
if(messages != null && getShowErrorIcon()==99){//Set Error/Info for messages that are not added here :
while(messages.hasNext()){
log.debug("There are ***messages***");
FacesMessage aMessage =(FacesMessage) messages.next();
if(aMessage.getSeverity().compareTo(FacesMessage.SEVERITY_ERROR)==0){
setShowErrorIcon(1);
break;//just once is enough
}
if(aMessage.getSeverity().compareTo(FacesMessage.SEVERITY_INFO)==0){
setShowErrorIcon(2);
break;
}
}
}
}//if it is not default, then something has been set already, why again?
Now the problem I have is , There are FacesMessage(s) that are added by the MyFacesImpl – like the required=true and the custom validator messages which are added during PROCESS_VALIDATION Phase, These are not shown in the screen since my integer variable of the Backing Bean is not set , and since the INVOKE_APPLICATION Phase was not called (and that means the above code was not called!!!)
How do I resolve this? Or Whats the best way / Where's the best place to place the above checking code ?
Appreciate your help.Thanks!
Best Answer
I'm not sure, but this all look like unnecessarily overcomplicated. To change icons/styles based on the message severity, just make use of the CSS powers. You can specify different CSS classes based on message severity using
infoClass
anderrorClass
attributes of the<h:messages>
and you can specify the icons as CSS background image.JSF:
CSS:
The
<h:messages layout="table">
itself already renders a HTML<table>
. I think the whole table around it is unnecessary as well. Just apply styles accordingly the usual CSS way.See also:
Update: as per the comments, you're looking for something like this:
with CSS:
This shows two separate message tables, one for
info
and other forerror
messages, each with a single icon on the left center.