You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Wayne Fay <wa...@gmail.com> on 2005/11/11 22:27:34 UTC

Getting the ContextPath to set stylesheet href properly

Hi guys,

Having some real problems with paths in my Faces app for some reason!

I am using Oracle App Server 10g 10.1.2 with MyFaces 1.1.0, JSP 1.2 &
Web-app 2.3. Wish I could use Web-app 2.4 but 10.1.2 does not support
it, and it requires DTDs in config xml files etc. Lots of fun.

File structure:
\index.jsp
\style\css\project.css
\uploadfile.jsp
\results\uploadfile.jsp
\results\pageheader.jsp
\results\externaldata.jsp

I am including pageheader.jsp in each page in my project like so:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:t="http://myfaces.apache.org/tomahawk">
<jsp:directive.page contentType="text/html;charset=utf-8"/>
<f:view locale="#{facesContext.externalContext.request.locale}">
<f:loadBundle basename="web.bundles.Messages" var="msg"/>
<html>
  <head>
    <link rel="stylesheet" href="style/css/dcts.css" type="text/css"/>
    <title><h:outputText value="#{msg.title}" /></title>
      </head>
      <body width="100%">
        <jsp:include page="pageheader.jsp" flush="true"/> <!-- or
/results/pageheader.jsp in files in the "root" -->

This works some times but not always. I have also tried /style/ and
../style/ and it does not work every time either. It seems to depend
on where I'm "coming from" ie coming from index.jsp, then calling a
/results/externaldata.jsp with href="style/" works. But then if I'm in
/results/uploadfile.jsp and call /results/externaldata.jsp, then the
href="style/" does not work, and it requires ="../style".

So I have decided to provide the entire path, using Context Path or
something similar.

I have tried:
<link rel="stylesheet" href="<h:outputText
value="#{facesContext.externalContext.requestContextPath}"
/>/style/css/project.css" type="text/css"/>
does not work

<link rel="stylesheet"
href="${facesContext.externalContext.requestContextPath}/style/css/project.css"
type="text/css"/>
does not work

<link rel="stylesheet" href="${pageContext.request.contextPath}
/style/css/project.css" type="text/css"/>
does not work

Also, I'm looking for a "universal" way to refer to this
/results/pageheader.jsp include file. In some places I have to use
/results and in others I can just use pageheader.jsp. There must be a
way to get the my application path (in the file system) and use it in
the jsp:include call rather than sometimes using /results and other
times not.

This seems to be a pretty easy thing, but its not working for me, at
least not consistently.

How are the rest of you doing this?

Thanks.
Wayne

Re: Getting the ContextPath to set stylesheet href properly

Posted by Francesco Consumi <co...@istitutodeglinnocenti.it>.
>> How are the rest of you doing this?
>>

we use:
   <link href="${pageContext.request.contextPath}/css/stile.css" 
rel="stylesheet" type="text/css">

in JSF EL we use instead:

#{facesContext.externalContext.requestContextPath}


and, finally, in javascript:

location.pathname == ('/' + location.pathname.split('/')[1]) ? "" : '/' 
+ location.pathname.split('/')[1];


We use often these functions because almost all our projects, when are 
in development run on a subdir (localhost:8084/project on 
Tomcat/NetBeans), but in production run on a rootdir 
(project.istitutodeglinnocenti.it on Apache 2.0/Tomcat/mod_jk)

see for example www.istitutodeglinnocenti.it, quite completely made with JSF.

-- 
Francesco Consumi
Ufficio Sistemi informativi
Istituto degli Innocenti
Piazza SS.Annunziata, 12
50122 Firenze
consumi at istitutodeglinnocenti.it
Tel. +39 055 2037320
ICQ# 12516133



Re: Getting the ContextPath to set stylesheet href properly

Posted by Volker Weber <us...@weber-oldenburg.de>.
Hi Wayne,

I'm not shure about the 'best' way to solve this, but one of the easyest
is to provide a bean method for getting the contectPath.

public static String getContextPath() {
  return
FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
}

Put this in a ManagedBean, then you can access it like this:

