前言
学习这个之前发现网上相关的示例都是通过xml配置的。我这里给出一个用JavaConfig进行配置的Demo
为了节省读者的时间,我这里给出一份已经配好基本环境写好框架代码的Demo:http://download.youkuaiyun.com/detail/luchengtao11/9805811
本文将专注于Tiles的整合部分,读者可以在给出的Demo上进行修改。项目为Maven项目,Eclipse导入可以直接运行。
整合进ApacheTiles之后的工程在这里:http://download.youkuaiyun.com/detail/luchengtao11/9805815可以做个参照。
1.配置TilesConfigurer来解析定义
也就是在WebConfig中添加一下代码,Spring自动装配Bean
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tiles = new TilesConfigurer();
tiles.setDefinitions(new String[] {
"/WEB-INF/layout/tiles.xml",
"/WEB-INF/views/**/tiles.xml"
});
tiles.setCheckRefresh(true);
return tiles;
}
@Bean
public ViewResolver viewResolver() {
return new TilesViewResolver();
}
2.定义Tile
在WEB-INF下建立文件夹tiles.xml代码如下
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="base" template="/WEB-INF/layout/page.jsp">
<put-attribute name="header" value="/WEB-INF/layout/header.jsp" />
<put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
</definition>
<definition name="home" extends="base">
<put-attribute name="body" value="/WEB-INF/views/home.jsp" />
</definition>
</tiles-definitions>3.在layout中分别定义page.jsp/footer.jsp,和header.jsp
三者代码如下:
page.jsp
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>
<%@ page session="false" %>
<!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=ISO-8859-1">
<title>Spittr</title>
</head>
<body>
<div id="header">
<t:insertAttribute name="header"> </t:insertAttribute>
</div>
<div id="content">
<t:insertAttribute name="body"></t:insertAttribute>
</div>
<div id="footer">
<t:insertAttribute name="footer"></t:insertAttribute>
</div>
</body>
</html>
footer.jsp
Copyright © Craig Walls
最终的结果如下:
本文提供了一个使用JavaConfig配置的Tiles示例项目,详细介绍了如何配置TilesConfigurer以解析定义,以及如何定义Tiles布局。该项目适用于希望快速了解并实践Tiles整合的开发者。
253

被折叠的 条评论
为什么被折叠?



