开发者

How to change location of static file in WAR archive?

开发者 https://www.devze.com 2023-03-17 11:07 出处:网络
By default static files are located in WEB-INF directory (accessible as /images/logo.png): foo.war WEB-INF

By default static files are located in WEB-INF directory (accessible as /images/logo.png):

foo.war
  WEB-INF
    web.xml
    images
      logo.png

I want to change the structure and use this one instead (still accessible as /images/logo.png):

foo.war
  WEB-INF
    web.xml
  static
    images
      logo.png

How can I do this with web.xm开发者_如何学Cl?


The container will repsond with a 404 NOT FOUND error if you directly access the files under WEB-INF using HTTP GET .

But now , you said you can access WEB-INF/images/logo.png by /images/logo.png , so I think your web application most probably achieve this result by some URLRewriteFilter mechainsim or by some Java code in the servlet level (eg a filter) , or by your web application 's framework . I suggest you to check your web application to see what mechanism causes this behvaior now and configurate to your desired result accordingly.


According to http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html,:

A WAR has a specific directory structure. The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static Web resources are stored.

The document root contains a subdirectory called WEB-INF, which contains the following files and directories:

web.xml: The Web application deployment descriptor Tag library descriptor files (see Tag Library Descriptors) classes: A directory that contains server-side classes: servlets, utility classes, and JavaBeans components lib: A directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes).

You can also create application-specific subdirectories (that is, package directories) in either the document root or the WEB-INF/classes directory.

So the default behavior is what you're looking for. Is your document root set incorrectly to serve content from WEB-INF?


You may use a filter or URLRewriteFilter to point /images/* to /static/images/*.

If you just want your folder structure to be /static/images for development time organization purposes, but the deployment URL to be /images -- you may need to alter your build script to copy /static/** to /.

I personally would not bother whether my static files are referred as /static/images or /images -- because they would be referred in my code (only), which I have control over.

If you are using these files in CSS and that's why you wanted the path to stay the same... better keep the images under /static/css/images and have the images that are referred in the CSS here. In this way, no matter where you move your CSS folder, you would not bother spoiling your CSS.

0

精彩评论

暂无评论...
验证码 换一张
取 消