<link rel="stylesheet"
    href="${BEAN_NAME.contextPath}/style/css/project.css"
    type="text/css"/>

Note the '${...}', the '#{...}' syntax can only used inside jsf tag
attributes.

Regards

   Volker

Wayne Fay wrote:
> Hi guys,
> 
> Having some real problems with paths in my Faces app for some reason!
> 
> I am using Oracle App Server 10g 10.1.2 with MyFaces 1.1.0, JSP 1.2 &
> Web-app 2.3. Wish I could use Web-app 2.4 but 10.1.2 does not support
> it, and it requires DTDs in config xml files etc. Lots of fun.
> 
> File structure:
> \index.jsp
> \style\css\project.css
> \uploadfile.jsp
> \results\uploadfile.jsp
> \results\pageheader.jsp
> \results\externaldata.jsp
> 
> I am including pageheader.jsp in each page in my project like so:
> 
> <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
>           xmlns:f="http://java.sun.com/jsf/core"
>           xmlns:h="http://java.sun.com/jsf/html"
>           xmlns:t="http://myfaces.apache.org/tomahawk">
> <jsp:directive.page contentType="text/html;charset=utf-8"/>
> <f:view locale="#{facesContext.externalContext.request.locale}">
> <f:loadBundle basename="web.bundles.Messages" var="msg"/>
> <html>
>   <head>
>     <link rel="stylesheet" href="style/css/dcts.css" type="text/css"/>
>     <title><h:outputText value="#{msg.title}" /></title>
>       </head>
>       <body width="100%">
>         <jsp:include page="pageheader.jsp" flush="true"/> <!-- or
> /results/pageheader.jsp in files in the "root" -->
> 
> This works some times but not always. I have also tried /style/ and
> ../style/ and it does not work every time either. It seems to depend
> on where I'm "coming from" ie coming from index.jsp, then calling a
> /results/externaldata.jsp with href="style/" works. But then if I'm in
> /results/uploadfile.jsp and call /results/externaldata.jsp, then the
> href="style/" does not work, and it requires ="../style".
> 
> So I have decided to provide the entire path, using Context Path or
> something similar.
> 
> I have tried:
> <link rel="stylesheet" href="<h:outputText
> value="#{facesContext.externalContext.requestContextPath}"
> />/style/css/project.css" type="text/css"/>
> does not work
> 
> <link rel="stylesheet"
> href="${facesContext.externalContext.requestContextPath}/style/css/project.css"
> type="text/css"/>
> does not work
> 
> <link rel="stylesheet" href="${pageContext.request.contextPath}
> /style/css/project.css" type="text/css"/>
> does not work
> 
> Also, I'm looking for a "universal" way to refer to this
> /results/pageheader.jsp include file. In some places I have to use
> /results and in others I can just use pageheader.jsp. There must be a
> way to get the my application path (in the file system) and use it in
> the jsp:include call rather than sometimes using /results and other
> times not.
> 
> This seems to be a pretty easy thing, but its not working for me, at
> least not consistently.
> 
> How are the rest of you doing this?
> 
> Thanks.
> Wayne
> 

-- 
Don't answer to From: address!
Mail to this account are droped if not recieved via mailinglist.
To contact me direct create the mail address by
concatenating my forename to my senders domain.

Re: Getting the ContextPath to set stylesheet href properly

Posted by Wayne Fay <wa...@gmail.com>.
Here's the equivalent using "old" style JSP. This works! But I'd
prefer to make it work the "other" way.

<%@ page contentType="text/html; charset=utf-8" buffer="none"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<%@ taglib uri="http://myfaces.apache.org/extensions" prefix="x"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>

<f:loadBundle basename="web.bundles.Messages" var="msg"/>
<f:view locale="#{facesContext.externalContext.request.locale}">

<html>
<head>
<link rel="stylesheet" href="<%= request.getContextPath()
%>/style/css/project.css" type="text/css"/>
<title><h:outputText value="#{msg.title}" /></title>
</head>
<body>
<c:import url="/results/pageheader.jsp"/>
<h:panelGrid width="100%" style="height:100%;" columns="2"
columnClasses="tree_panel, content_panel">
<t:collapsiblePanel collapsed="false">
<c:import url="/results/navigationtree.jsp"/>

