今天的任務是添加Tiles2的支持,完成之後效果如下:
直接開始今天的內容了
1.在pom.xml中添加Tiles2的依賴
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.1.6</version>
</dependency>
2.添加Tiles2的配置文件tiles-def.xml,並添加tiles-jsp.tld文件(找到對應版本)(注意:DOCTYPE tiles-definitions PUBLIC 聲明要注意版本,比如Tiles包是2.1的要申明為2.1,這裡我們用的Tiles2.0.x,所以申明用2.0的配置文件
)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="welcome" template="/template/layout.jsp">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="head" value="/template/head.jsp"/>
<put-attribute name="content" value="/template/content.jsp"/>
<put-attribute name="foot" value="/template/foot.jsp"/>
</definition>
</tiles-definitions>
3.創建Tiles模版以及需要的內容頁
layout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title><tiles:insertAttribute name="title" /></title>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
</HEAD>
<BODY>
<table>
<tbody><tr>
<td><tiles:insertAttribute name="head"/></td></tr>
<tr>
<td><tiles:insertAttribute name="content"/></td></tr>
<tr>
<td><tiles:insertAttribute name="foot"/></td></tr>
</tbody></table>
</BODY>
</HTML>
head.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.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
this is head
content.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.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<table height="420px">
<tr><td>this is body</td>
</tr>
</table>
foot.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
this is foot
4.修改struts.xml文件,使用Tiles解析
user.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" namespace="/user" extends="tiles-default">
<action name="login" class="userAction" method="login">
<result name="input">/login.jsp</result>
<result name="success" type="tiles">welcome</result>
</action>
</package>
</struts>
Tiles的支持到此完成。
這個系列到此也基本結束,後面有根據時間可能會加上隨機碼驗證,以及MD5編譯密碼或者Fckeditor錄入文章等常用內容,謝謝關注