Refactor a link and an image

Posted by Mihail Stoynov on Stack Overflow See other posts from Stack Overflow or by Mihail Stoynov
Published on 2011-02-21T23:05:56Z Indexed on 2011/02/21 23:25 UTC
Read the original article Hit count: 225

Filed under:
|

I have to write an link with an image inside. Instead of explaining, here's the code I have now:

<c:if test="${userSession.loggedUser eq null and company.image != null}">
    <a onclick="${rich:component('loginPanel')}.show()">
        <img src="/download.do?hash=#{company.image.hash}" />
    </a>
</c:if>
<c:if test="${userSession.loggedUser eq null and company.image == null}">
    <a onclick="${rich:component('loginPanel')}.show()">
        <img src="${request.contextPath}/img/icons/logo_default.jpg" />
    </a>
</c:if>
<c:if test="${userSession.loggedUser ne null and company.image != null}">
    <a href="company.xhtml?${company.name}">
        <img src="/download.do?hash=#{company.image.hash}" />
    </a>
</c:if>
<c:if test="#{userSession.loggedUser ne null and company.image == null}">
    <a href="company.xhtml?${company.name}">
        <img src="${request.contextPath}/img/icons/logo_default.jpg" />
    </a>
</c:if>

This code looks awful - there are two exact links with two exact images but combined in all possible combinations. Is there a better way? Is there a way to avoid c:if - it created tables?

Update: Bozho proposes: You can replace <c:if and <a with <h:outputLink rendered="#{..}". Apart from that I don't see any other optimization.

But it doesn't work. This does not render correctly:

<a href=>
<h:outputLink rendered="#{..}
<h:outputLink rendered="#{..}
</a>

(the image is outside the anchor)

This does render fine:

<h:outputLink value=>
<h:outputLink rendered="#{..}
<h:outputLink rendered="#{..}
</a>

, but it always adds href and in two of the cases I don't want href when rendered.

© Stack Overflow or respective owner

Related posts about jsf

Related posts about richfaces