struts----tiles官方例子

来源:互联网 发布:网络语鬼畜啥意思 编辑:程序博客网 时间:2024/05/18 00:08
Struts 2的 Struts 2的瓷砖范例Struts 2的瓷砖范例下面的示例演示如何集成Struts 2的和Tiles使用Struts2的瓷砖插件。在部署描述符先设定Tiles定义文件。 <context-param><param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name><param-value>/WEB-INF/tiles.xml</param-value></context-param>然后设置的侦听。<listener><listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class></listener>完整的web.xml文件。<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>Struts2Example15</display-name> <context-param><param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name><param-value>/WEB-INF/tiles.xml</param-value></context-param> <listener><listener-class>org.apache.struts2.tiles.StrutsTilesListener </listener-class></listener> <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>tiles.xml文件包含以下的定义。<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tiles-definitions PUBLIC"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN""http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="baseLayout" template="/baseLayout.jsp"><put-attribute name="title"  value="Template"/><put-attribute name="header" value="/header.jsp"/><put-attribute name="menu"   value="/menu.jsp"/><put-attribute name="body"   value="/body.jsp"/><put-attribute name="footer"   value="/footer.jsp"/></definition> <definition name="welcome" extends="baseLayout"><put-attribute name="title"  value="Welcome"/><put-attribute name="body"   value="/welcome.jsp"/>     </definition> <definition name="friends" extends="baseLayout"><put-attribute name="title"  value="Friends"/><put-attribute name="body"   value="/friends.jsp"/>     </definition> <definition name="office" extends="baseLayout"><put-attribute name="title"  value="Office"/><put-attribute name="body"   value="/office.jsp"/>     </definition> </tiles-definitions>在这里我们定义一个包含一个标题,标题,菜单,身体和页脚区域的“baseLayout” 。 头,菜单和页脚地区仍然只有标题和正文内容的变化所有的布局相同。在baseLayout.jsp页面中,我们创建一个经典瓷砖布局.------------header------------      |menu  | body      |------------footer------------<%@ taglib uri="http://tiles.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=UTF-8"><title><tiles:insertAttribute name="title" ignore="true" /></title></head><body><table border="1" cellpadding="2" cellspacing="2" align="center"><tr><td height="30" colspan="2"><tiles:insertAttribute name="header" /></td></tr><tr><td height="250"><tiles:insertAttribute name="menu" /></td><td width="350"><tiles:insertAttribute name="body" /></td></tr><tr><td height="30" colspan="2"><tiles:insertAttribute name="footer" /></td></tr></table></body></html>       header.jsp<div align="center" style="font-weight:bold">TV shows</div>menu.jsp<%@taglib uri="/struts-tags" prefix="s"%><a href="<s:url action="friendsLink"/>" >Friends</a><br><a href="<s:url action="officeLink"/>" >The Office</a><br>body.jsp<p> sample body content.</p>footer.jsp<div align="center">© vaannila.com</div>friends.jsp<%@page contentType="text/html" 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>JSP Page</title>    </head>    <body>        <p>More details about the Friends TV show goes here...</p>    </body></html>office.jsp<%@page contentType="text/html" 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>JSP Page</title>    </head>    <body>        <p>More details about the Office TV show goes here...</p>    </body></html>index.jsp<META HTTP-EQUIV="Refresh" CONTENT="0;URL=welcomeLink.action">welcome.jsp<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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>Welcome Guest.</body></html>在struts.xml文件中创建一个新的瓷砖的结果如下所示。<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><package name="default" extends="struts-default"><result-types><result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /></result-types><action name="*Link" method="{1}" class="com.vaannila.action.LinkAction"><result name="welcome" type="tiles">welcome</result><result name="friends" type="tiles">friends</result><result name="office" type="tiles">office</result></action></package></struts>对于每个结果,而不是转发到JSP页面转发给Tiles定义。当您执行的例子,下面的页面会显示。 ---------------TV showes---------------         |Friends  | Wellcome GuestThe Office      |---------------vaannila.com---------------menu.jsp页的菜单项,点击每个菜单项的标题和正文内容仅变化。<%@taglib uri="/struts-tags" prefix="s"%><a href="<s:url action="friendsLink"/>" >Friends</a><br><a href="<s:url action="officeLink"/>" >The Office</a><br>每个菜单项被点击时,在不同的 LinkAction类的方法被调用。package com.vaannila.action;import com.opensymphony.xwork2.ActionSupport; public class LinkAction extends ActionSupport { private static final long serialVersionUID = -2613425890762568273L; public String welcome(){   return "welcome";      }public String friends(){   return "friends";      } public String office(){   return "office";       }}您需要下面的lib文件运行的例子。commons-fileupload-1.2.1commons-io-1.3.2commons-logging-1.1freemarker-2.3.13junit-3.8.1ognl-2.6.11struts2-convention-plugin-2.1.6struts2-core-2.1.6xwork-2.1.2 struts2-tiles-plugin-2.1.6.jartiles-api-2.1.2tiles-compat-2.1.2tiles-core-2.1.2tiles-jsp-2.1.2tiles-servlet-2.1.2commons-beanutils-1.8.01commons-digester-1.8.1工程目录结构: