Spring: DispatcherServlet and static content

旧街凉风 提交于 2019-12-30 10:57:20

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/5976266/spring-dispatcherservlet-and-static-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!