How to use if, else condition in jsf to display image

后端 未结 3 640
花落未央
花落未央 2021-01-31 08:43

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 no

相关标签:
3条回答
  • 2021-01-31 09:16

    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.

    0 讨论(0)
  • 2021-01-31 09:18

    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?

    0 讨论(0)
  • 2021-01-31 09:34

    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>
    
    0 讨论(0)
提交回复
热议问题