wood burning stoves 2.0*
The moose likes JSF and the fly likes strange contextpath problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "strange contextpath problem" Watch "strange contextpath problem" New topic
Author

strange contextpath problem

Filipe Felisbino
Greenhorn

Joined: Jul 17, 2009
Posts: 4
Hello Guys. I'm facing a strange problem trying to access the pageContext.servletContext.contextPath. The following code:


is rendered this way:
<a href="/resources/img.jpg"><img src="/help/resources/img.jpg" height="60px" /></a>

That is, inside graphiImage, the contextPath is being resolved, but in outputLink no. I've tried to put that expression in the value of a outputText, and still the path isn't printed.

Anyone has idea of what may be happening here ?

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Welcome to the JavaRanch, Filipe!

Most of the JSF components are fully aware of their context paths, so you don't need to explicitly include them. I know that the graphicImage tag is one of them. So you're actually being more clever than you need to be.

There's also a simpler way to code this:


Customer surveys are for companies who didn't pay proper attention to begin with.
Filipe Felisbino
Greenhorn

Joined: Jul 17, 2009
Posts: 4
Thank you Tim.

I've tried to put the link without the contextpath prefix. But, because my jsp is actually inside a sub-directory of the application, the link becomes invalid ( it points to a servlet ). So I've put the the link prefixed with ../, and that works great. But, I'm still intrigued. Why #{pageContext} apears to be null. I've tried to output the values of: #{pageContext.contextPath}, #{pageContext.request.contextPath}, #{pageContext.servletContext.contextPath} in outputLabels, and no one gave me results.

I'm confused about this.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Oops. It turns out that "pageContext" was a backing bean I created that looks up the webapp context via JSFContext. I think there is a generic JSP equivalent, however - I just wasn't aware of it at the time.

"../" starts off a relative URL. A problem with JSF, however, is that the URL isn't as tightly tied to the page being displayed as it is in most other web technologies. This can cause difficulties sometimes, especially when referring to global resources like javascripts and CSS.
Filipe Felisbino
Greenhorn

Joined: Jul 17, 2009
Posts: 4
The pageContext is in the JSF specification as an implicit object:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
# pageContext: The context for the JSP page. Provides access to various objects including:
* servletContext: The context for the JSP page's servlet and any web components contained in the same application. See Accessing the Web Context.
* session: The session object for the client. See Maintaining Client State.
* request: The request triggering the execution of the JSP page. See Getting Information from Requests.
* response: The response returned by the JSP page. See Constructing Responses.


I don't know if there is anything in the faces specification that hides de pageContext, I've never read nothing about this.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Filipe Felisbino wrote:The pageContext is in the JSF specification as an implicit object:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html
# pageContext: The context for the JSP page. Provides access to various objects including:
* servletContext: The context for the JSP page's servlet and any web components contained in the same application. See Accessing the Web Context.
* session: The session object for the client. See Maintaining Client State.
* request: The request triggering the execution of the JSP page. See Getting Information from Requests.
* response: The response returned by the JSP page. See Constructing Responses.


I don't know if there is anything in the faces specification that hides de pageContext, I've never read nothing about this.


Thanks. I thought it was, but I had created the bean before I knew about it. I'm not sure if I'm hiding anything or not - I'd have to go back and check faces-config.
Filipe Felisbino
Greenhorn

Joined: Jul 17, 2009
Posts: 4
Creating a method inside some managed bean, it works. But, my doubt about this problem remains.

Anyway, thanks...
 
jQuery in Action, 2nd edition
 
subject: strange contextpath problem
 
Similar Threads
Menu link image change when I click the button
Take input from <h:inputText> in rich <modalPanel>
can't do hello world with TrueZip
JSF + Portlet URL encoding
breakpoint on scrollableDataTable