显示JSF错误消息 [英] Showing the JSF Error Messages
问题描述
我使用的JSF Myfaces Impl 1.2没有tomahawk和其他libs:
I am using JSF Myfaces Impl 1.2 without tomahawk and other libs :
我使用不同的样式+图像显示JSF错误消息,下面的示例。 / p>
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>
基于int变量我在显示一个差异图像和相应的FacesMessage(s)在屏幕中 - 只有2个例子 - 错误或信息。
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.
我使用下面的设置Backing Bean变量的代码
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?
现在我遇到的问题是,有由FacesMessage(s)添加的MyFacesImpl required = true和在PROCESS_VALIDATION阶段期间添加的自定义验证器消息,这些不会显示在屏幕中,因为我没有设置Backing Bean的整数变量,并且因为INVOKE_APPLICATION阶段没有被调用(这意味着上面的代码未调用!!!)
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!!!)
如何解决这个问题?
欣赏你的帮助。谢谢!
推荐答案
我不知道,但这一切看起来像不必要的过于复杂。要根据邮件严重性更改图标/样式,只需使用CSS权限。您可以根据消息的严重性使用<
,您可以将图标指定为CSS背景图片。 infoClass
和 errorClass
; h:messages>
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
and errorClass
attributes of the <h:messages>
and you can specify the icons as CSS background image.
JSF:
<h:messages id="messages" layout="table" infoClass="info" errorClass="error" />
CSS:
#messages .info td {
background: url('info.gif') no-repeat left center;
padding-left: 15px;
}
#messages .error td {
background: url('error.gif') no-repeat left center;
padding-left: 15px;
}
< h:messages layout = >
本身已经呈现了一个HTML < table>
。我认为整个桌子周围是不必要的。只需按照通常的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.
#messages {
width: 375px;
margin: 0 auto;
border: 1px black solid;
border-collapse: collapse;
}
另请参阅:
- W3schools CSS教程/参考
- CSStutorial.net CSS教程
- W3schools CSS tutorial/reference
- CSStutorial.net CSS tutorial
See also:
更新:根据评论,您正在寻找这样的:
Update: as per the comments, you're looking for something like this:
<h:messages layout="table" styleClass="messages info" infoClass="info" errorClass="error" />
<h:messages layout="table" styleClass="messages error" infoClass="info" errorClass="error" />
使用CSS:
.messages {
width: 375px;
margin: 0 auto;
border-collapse: collapse;
border: 1px black solid;
}
.messages.info {
background: url('info.gif') no-repeat left center;
}
.messages.error {
background: url('error.gif') no-repeat left center;
}
.messages.info tr.error, .messages.error tr.info {
display: none;
}
.messages td {
padding-left: 15px;
}
这显示两个单独的消息表,一个用于 info
以及其他错误
讯息,每个讯息在左侧中央有一个图示。
This shows two separate message tables, one for info
and other for error
messages, each with a single icon on the left center.
这篇关于显示JSF错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!