问题
I'm trying to configure and integrate Tiles 2.2 to a webapp build with Spring MVC 3 and Spring Security 3 but I'm having some difficulties:
- Tiles definitions are not being displayed.
- No static resources (css, js) are being loaded either, even the ones that are called from pages that are not using Tiles.
NOTE 1: when I click on the .css and .js links of the jsp files that don't use Tiles, I'm always redirected to the index page.
NOTE 2: Before adding Tiles, everything worked just fine.
web.xml
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/myapp/*</url-pattern>
</servlet-mapping>
myapp-servlet.xml
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<!-- JSP View Resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Tiles View Resolver -->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" >
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd" >
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/pages/layouts/base-layout.jsp">
<put-attribute name="header" value="/WEB-INF/pages/tiles/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/pages/tiles/footer.jsp" />
</definition>
<definition name="template2-tiles" extends="baseLayout">
<put-attribute name="title" value="FixealaBA" />
<put-attribute name="body" value="/WEB-INF/pages/template2.jsp" />
</definition>
</tiles-definitions>
base-layout.jsp
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title><tiles:insertAttribute name="title" ignore="true" /></title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/myjquery.js"></script>
<link type="text/css" href="${pageContext.request.contextPath}/resources/css/mystyle.css" rel="stylesheet">
</head>
<body>
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="body" />
<tiles:insertAttribute name="footer" />
</body>
</html>
回答1:
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/" />
回答2:
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.
来源:https://stackoverflow.com/questions/15010977/spring-mvc-3-tiles-2-static-resources-are-not-displayed-even-in-non-tiles-pa