This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange contextpath problem

 
Filipe Felisbino
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17620
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Filipe Felisbino
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17620
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17620
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a method inside some managed bean, it works. But, my doubt about this problem remains.

Anyway, thanks...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic