Posts Tagged JSTL
loadBundle’s behaviour with JSTL tags
Let’s start with a bit of knowledge.
f:loadBundle
is a JSF tag which loads a resource bundle and saves it as a variable in the request scope. The RichFaces a4j:loadBundle
tag is a substitution for the f:loadBundle
tag and allows to use reference to bundle messages during the Ajax re-rendering.
When I discovered the RichFaces tag, I immediately replaced all the f:loadBundle
tags by a4j:loadBundle
. Was I right? I thought at first, but then I got a problem. 🙁
The problem was appearing when I started mixing RichFaces and JSF tags.
For example, let’s take the following resource bundle:
active=true
And the following code:
<a4j:loadBundle basename="Messages" var="msg" /> Active is #{msg.active} -- <c:if test="#{msg.active}"> Hello World! </c:if>
This displays ‘Active is true --
‘.
What is wrong there? The active message is true but the c:if
condition failed! 😯
Let’s now try the following:
<a4j:loadBundle basename="Messages" var="msg" /> Active is #{msg.active} -- <c:if test="#{empty msg.active}"> Hello World! </c:if>
This displays ‘Active is true -- Hello World!
‘.
What does that mean? It seems that JSTL doesn’t get the value of the active
message but gets an empty string instead!
In conclusion, if the resource bundle is loading using the RichFaces tag, the messages will be not visible by the JSTL tags.
To fix this problem, you will have to also load the resource bundle using the JSF tag:
<a4j:loadBundle basename="Messages" var="msg" /> <f:loadBundle basename="Messages" var="msg" /> Active is #{msg.active} -- <c:if test="#{msg.active}"> Hello World! </c:if>
As expected, this displays ‘Active is true -- Hello World!
‘. 🙂