EL context path evaluation difference between outputLink and graphicImage

Adam picture Adam · Nov 10, 2010 · Viewed 10k times · Source

I'm using the following to get a help document in our app. My problem is that while the <h:graphicImage> evaluates the context path correctly, the h:outputLink evalutates it to nothing. I have tried using both $ and # in the h:outputLink because I understand they have different evaluation times.

What is the difference in how the two EL expressions evaluate?

<h:outputLink value="${pageContext.servletContext.contextPath}/services/help.pdf">
    <h:graphicImage 
        url="${pageContext.servletContext.contextPath}/images/help.png" 
        alt="Online Help"/>
</h:outputLink>

Answer

BalusC picture BalusC · Apr 13, 2011

That the context path doesn't appear in <h:outputLink> suggests that you're actually using Facelets instead of JSP. The ${pageContext} doesn't exist in Facelets at all. It's specific to legacy JSP. Both expressions have just evaluated to an empty string. There is thus no difference between them at all.

That the context path appears in <h:graphicImage> is fully expected. This is automatically included by the component itself. In fact, the entire expression is superfluous and the following should work as good.

<h:graphicImage url="/images/help.png" alt="Online Help"/>

The <h:outputLink> does indeed not automatically include the context path. Only the <h:link> does that. You'd need to include it yourself. In Facelets, you can use #{request} to get a handle to HttpServletRequest which in turn has a getContextPath() as well (and which is used by <h:graphicImage> under the covers).

<h:outputLink value="#{request.contextPath}/services/help.pdf">