In my previous post
I've described how to serve static HTML files with simple WAR
In this post, I will add a description for the same task but using Spring MVC
In your WAR file, create any folder under WEB-INF
- sample-war.war
----- META-INF
-------- MANIFEST.MF
----- WEB-INF
-------- classes
-------- some-pages
----------- some-page.html
-------- lib
-------- web.xml
----- index.html
then, go to your servlet-context.xml file, this is a file used by spring to configure web context, and add this line
<resources mapping="/**" location="/WEB-INF/some-pages/" />
or
<mvc:resources mapping="/**" location="/WEB-INF/some-pages/" />
depends in what namespace your servlet-context.xml is configured
The meaning of this line is: serve me any HTML located under /WEB-INF/some-pages/ and map it to my root context
This would mean, that the link http://localhost:8080/sample-war/some-page.html will work
Another example: if we use this mapping
<resources mapping="/super-web-site/**" location="/WEB-INF/some-pages/" />
then this link http://localhost:8080/sample-war/super-web-site/some-page.html will work
Wednesday, February 13, 2013
Serving Static HTML within WAR file using Spring
Serving Static HTML within WAR file
War file, as you know is a zipped folder. It must have the following structure
- sample-war.war
----- META-INF
-------- MANIFEST.MF
----- WEB-INF
-------- classes
-------- lib
-------- web.xml
Two important thing to remember:
- HTML file or folder that contains HTML files located inside WEB-INF will not be accessible to users
- HTML file or folder that contains HTML files located inside the WAR file, on the same level as WEB-INF will be visible to users
Let's consider the following structure, and see what will work and what will not
Assumption: your application deployed on localhost, port 8080 and the application name is sample-war
- sample-war.war
----- META-INF
-------- MANIFEST.MF
----- WEB-INF
-------- classes
-------- hidden-pages
----------- hidden-page.html
-------- lib
-------- web.xml
----- pages
-------- visible-page.html
----- index.html
----- outside.html
URI | Is Page Visible |
---|---|
http://localhost:8080/sample-war/index.html | true |
http://localhost:8080/sample-war/outside.html | true |
http://localhost:8080/sample-war/pages/visible-page.html | true |
http://localhost:8080/sample-war/hidden-page.html | false |
http://localhost:8080/sample-war/hidden-pages/hidden-page.html | false |
Note: Normally you would like to enable this URI
http://localhost:8080/sample-war
and make it serve some home page for you, index.html for example.
In this case, simply add those lines to your web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>