显示JSF错误消息

gek*_*ish 4 css jsf

我正在使用没有tomahawk和其他库的JSF Myfaces Impl 1.2:

我使用不同的样式+图像来显示JSF错误消息,在下面找到一个示例.

<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>
Run Code Online (Sandbox Code Playgroud)

基于Backing Bean的int变量,我在屏幕上显示差异图像和相应的FacesMessage - 只有2个案例 - 错误或信息.

我使用下面的代码来设置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?
Run Code Online (Sandbox Code Playgroud)

现在我遇到的问题是,MyFacesImpl添加了FacesMessage - 比如required = true和在PROCESS_VALIDATION阶段添加的自定义验证器消息,这些都没有显示在屏幕上,因为我的整数变量是没有设置Backing Bean,因为没有调用INVOKE_APPLICATION阶段(这意味着上面的代码没有被调用!!!)

我该如何解决这个问题?或者什么是最好的方式/哪里是放置上述检查代码的最佳位置?

感谢你的帮助.谢谢!

Bal*_*usC 11

我不确定,但这看起来都不必要地过于复杂.要根据消息严重性更改图标/样式,只需使用CSS权限即可.您可以根据消息严重性使用infoClasserrorClass属性指定不同的CSS类,<h:messages>并可以将图标指定为CSS背景图像.

JSF:

<h:messages id="messages" layout="table" infoClass="info" errorClass="error" />
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

<h:messages layout="table">本身已经呈现了一个HTML <table>.我认为围绕它的整个表格也是不必要的.只需按照常用的CSS方式应用样式.

#messages {
    width: 375px;
    margin: 0 auto;
    border: 1px black solid;
    border-collapse: collapse;
}
Run Code Online (Sandbox Code Playgroud)

也可以看看:


更新:根据评论,你正在寻找这样的东西:

<h:messages layout="table" styleClass="messages info" infoClass="info" errorClass="error" />
<h:messages layout="table" styleClass="messages error" infoClass="info" errorClass="error" />
Run Code Online (Sandbox Code Playgroud)

用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;
}
Run Code Online (Sandbox Code Playgroud)

这显示了两个单独的消息表,一个用于消息,info另一个用于error消息,每个消息表在左中心都有一个图标.