六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 176|回复: 0

Tiles框架

[复制链接]

升级  42%

31

主题

31

主题

31

主题

秀才

Rank: 2

积分
113
 楼主| 发表于 2013-2-6 10:57:02 | 显示全部楼层 |阅读模式
1.在你的struts配置文件struts-config.xml中加入下面的配置:

<plug-in className="org.apache.struts.tiles.TilesPlugin" >    <!-- Path to XML definition file -->    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />    <set-property property="moduleAware" value="true" />  </plug-in>

2.生成tiles-def.xml文件(/WEB-INF/tiles-def.xml):

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC        "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"        "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"> <tiles-definitions>  <definition name="base-definition" path="/layout.jsp">     <put name="sidebar" value="sidebar.jsp"/>     <put name="header" value="header.jsp"/>     <put name="content" value=""/>     <put name="footer" value="footer.jsp"/>  </definition>  <definition name="index-definition" extends="base-definition">     <put name="content" value="indexContent.jsp"/>  </definition> </tiles-definitions>

3.生成layout.jsp布局文件:

<%@ page language="java" pageEncoding="ISO-8859-1"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %><%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %><%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html:html lang="true">  <head>    <html:base />        <title>layout.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <table width="100%" height="100%">    <tr>    <td width="150" valign="top" align="left" bgcolor="#CCFFCC">       <tiles:insert attribute="sidebar"/>    </td>    <td valign="top" height="100%" width="*">      <table width="100%" height="100%">         <tr> <td height="15%"> <tiles:insert attribute="header"/> </td> </tr>        <tr> <td valign="top" height="*"> <tiles:insert attribute="content"/> </td></tr>       <tr> <td valign="bottom" height="15%"><tiles:insert attribute="footer"/></td></tr>     </table>    </td>  </tr>  </table>    </body></html:html>

4.生成要使用的JSP文件sidebar.jsp,header.jsp,footer.jsp。

5.通过action-mappings配置你的tiles组件:

<action path="/tilesExam" type="com.yourcompany.struts.action.TilesExamAction">      <set-property property="cancellable" value="true" />      <forward name="success" path="index-definition"></forward>    </action>

只要tilesExam.do的响应一到达,在经过action的内部处理以后,就会转发复合视图来显示。

6.TilesExamAction类

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;/**  * MyEclipse Struts * Creation date: 04-19-2010 *  * XDoclet definition: * @struts.action validate="true" */public class TilesExamAction extends Action {/* * Generated Methods *//**  * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {// TODO Auto-generated method stubreturn mapping.findForward("success");}}

7.index.jsp

<%@ page contentType="text/html; charset=gb2312" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <tiles:insert page="layout.jsp" flush="true">         <tiles:put name="sidebar" value="sidebar.jsp"/>       <tiles:put name="header"  value="header.jsp"/>          <tiles:put name="content" value="indexContent.jsp"/>          <tiles:put name="footer"  value="footer.jsp"/>    </tiles:insert>

8.上面的jsp页面也可以写为

<%@ page contentType="text/html; charset=gb2312" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <tiles:insert definition="index-definition"/>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表