Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL c:if alternative?

 
Eric Wei
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I'm trying to use the jstl tag if, but it doesn't work. I'm sure the condition is false, but it gets into the if anyway.

<c:if test="${sickBean.description == 'ADR'}">
<a href="usuarioList.jsf" onclick="updateObjectIframe(this); return false;"> XXXX !!! </a>
</c:if>

I've searched a lil bit:

With Facelets, JSTL evaluations happen at compilation time, whereas other EL evaluations happen at render time. This means that the majority of the time, JSTL does not do what the developer intends. Unless they know JSTL quite well.


Uh.. and most people suggests to use the rendered atribute, but that's not my case.
What are the alternatives for the c:if ?

Thanks in advance!!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18023
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primary alternative to the "c:if" tag is the "rendered" attribute, since most other conditional expressions covered by the JSTL "if" tag have no business on the View definition. A JSF View is not a JSP, even when you give it the ".jsp" extension - it's a template.

The problem is you're using brute-force HTML. I don't recommend mixing HTML and JSF either, but it's far less likely to cause actual malfunction than JSTL does. Use the JSF "hutputLink" element and you can slap a "rendered" attribute on it no problem.

Speaking of keeping the MVC implementation clean, I also recommend not coding any complex or business-related logic into EL. Put the logic in a backing bean and expose the results as a property. For example, instead of
code]
test="${sickBean.description == 'ADR'}"
[/code]

do
[code]
rendered="#{sickBean.describedAsADR}"
[/code]

Pair this with an "isDescribedAsADR" method in sickBean and you'll keep your maintained logic all in one place and not have to play hide-and-seek to find while file the logic is in when the codes change.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic