Wednesday, February 13, 2013

Serving Static HTML within WAR file using Spring

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/" />
<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

No comments: