35

I have a condition where I have an enrollment form in which if userid is 0 it should show the dummy image and when I edit user from any update, I check for if userid which is not equal to 0 then display the image corresponding to userid.

I used JSTL inside jsf page. But always it tries to go to else loop for showing image. The functionality is working fine. The only thing is I can't display the dummy image when I visit the page first. Here s my code.:

<c:if test="${'#{user.userId}' == '0'}">
  <a href="Images/thumb_02.jpg" target="_blank" ></a>
  <img src="Images/thumb_02.jpg" />
</c:if>
<c:otherwise>
  <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"</a>
  <img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:otherwise>

Can I use this JSTL tag or can I do it using jsf?

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
pravi
  • 434
  • 1
  • 4
  • 13

3 Answers3

57

For those like I who just followed the code by skuntsel and received a cryptic stack trace, allow me to save you some time.

It seems c:if cannot by itself be followed by c:otherwise.

The correct solution is as follows:

<c:choose>
    <c:when test="#{some.test}">
        <p>some.test is true</p>
    </c:when>
    <c:otherwise>
        <p>some.test is not true</p>
    </c:otherwise>
</c:choose>

You can add additional c:when tests in as necessary.

jmkgreen
  • 1,633
  • 14
  • 22
  • It's great you've written an update! Just corrected my answer so that it'd compile as well. It's sometimes helpful to reread your answer. – skuntsel Sep 19 '14 at 15:28
  • It's also worth noting that some.test needs to not evaluate to false during the apply request values phase. See here for more (point 5): https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value – Sam Apr 10 '18 at 13:41
  • I believe to use the h:outputText component is better, check this answer: https://coderanch.com/t/213739/java/JSTL-tag-working-JSF-page – Hazim May 19 '22 at 11:03
39

It is illegal to nest EL expressions: you should inline them. Using JSTL is perfectly valid in your situation. Correcting the mistake, you'll make the code working:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core">
    <c:if test="#{not empty user or user.userId eq 0}">
        <a href="Images/thumb_02.jpg" target="_blank" ></a>
        <img src="Images/thumb_02.jpg" />
    </c:if>
    <c:if test="#{empty user or user.userId eq 0}">
        <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"></a>
        <img src="/DisplayBlobExample?userId=#{user.userId}" />
    </c:if>
</html>

Another solution is to specify all the conditions you want inside an EL of one element. Though it could be heavier and less readable, here it is:

<a href="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></a>
<img src="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></img>
Victor
  • 8,309
  • 14
  • 80
  • 129
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Not only does this result in (almost) duplicated code but also doubled evaluation time. Consider `c:choose + c:when + c:otherwise` instead. – Quoting Eddie Dec 07 '17 at 12:48
8

Instead of using the "c" tags, you could also do the following:

<h:outputLink value="Images/thumb_02.jpg" target="_blank" rendered="#{not empty user or user.userId eq 0}" />
<h:graphicImage value="Images/thumb_02.jpg" rendered="#{not empty user or user.userId eq 0}" />

<h:outputLink value="/DisplayBlobExample?userId=#{user.userId}" target="_blank" rendered="#{not empty user and user.userId neq 0}" />
<h:graphicImage value="/DisplayBlobExample?userId=#{user.userId}" rendered="#{not empty user and user.userId neq 0}"/>

I think that's a little more readable alternative to skuntsel's alternative answer and is utilizing the JSF rendered attribute instead of nesting a ternary operator. And off the answer, did you possibly mean to put your image in between the anchor tags so the image is clickable?

Ryan D
  • 1,023
  • 11
  • 16