Vojtěch
Vojtěch

Reputation: 12416

Accessing static files (webapp/static/ for inst.) from the web application

I have a web application with a following structure:

src
+ main
+-+ java
  + resources
  + webapp
  +-+ WEB-INF
    + static

The static directory is server by org.apache.catalina.servlets.DefaultServlet, otherwise it is served by the application. What I am trying to achieve, is to access the static directory from the application. However, once it is deployed on the server, the directory seems to be "lost" - I can't access it neither in relative path (by new File()) nor in classpath (by new ClassPathResource()). I could see, that the directory is missing in the target dir, which is probably correct, but I don't know what I can do about it.

Thanks.

Edit

  1. Maybe it is not clear, but directory static is a sibling of WEB-INF and not its child.
  2. The static directory might contain 2 GB of images. So it is not desired that this directory should be copied every time the application is deployed (so putting it in the classpath is not probably a good idea).
  3. I just can't find the behaviour of DefaultServlet, which I allow to access this directory. Does it copy all the stuff somewhere else? Or does it access it as by absolute path?

Upvotes: 2

Views: 2880

Answers (2)

Mike Clark
Mike Clark

Reputation: 10136

There is no standardized way to access files stored under WEB-INF. The spirit of the WEB-INF folder is that it contains data that the framework (container/server) reads. It is not seen as a place for data that the user of the container (you) read directly. Depending on the container software (and version) that you are using (Tomcat vs. JBoss vs. WebSphere vs. WebLogic, etc), you might be able to kludge your way into WEB-INF, but it would not be portable across containers, and so I would not recommend it.

Consider putting the files somewhere else, such as in the classpath or in a subfolder of the webapp that is not WEB-INF.

Upvotes: 1

dardo
dardo

Reputation: 4970

I'm pretty sure WEB-INF isn't visible to the classpath, you'd have to put it in the webapp folder. Better yet, you probably want to look into using an mvc:resources tag.

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

Search mvc:resources for an explaination.

EDIT

If you place the resource in src/main/resources, they should be available to a ClassPathResource

Upvotes: 2

Related Questions