我正在尝试使用Spring MVC 3和Spring Security 3配置Tiles 2.2并将其集成到Webapp构建中,但是遇到了一些困难:


未显示图块定义。
也不加载任何静态资源(css,js),甚至是那些从不使用Tiles的页面调用的资源。


注意1:当我单击不使用Tiles的jsp文件的.css和.js链接时,总是会重定向到索引页面。

注意2:在添加图块之前,一切正常。

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>

最佳答案

当您计划仅使用“ Tiles View Resolver”时,应该从“ myapp-servlet.xml”文件中删除“ Jsp View Resolver”。

关于静态资源,即在baselayout.jsp中引用静态资源的方式,您必须将静态资源文件夹移至“ WEB-INF”文件夹之外。更新myapp-servlet.xml文件上的资源位置:

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

关于css - Spring MVC 3和Tiles 2:即使在非tiles页面中也不会显示静态资源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15010977/

10-12 02:33