EL context path evaluation difference between outputLink and graphicImage
Asked Answered
P

2

5

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>
Prudenceprudent answered 10/11, 2010 at 20:4 Comment(0)
A
15

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">
Anastigmatic answered 13/4, 2011 at 20:45 Comment(2)
We are using Facelets. I didn't think we were using a JSP servlet at all, but I may be wrong. I'm still a little confused how in one line the pageContext object is valid and in the next is not. I thought I'd tried relying on not using the context and it had failed also. I'll need to get into the guts of our environment and sort out these shenanigans.Prudenceprudent
It won't evaluate at all in Facelets. It's the h:graphicImage which added the context path itself.Anastigmatic
N
3

Try this #{facesContext.externalContext.requestContextPath} i hope this can help you also check this link link text

Regards, Sergio Valdez

Nb answered 22/12, 2010 at 19:46 Comment(3)
You need to elaborate on your answer. Where should I try that? What difference does the # make? What is different between requestContextPath and contextPath?Prudenceprudent
# is used by JSF to declare EL instead of $ that is used in standar JSP # is prefered by JSF but you can also still using $, you have to check which object you can use with facesContext.externalContext cause the request now is managed by JSF.Nb
The #{request.contextPath} is shorter.Anastigmatic

© 2022 - 2024 — McMap. All rights reserved.