简体   繁体   中英

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

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.

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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM