0
<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?

Spring
  • 11,333
  • 29
  • 116
  • 185

3 Answers3

2

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)

Community
  • 1
  • 1
meriton
  • 68,356
  • 14
  • 108
  • 175
  • tnx looks like this can help me, but since my JSF knowledge is very little I dont know how I can use this idea. This is for how to access external txt file only I guess? maybe you could edit question to explain a bit more how this looks in xhtml and java file? – Spring Jun 06 '13 at 18:00
  • and why use a el-function instead of calling that method like a normal method like #{bean.toString(Account.status)} ? – Spring Jun 07 '13 at 09:00
  • For brevity and readability of the el expression. Sure, you could write #{enumFormatter.toString(Account.status)}, but that's somewhat lengthy. – meriton Jun 07 '13 at 16:34
2

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
-1

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>
grepit
  • 21,260
  • 6
  • 105
  • 81
  • JSTL tags and JSF are a big no no! This can cause major headaches when mixed with facelets! – Jonathan S. Fisher Jun 06 '13 at 15:57
  • 2
    I respectfully disagree with you. There are circumstances that you may want to use JSTL with JSF given what you want to accomplish. – grepit Jun 06 '13 at 17:01
  • 2
    That may be so, but is this such a case? Not knowing whether the account status changes during the lifecycle of the view, the advice to use JSTL rather than the rendered attribute can be quite hazardous - and I don't see any compelling advantage JSTL can offer here? – meriton Jun 06 '13 at 18:52
  • @meriton I think your solution is definitely the best approach here but I just wanted to mention that too just in case to give him/her another option. – grepit Jun 06 '13 at 18:56
  • @user717630 JSTL should only be used to cut a component tree out of the view completely. There are a few cases where this may be a valid use, but the above is not one of them. If he has any sort of `f:ajax` in his view, JSTL will cause various problems. `` would be much more appropriate here. – Jonathan S. Fisher Jun 06 '13 at 20:16