当前位置: 首页 > 工具软件 > Apache Tiles > 使用案例 >

第6章 渲染Web视图---Apache Tiles 笔记2

仲孙思源
2023-12-01

概述:

Apache Tiles,定义适用于所有页面的通用页面布局。Spring MVC以视图解析器的形式为Apache Tiles提供了支持,这个视图解析器能够将逻辑视图名解析为Tile定义

6.3.1 配置Tiles视图解析器

需要配置两个bean TilesConfigurer 和 TilesViewResolver

需要引入jar包

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>3.0.7</version>
</dependency>


@Bean
public TilesConfigurer tilesConfigurer(){
TilesConfigurer tiles = new TilesConfigurer();
//指定Tile定义的位置  可以使用通配符
// tiles.setDefinitions(new String[] {"WEB-INF/**/tiles.xml"})
tiles.setDefinitions(new String[]{"/WEB-INF/layout/tiles.xml"});
//启用刷新功能
tiles.setCheckRefresh(true);
return tiles;
}

@Bean
public ViewResolver viewResolver(){
return new TilesViewResolver();
}

写一个tiles.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="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>
	<definition name="registerForm" extends="base">
		<put-attribute name="body" value="/WEB-INF/views/registerForm.jsp"/>
	</definition>
	
	<definition name="profile" extends="base">
		<put-attribute name="body" value="/WEB-INF/views/profile.jsp"/>
	</definition>
	<definition name="spittles" extends="base">
		<put-attribute name="body" value="/WEB-INF/views/spittles.jsp"/>
	</definition>
	
	<definition name="spittle" extends="base">
		<put-attribute name="body" value="/WEB-INF/views/spittle.jsp"/>
	</definition>
</tiles-definitions>


page.jsp 
<%@ taglib	uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>
<!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>Spittr</title>
<link rel="stylesheet" 
	type="text/css"
	href="<c:url value="/resources/style.css"/>">
</head>
<body>
	<div id="header">
		<t:insertAttribute name="header"/>
	</div>
	<div id="content">
		<t:insertAttribute name="body" />
	</div>
	
	<div id="footer">
		<t:insertAttribute name="footer"/>
	</div>
</body>
</html>


header.jsp


<%@ taglib	uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>
<!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>Spittr</title>
<link rel="stylesheet" 
	type="text/css"
	href="<c:url value="/resources/style.css"/>">
</head>
<body>
	<a href="<s:url value="/"/>">
		<img  src="<s:url value="/resources"/> /images/1.png" border="0"/>
	</a>
</body>
</html>

footer.jsp

<%@ taglib	uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>
<!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>Spittr</title>
<link rel="stylesheet" 
	type="text/css"
	href="<c:url value="/resources/style.css"/>">
</head>
<body>
	Copyright @copy; Craig Walls
</body>
</html>

总结:就是通过tiles.xml将jsp进行组装,然后引入到另一个JSP中


 类似资料: