Pregunta

<h:panelGroup rendered="#{Account.Status eq 'FAILED' }">
            <h:outputText value="# {msg['account/failed']}" escape="false" />
</h:panelGroup>

<h:panelGroup rendered="#{Account.Status eq 'SUCCESS' }">
            <h:outputText value="# {msg['account/success']}" escape="false" />
</h:panelGroup>

...more panelgroup messages like this for all different Status types,
 and different messages to be shown..

On my glassfish Jee6 application, I show my messages depending of the status of the account, I keep my strings in a text file and reach from JSF page as above {msg['account/failed']}

Now these if-else Status conditions are getting more for different types of messages, and make the JSF page a mess, how can I do the same thing from the Java code?

I can set the value of the message and put the if else condition in my Java code but then how I will access to my text file where I keep my strings? or any other clean JSF way to do this?

¿Fue útil?

Solución

Assuming the account status is an enum, I'd publish the method

public static String toString(Enum<?> e) {
    ResourceBundle bundle = FacesContext.getCurrentInstance().getApplication().getResourceBundle("messages");
    return bundle.getString(e.getClass().getSimpleName() + "/" + e.name());
}

as EL-function, and use it like

#{e:toString(Account.status)}

(code untested as I don't have an eclipse at hand, but the general idea ought to be sound)

Otros consejos

Just make the enum value part of bundle key. E.g.

<c:set var="key" value="account.status.#{account.status}" />
<h:outputText value="#{msg[key]}" escape="false" />

(I only replaced / by . and lowercased the instance/property names conform standard conventions)

with

account.status.FAILED = Failed
account.status.SUCCESS = Success

Alternatively, make the key a property of the enum. See also among others:

you may want to use JSTL tags ...I worked with JSF and sometimes you have to try a few things. you can use ,

   <c:if test="condition">
   <h:outputText value="# {msg['account/success']}" escape="false" />
  </c:if>

or use

    <c:choose>
          <c:when test="${param.enter=='1'}">
  <h:outputText value="# {msg['account/success']}" escape="false" />
          </c:when>

          <c:otherwise>
         <h:outputText value="# {msg['account/failed']}" escape="false" />
          </c:otherwise>
    </c:choose>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top