Spring MVC 3 & Tiles 2: static resources are not displayed, even in non-tiles pages

旧巷老猫 提交于 2019-12-06 12:26:53

You should remove 'Jsp View Resolver' from 'myapp-servlet.xml' file as you are planning to use only 'Tiles View Resolver'.

Regarding static resources, the way you referenced your static resources in baselayout.jsp, you have to move your static resources folder outside 'WEB-INF' folder. Update resources location on myapp-servlet.xml file:

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

I got it working. The implementation of Tiles helped me discover a few configuration problems in my webapp. After I added Tiles, not only Titles didn't work but also none of the static resources (css, js, images) displayed correctly. I tried removing Tiles and going back to the previous state (when everything seemed to work) but the resources issue still remained.

To solve the problem of resources not displaying I followed some guidelines from these posts: Using Spring, mapping to root in web.xml, static resources aren't found, Servlet mapping / vs /* and from a diagram at the Official Maven page.

I moved the resources and jsp folders out of WEB-INF and added the following servlet to the web.xml to handle all static files:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>    
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.jpeg</url-pattern>
    <url-pattern>*.eot</url-pattern>
    <url-pattern>*.svg</url-pattern>
    <url-pattern>*.ttf</url-pattern>
    <url-pattern>*.woff</url-pattern>
</servlet-mapping>

Finally, I updated all paths inside tiles.xml to suit the new directory.

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