How do I check if an <s:message /> has been set in JSF?

[亡魂溺海] 提交于 2019-12-11 09:56:51

问题


I have the following two files (JSF 1.2) to build a form:

<!-- segment of form.xhtml -->
<s:decorate template="edit.xhtml">
    <h:inputText label="First Name" id="firstName" required="true" value="#{contactBean.firstName}">
        <f:validateLength minimum="1" maximum="25"/>
    </h:inputText>
</s:decorate>

<!-- segment of edit.xhtml -->
<s:validateAll>
    <ui:insert />
</s:validateAll>

<h:graphicImage value="/images/errorIcon.png" rendered="#{when we have a message for this input}" />
<s:message />

In edit.xhtml, is there an expression I use in the <h:graphicImage> rendered attribute?

I tried rendered="#{invalid}", but the invalid remains true even after the <s:messages /> are cleared. This results in the errorIcon.png being displayed without an associated <s:message />.

Let me know if I'm taking entirely the wrong approach.


回答1:


I'm not sure about the Seam part, I'll only suggest the generic JSF approach. I see basically two ways to achieve this requirement:

  1. Wrap FacesContext#getMessages(String clientId) in a bean property.

    public boolean hasMessages(String clientId) {
        return FacesContext.getCurrentInstance().getMessages(clientId).hasNext();
    }
    

    Use it as follows:

    <h:graphicImage rendered="#{bean.hasMessages('form:firstName')}" />
    

    (I assume that you're already using JBoss EL)

  2. Set the image as CSS background image instead (my preferred approach):

    <s:message errorClass="message error" />
    

    with

    .message.error {
        background: url('error.png') no-repeat left center;
        padding-left: 15px;
    }
    


来源:https://stackoverflow.com/questions/4442366/how-do-i-check-if-an-smessage-has-been-set-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!