views:

171

answers:

2

Hello,

I'm trying to read a css file with the Resources provided by Spring.

My application looks like this:

  • src
    • src/com herer my classes inside packages
  • WebContent
    • WebContent/resources/style/myCSS.css --> the css I want to read
    • WebContent/WEB-INF --> here is my application-context.xml

I can get the css and read it by doing something like this:

UrlResource file = new UrlResource("http://localhost:8080/myApp/resources/style/myCSS.css");

but it depends on the server and aplication names. I've tried to do it by other implementations of Resource Interface, but the file is not found cause I can't find out how to wite the path. I've tried with this:

FileSystemResource file = new FileSystemResource("/WebContent/resources/style/myCSS.css");

I also tried with wildcards, but it doesn't find the file either.

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:/WEB-INF/application-context-core.xml");
Resource file = ctx.getResource("file:**/myCSS.css");

How should I write the path to get the css.

Thanks.

+2  A: 

There is ServletContextResource. You can construct it passing the ServletContext and the relative path.

Bozho
A: 

What about new ClassPathResource("/resources/style/myCSS.css")?

Arne Burmeister
that won't work, because the css is _not_ on the classpath.
Bozho