Tiles 3.0.5无法使用spring 4.1.7

时间:2015-07-26 05:58:09

标签: jsp maven spring-mvc spring-4 tiles-3

我将基于Spring的应用程序(4.1.7)配置为使用Tiles 3.0.5,但它无法正常工作。它没有在baseLayout中显示页眉,页脚。它只显示index.jsp页面

的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.learning.springmvc</groupId>
    <artifactId>spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Learning</name>
  <properties>
    <org.springframework.version>4.1.7.RELEASE</org.springframework.version>
    <apache.tiles>3.0.5</apache.tiles>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${apache.tiles}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>

</project>

瓦片definition.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software
Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
    <definition name="baseLayout" template="/WEB-INF/tiles/template/baseLayout.jsp">
        <put-attribute name="title" value="Layout Title"/>
        <put-attribute name="heading" value=""/>
        <put-attribute name="tagline" value=""/>
        <put-attribute name="header" value="/WEB-INF/tiles/template/header.jsp"/>
        <put-attribute name="content" value=""/>
        <put-attribute name="footer" value="/WEB-INF/tiles/template/footer.jsp"/>        
    </definition>

    <definition name="home" extends="baseLayout">
        <put-attribute name="title" value="Home" />
        <put-attribute name="heading" value="Home" />
        <put-attribute name="tagline" value="Home" />
        <put-attribute name="content" value="/WEB-INF/views/index.jsp" />
    </definition>
</tiles-definitions>

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="com.learning.controller"/>

    <!-- define resource folder location -->
    <mvc:resources mapping="/resources/**" location="/resources/"  />

    <mvc:annotation-driven/>

    <bean 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- For tiles -->
  <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
      <property name="order" value="-2" />
  </bean>
  <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
      <property name="definitions">
          <list>
              <value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
          </list>
      </property>
  </bean>

</beans>

控制器

package com.learning.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    @RequestMapping(value="/", method=RequestMethod.GET )
    public String index(ModelMap model)
    {
        model.addAttribute("message", "Welcome to Home Page");
        return "home";
    }

    @RequestMapping(value="/welcome", method=RequestMethod.GET )
    public String welcome(ModelMap model)
    {
        model.addAttribute("message", "Welcome to Home Page");
        return "welcome";
    }

}

baseLayout.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><tiles:getAsString name="title"/></title>
         <jsp:include page="baseCss.jsp" />
    </head>
    <body>
        <!--header and navigation start-->
            <jsp:include page="header.jsp" />
        <!--header and navigation end-->

         <!--main content start-->
            <tiles:insertAttribute name="content"/>
         <!--main content end-->



        <!--footer start-->
            <jsp:include page="footer.jsp" />
        <!--footer end-->

        <jsp:include page="baseJS.jsp" />
     </body>
</html>

的index.jsp

<div class="container_12">
This is index page
</div

1 个答案:

答案 0 :(得分:0)

您没有看到页眉和页脚的原因是因为您正在使用

<jsp:include page="footer.jsp" />

相反,你应该使用

<tiles:insertAttribute name="footer" />

如果以上内容无法立即解决问题,请按照以下指南进行操作

删除

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

更改此

  <!-- For tiles -->
  <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
      <property name="order" value="-2" />
  </bean>
  <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
      <property name="definitions">
          <list>
              <value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
          </list>
      </property>
  </bean>

使用此

<bean id="tilesViewResolver"
        class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
    </bean>
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles/definitions/tile-definition.xml</value>
            </list>
        </property>


    </bean>

使用后续

更改baseLayout.jsp
<title>
   <tiles:insertAttribute name="title"></tiles:insertAttribute>
</title>

<body>
        <header class="header">
            <tiles:insertAttribute name="header" />
        </header>
        <div class="content">
            <tiles:insertAttribute name="content" />
        </div>
        <footer class="footer">
            <tiles:insertAttribute name="footer" />
        </footer>
    </body>

<强> POM

<dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>