Tiles 3.0.5 不适用于 spring 4.1.7
Tiles 3.0.5 is not working with spring 4.1.7
我将基于 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
您看不到页眉和页脚的原因是您使用的是
<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>
我将基于 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
您看不到页眉和页脚的原因是您使用的是
<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>