Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: DispatcherServlet and static content

In my Spring web-app i have mapped /app/* to dispatcher servlet. What is the best approach in that scenerio to separate a static content like images, .js, .css from dispatcher ?

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

For example, when i open url: http://server/context/app/users and on users.jsp have <img src="images/test.png"/> i get no image because http://server/conext/app/users/images/test.png is not a right url.

Now i use absolute path like: <img src="/context/images/test.png"/> but it complicates JSP code a little and searching something better.

like image 205
marioosh Avatar asked Jan 19 '23 18:01

marioosh


2 Answers

You can use <mvc:resources> to tell the DispatcherServlet that certain paths should be handles as static resources.

See section 15.12.4 of the Spring 3.0.x manual for how to configure it.

like image 191
skaffman Avatar answered Jan 31 '23 10:01

skaffman


I serve static content using this configuration which will default if there is no handler mapping found for a URL and just serves the URL.

<mvc:default-servlet-handler/>
like image 45
Tito Avatar answered Jan 31 '23 11:01

Tito