MyEclipse generates the following on every jsp page, and I have been keeping it without knowing exactly why, that is until now J

 

<%String path = request.getContextPath();

      String basePath = request.getScheme() + "://"

                  + request.getServerName() + ":" + request.getServerPort() + path + "/";

%>

<html>

<head>

      <base href="<%=basePath%>">

 

 

Yes – it would be much cleaner to have a JSF component for this.

 

Regards,

Yee

 

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Craig McClanahan
Sent: Monday, 28 November 2005 12:41 PM
To: MyFaces Discussion
Subject: Re: Problem with Stylesheet loading after returning to the site by pressing the cancel button

 

 

On 11/27/05, Simon Kitching <[EMAIL PROTECTED]> wrote:

Wayne Fay wrote:
>> Maybe your page is in a subfolder, so on the next page the path to the
>> stylesheet  has  a  wrong base. Try to change the path to the css file
>> to an absolute path instead of a relative one.
>
> I have had numerous problems with stylesheets in JSF.
>
> The only way I could get things to work consistently was with absolute
> paths, as Steffen has already suggested.

Remember that the *browser* fetches the stylesheet by passing an
*absolute* URL to the remote server.

When the stylesheet URL is relative, the *browser* forms an absolute URL
for it by taking the URL it used *to fetch the current html page*, and
appending the relative stylesheet path.

Any server-side framework that uses forwards internally is going to have
problems with this, because the browser doesn't know the actual *file
path* for the jsp (or whatever) that was forwarded to in order to
generate the response page, it only knows the original URL it sent.


There is actually an additional gotcha to be aware of with JSF (as well as other frameworks, like Struts, that do RequestDispatcher.forward() calls on the server side).

You'll note that the location bar of your browser shows the URL to which the last form submit was performed, *not* the page you navigated to that caused the most recent rendering.  If you try to use relative URLs, *this* is the URL that the browser will resolve against ... which will cause problems if the new page is at a different subdirectory level than the previous page was.

 

For just about any dynamic server stuff you therefore *must* ensure the
page returned to the browser contains *absolute* URLs in it. There are
special Struts JSP tags to help with this; I'm sure PHP and similar also
have helpers to turn relative paths into absolute paths.

It looks to me like the tomahawk t:stylesheet tag currently just adds
the webapp context to the path, but doesn't handle paths relative to the
current JSP.


Struts has a tag to emit a <base> element (which goes in the <head> section) that tells the browser "use this page's URL as the base for resolving relative paths).  It would be handy to have a JSF component that did the same -- indeed, there's such a component available in the struts-faces integration library that oculd be copied into Tomahawk.

 

Regards,

Simon


Craig
 

 

 

Reply via email to