struts2.0 之tiles页面布局的简易实现(转)
我的页面布局设置非常非常非常简单,就是用户登录成功后转向的页面只分为三块,上部欢迎,中部详细内容,下部版权信息块
1.lib中在原先基础上需要导入的包:
commons-beanutils-1.6.jar ; commons-collections.jar ; commons-digester-1.8.jar ; struts2-tiles-plugin-2.0.11.2.jar ; tiles-api-2.0.4.jar ; tiles-core-2.0.4.jar ; tiles-jsp-2.0.4.jar
2.在WEB-INF下的
web.xml中补充配置如下信息:
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
3.在WEB-INF下,创建tiles.tld文件,文件内容引自tiles-jsp-2.0.4.jar包中;
4.在WEB-INF下,创建tiles.xml文件,内容参考如下:
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
" http://struts.apache.org/dtds/tiles-config_2_0.dtd">
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
" http://struts.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="showWelcomePage" template="layout.jsp">
<put-attribute name="title" value=""/>
<put-attribute name="head" value="/head.jsp"/>
<put-attribute name="content" value="/welcome.jsp"/>
<put-attribute name="foot" value="/foot.jsp"/>
</definition>
</tiles-definitions>
<definition name="showWelcomePage" template="layout.jsp">
<put-attribute name="title" value=""/>
<put-attribute name="head" value="/head.jsp"/>
<put-attribute name="content" value="/welcome.jsp"/>
<put-attribute name="foot" value="/foot.jsp"/>
</definition>
</tiles-definitions>
请事先建立好所需要引用的JSP文件块
5.在WebRoot下建立layout.jsp,我的代码是这个样子滴,很简单的:
<%@ page language="
java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title><tiles:insertAttribute name="title" /></title>
</head>
<body bgcolor="#7A97B2">
<table width="100%" height="100%" border="2">
<tr>
<td>
<table width="100%" height="100%">
<tr>
<td height="10">
<tiles:insertAttribute name="head"/>
</td>
</tr>
<tr>
<td height="100%">
<tiles:insertAttribute name="content"/>
</td>
</tr>
<tr>
<td height="10" valign="bottom">
<tiles:insertAttribute name="foot"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title><tiles:insertAttribute name="title" /></title>
</head>
<body bgcolor="#7A97B2">
<table width="100%" height="100%" border="2">
<tr>
<td>
<table width="100%" height="100%">
<tr>
<td height="10">
<tiles:insertAttribute name="head"/>
</td>
</tr>
<tr>
<td height="100%">
<tiles:insertAttribute name="content"/>
</td>
</tr>
<tr>
<td height="10" valign="bottom">
<tiles:insertAttribute name="foot"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
6.修改struts.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">
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.devMode" value="false" />
<package name="jessica" extends="tiles-default" >
<action name="logon" class="com.tang.LoginAction">
<result type="tiles">showWelcomePage</result>
<result name="input" type="redirect-action">/index.jsp</result>
</action>
</package>
<action name="logon" class="com.tang.LoginAction">
<result type="tiles">showWelcomePage</result>
<result name="input" type="redirect-action">/index.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
完事了,就是这个样子啦~~~
完事了,就是这个样子啦~~~