1
votes

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.

2
Are you sure you use correct URLs when you try to load these resources, e.g. do you add context path to these URLs?axtavt
Yup, I add context path.Patryk Dobrowolski
I tried to retrieve resources directly using browser. Maybe problem is somewhere else. Just tell me if I configured it correctly. Maybe there are some not supposed slashes, asterisks or anything.Patryk Dobrowolski

2 Answers

0
votes

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

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