在struts1中使用tiles来对jsp页面进行模板的配置,主要目的是为了将复数的jsp页面作为一个的页面的部分机能,然后用来组合成一个最终表示用页面用的。
1.在jsp页面上需要导入该标签
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
2.新建一个模板jsp文件layout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!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>Insert title here</title>
</head>
<body>
<table border="2" width="300" bordercolor="Gray">
<tr>
<td bgcolor="Blue"><strong><tiles:getAsString name="title" /></strong></td>
</tr>
<tr>
<td><tiles:insert attribute="header" /></td>
</tr>
<tr>
<td><tiles:insert attribute="body" /></td>
</tr>
</table>
</body>
</html>
创建header.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<strong>This is the header</strong>
<a href="todo.do">goto</a>
<img src="<%=request.getContextPath()%>/images/struts-power.gif" align="right" border="0" />
</body>
</html>
创建body.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<div align="center">
<b><i>This is a body</i></b>
</div>
</body>
3.使用模板文件来生成jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!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>Insert title here</title>
</head>
<body>
<tiles:insert template="layout.jsp">
<tiles:put name="title" value="This is the title." />
<tiles:put name="header" value="header.jsp" />
<tiles:put name="body" value="body.jsp" />
</tiles:insert>
</body>
</html>
当如果模板中的其中一部分改变,则只需要替换掉其中的区域就可以了,可以很好的对jsp代码进行重用