Ideally, I'd get this working using the "newer" XML style, but if
there's no way to do it, this will have to work. :-(

Wayne

Re: Getting the ContextPath to set stylesheet href properly

Posted by Wayne Fay <wa...@gmail.com>.
I thought I should provide version info for all the pieces...

JDK 1.4.2
Oracle App Server 10g 10.1.2
J2EE 1.3
JSP 1.2
Servlets 2.3
EJB 2.0
MyFaces 1.1.0
Tomahawk 1.1.0

The next version of OAS 10g (10.1.3) will support
J2EE 1.4
JSP 2.0
Servlets 2.4
EJB 2.1 and 3.0
but of course its not out yet. Just a Dev Preview which isn't working
for me, and my IT Ops group won't deploy to a non-final release of OAS
anyways.

Thanks for any help.
Wayne

RE: Getting the ContextPath to set stylesheet href properly

Posted by Warren Bell <wa...@clarksnutrition.com>.
I had the same problem with images. It seemed to work some times and not
others, depending on the previous page path. I ended up using absolute paths
or ../../ . I get an empty string when I use <h:outputText
value="#{facesContext.externalContext.requestContextPath}"/> anywhere, how
come?

> Subject: Getting the ContextPath to set stylesheet href properly
>
>
> Hi guys,
>
> Having some real problems with paths in my Faces app for some reason!
>
> I am using Oracle App Server 10g 10.1.2 with MyFaces 1.1.0, JSP 1.2 &
> Web-app 2.3. Wish I could use Web-app 2.4 but 10.1.2 does not support
> it, and it requires DTDs in config xml files etc. Lots of fun.
>
> File structure:
> \index.jsp
> \style\css\project.css
> \uploadfile.jsp
> \results\uploadfile.jsp
> \results\pageheader.jsp
> \results\externaldata.jsp
>
> I am including pageheader.jsp in each page in my project like so:
>
> <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
>           xmlns:f="http://java.sun.com/jsf/core"
>           xmlns:h="http://java.sun.com/jsf/html"
>           xmlns:t="http://myfaces.apache.org/tomahawk">
> <jsp:directive.page contentType="text/html;charset=utf-8"/>
> <f:view locale="#{facesContext.externalContext.request.locale}">
> <f:loadBundle basename="web.bundles.Messages" var="msg"/>
> <html>
>   <head>
>     <link rel="stylesheet" href="style/css/dcts.css" type="text/css"/>
>     <title><h:outputText value="#{msg.title}" /></title>
>       </head>
>       <body width="100%">
>         <jsp:include page="pageheader.jsp" flush="true"/> <!-- or
> /results/pageheader.jsp in files in the "root" -->
>
> This works some times but not always. I have also tried /style/ and
> ../style/ and it does not work every time either. It seems to depend
> on where I'm "coming from" ie coming from index.jsp, then calling a
> /results/externaldata.jsp with href="style/" works. But then if I'm in
> /results/uploadfile.jsp and call /results/externaldata.jsp, then the
> href="style/" does not work, and it requires ="../style".
>
> So I have decided to provide the entire path, using Context Path or
> something similar.
>
> I have tried:
> <link rel="stylesheet" href="<h:outputText
> value="#{facesContext.externalContext.requestContextPath}"
> />/style/css/project.css" type="text/css"/>
> does not work
>
> <link rel="stylesheet"
> href="${facesContext.externalContext.requestContextPath}/style/css
> /project.css"
> type="text/css"/>
> does not work
>
> <link rel="stylesheet" href="${pageContext.request.contextPath}
> /style/css/project.css" type="text/css"/>
> does not work
>
> Also, I'm looking for a "universal" way to refer to this
> /results/pageheader.jsp include file. In some places I have to use
> /results and in others I can just use pageheader.jsp. There must be a
> way to get the my application path (in the file system) and use it in
> the jsp:include call rather than sometimes using /results and other
> times not.
>
> This seems to be a pretty easy thing, but its not working for me, at
> least not consistently.
>
> How are the rest of you doing this?
>
> Thanks.
> Wayne