Patryk Dobrowolski
Patryk Dobrowolski

Reputation: 1595

Static resources in Spring MVC app

In my Spring mvc application I want to serve static resources using mvc:resources.

My web.xml mapping looks that:

<servlet-mapping>
 <servlet-name>main</servlet-name>
 <url-pattern>/</url-pattern>       
</servlet-mapping>

Where main is dispatcher servlet to serve all the content

In my servlet.xml file I added:

<mvc:resources mapping="/static/**" location="/static/"/>

and it works properly when my application context is empty (like localhost:8080/), but when I deploy application in another context it doesn't work, I got 404.

I tried many combinations:

"static/**"
"*/static/**"

Nothing works.

I'm sure it's server context problem, but I have not idea (I couldn't find the solution in Google too) how to solve this. Please, help.

Upvotes: 1

Views: 1885

Answers (2)

Igor G.
Igor G.

Reputation: 7019

The easiest way for we that worked, was adding in the servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation) the following:

<mvc:default-servlet-handler/>

Upvotes: 0

Roy Kachouh
Roy Kachouh

Reputation: 1903

I was able to succesfully map my static resources using the following convention:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

Upvotes: 0

Related Questions