JSF JSR portlet - calling JSF bean attribute within a JSTL tag

Recently I have expericened that when you call a JSF bean attribute from within a JSTL tag specially inside a conditional tag like <c:when... > , doesn't work at first and will not show you updated value of a bean attribute.
When you do a page refresh then jsf bean attribute updated value will be shown when invoked inside a jstl tag.

So be cautious when calling jsf bean attribute tag from within a jstl tag.

Let me explain in here with an example.

When jsf page renders, jsf backing bean getters are called. Now if any of the getter are being called from a jstl tag, then this getter will not give you updated value of bean attribute.

if you have code like this
<c:when test="${customerBean.fname}">, in this case, when your page renders, this customerBean.fname will not give you the udpated value and when you do a page refresh then this getter will display the updated value of fname.

<c:choose>
<c:when test="${customerBean.fname=='Neeraj'}">
   <c:out value="hello"/>
</c:when>
<c:otherwise>
   <c:out value="Bye"/>
</c:otherwise>
</c:choose>


Set some default value of bean attribute fname to "MyName" and on run time assign fname value to "Neeraj".

In above lines of code, with your first page render, you will find result as Bye and when do a page refresh you will see result as hello.

Solution to this kind of scenario is to use JSF panel group tag with rendered attribute as shown below.


<h:panelGroup rendered="#{customerBean.fname=='Neeraj'}">
   <c:out value="Hello">
</h:panelGroup>
<h:panelGroup rendered="#{customerBean.fname!='Neeraj'}">
   <c:out value="Bye">
</h:panelGroup>

No comments: