You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Mazza, Glen R., ,CPMS" <gl...@CPMS.OSD.MIL> on 2003/07/07 19:38:33 UTC

resolving relative paths to servet webapp/myapp directory

Hello,

I'm doing an XML->PDF XSLT transformation using a XSL stylesheet.

Programmatically, within my Action class, I refer to the stylesheet needed
by using a relative path, with the goal of it being relative to my appname
directory off of the servlet "webapps" directory:

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new
StreamSource("stylesheets\\mystylesheet.xsl"));
..

Problem is, Struts appears to be resolving relative paths from my project
working directory, *not* relative to the "appname" from the webapps
directory in Tomcat.

To confirm this, removing the xsl stylesheet from my working directory
returned this error when I ran the app in Tomcat:

java.io.FileNotFoundException:
D:\myworkingdir\myapp\stylesheets\mystylesheet.xsl 

and not this error:

java.io.FileNotFoundException:
D:\tomcat4.1\webapps\myapp\stylesheets\mystylesheet.xsl

How do I specify filenames in Struts so they will be resolved relative to
the webapps\myapp directory (whatever I name "myapp" to be), and not my
(deleteable) working directory?

Thanks,
Glen

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: resolving relative paths to servet webapp/myapp directory

Posted by Kris Schneider <kr...@dotech.com>.
I tend to use ClassLoader.getResource or ClassLoader.getResourceAsStream to load
resources. The constraint, obviously, is that the class loader must be able to
access the resources. Generally, this means placing resources under
WEB-INF/classes or in JAR files in WEB-INF/lib. If you don't care about
depending on the Servlet API, try ServletContext.getResource or
ServletContext.getResourceAsStream. In your particular case, you'd use:

InputStream in = context.getResourceAsStream("/stylesheets/mystylesheet.xsl");

Quoting Mike Deegan <mp...@hotmail.com>:

> we do something similar but we use the WEB-INF directory as the starting
> point and all works fine
> lets say we are looking for "XML/extraction-map-classday.xml"
> 
> the following code achieves this ...
> 
> public final class HomePageAction extends Action
> ...
> ...
> String xmlURI =
> this.getServlet().getServletContext().getRealPath("/WEB-INF/XML/extraction-m
> ap-classday.xml");
> 
> Therefore the "RealPath" to extraction-map-classday.xml = the value in
> xmlURI
> 
> if you had "stylesheets\mystylesheet.xsl" under the WEB-INF directory maybe
> you could follow or approach
> 
> i know it works for us but maybe we just got lucky !!!
> 
> HTH
> Mike
> 
> 
> 
> ----- Original Message ----- 
> From: "Mazza, Glen R., ,CPMS" <gl...@CPMS.OSD.MIL>
> To: <st...@jakarta.apache.org>
> Sent: Monday, July 07, 2003 11:38 AM
> Subject: resolving relative paths to servet webapp/myapp directory
> 
> 
> > Hello,
> >
> > I'm doing an XML->PDF XSLT transformation using a XSL stylesheet.
> >
> > Programmatically, within my Action class, I refer to the stylesheet
> needed
> > by using a relative path, with the goal of it being relative to my
> appname
> > directory off of the servlet "webapps" directory:
> >
> > TransformerFactory tFactory = TransformerFactory.newInstance();
> > Transformer transformer = tFactory.newTransformer(new
> > StreamSource("stylesheets\\mystylesheet.xsl"));
> > ..
> >
> > Problem is, Struts appears to be resolving relative paths from my project
> > working directory, *not* relative to the "appname" from the webapps
> > directory in Tomcat.
> >
> > To confirm this, removing the xsl stylesheet from my working directory
> > returned this error when I ran the app in Tomcat:
> >
> > java.io.FileNotFoundException:
> > D:\myworkingdir\myapp\stylesheets\mystylesheet.xsl
> >
> > and not this error:
> >
> > java.io.FileNotFoundException:
> > D:\tomcat4.1\webapps\myapp\stylesheets\mystylesheet.xsl
> >
> > How do I specify filenames in Struts so they will be resolved relative to
> > the webapps\myapp directory (whatever I name "myapp" to be), and not my
> > (deleteable) working directory?
> >
> > Thanks,
> > Glen

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: resolving relative paths to servet webapp/myapp directory

Posted by Mike Deegan <mp...@hotmail.com>.
we do something similar but we use the WEB-INF directory as the starting
point and all works fine
lets say we are looking for "XML/extraction-map-classday.xml"

the following code achieves this ...

public final class HomePageAction extends Action
...
...
String xmlURI =
this.getServlet().getServletContext().getRealPath("/WEB-INF/XML/extraction-m
ap-classday.xml");

Therefore the "RealPath" to extraction-map-classday.xml = the value in
xmlURI

if you had "stylesheets\mystylesheet.xsl" under the WEB-INF directory maybe
you could follow or approach

i know it works for us but maybe we just got lucky !!!

HTH
Mike



----- Original Message ----- 
From: "Mazza, Glen R., ,CPMS" <gl...@CPMS.OSD.MIL>
To: <st...@jakarta.apache.org>
Sent: Monday, July 07, 2003 11:38 AM
Subject: resolving relative paths to servet webapp/myapp directory


> Hello,
>
> I'm doing an XML->PDF XSLT transformation using a XSL stylesheet.
>
> Programmatically, within my Action class, I refer to the stylesheet needed
> by using a relative path, with the goal of it being relative to my appname
> directory off of the servlet "webapps" directory:
>
> TransformerFactory tFactory = TransformerFactory.newInstance();
> Transformer transformer = tFactory.newTransformer(new
> StreamSource("stylesheets\\mystylesheet.xsl"));
> ..
>
> Problem is, Struts appears to be resolving relative paths from my project
> working directory, *not* relative to the "appname" from the webapps
> directory in Tomcat.
>
> To confirm this, removing the xsl stylesheet from my working directory
> returned this error when I ran the app in Tomcat:
>
> java.io.FileNotFoundException:
> D:\myworkingdir\myapp\stylesheets\mystylesheet.xsl
>
> and not this error:
>
> java.io.FileNotFoundException:
> D:\tomcat4.1\webapps\myapp\stylesheets\mystylesheet.xsl
>
> How do I specify filenames in Struts so they will be resolved relative to
> the webapps\myapp directory (whatever I name "myapp" to be), and not my
> (deleteable) working directory?
>
> Thanks,
> Glen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org