时间:2020-12-12 08:57:33 | 栏目:JAVA代码 | 点击:次
前言
本教程基于Springmvc,Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对Struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
Tiles 框架彻底揭示了 jsp:includes 内部的概念 ?D?D 从而允许您更灵活地创建可重用的页面。使用 Tiles 框架,开发人员能够通过组合可重用的 tile 来构建页面。您应该将 tile 看作是可视组件。
下面本文就给大家简单说tiles框架的整合。
先贴上源码:
http://xiazai.jb51.net/201712/yuanma/springmc(jb51.net).rar(这里用的是Idea,eclipse,导入的时候可能会有些差异)
1、tiles依赖的jar包:
maven代码:
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils-core</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.4.2</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-api</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>2.2.1</version> </dependency>
2、Spring mvc 中配置Tiles框架(springmvc-servlet.xml)
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass"> <value> org.springframework.web.servlet.view.tiles2.TilesView </value> </property> </bean> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> </bean>
3、配置tiles (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="base.definition" template="/jsp/templet.jsp"> <put-attribute name="title" value="" /> <put-attribute name="header" value="/jsp/head.jsp" /> <put-attribute name="menu" value="/jsp/menu.jsp" /> <put-attribute name="body" value="" /> <put-attribute name="footer" value="/jsp/foot.jsp" /> </definition> <definition name="CustomerForm" extends="base.definition"> <put-attribute name="title" value="HHHHHHH"/> <put-attribute name="body" value="/jsp/CustomerForm.jsp"/> </definition> <definition name="CustomerDetail" extends="base.definition"> <put-attribute name="title" value="DDDDDD"/> <put-attribute name="body" value="/jsp/CustomerDetail.jsp"/> </definition> </tiles-definitions>
其中templet.jsp(base.definition)作为模板,其中定义的header,menu,body,footer都需要自己配置相应的jsp文件,一般body为可变的,其他为固定的jsp。
templet.jsp代码:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> <%@ page language="java" 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><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" width="200"><tiles:insertAttribute name="menu" /></td> <td width="600"><tiles:insertAttribute name="body" /></td> </tr> <tr> <td height="30" colspan="2"><tiles:insertAttribute name="footer" /> </td> </tr> </table> </body> </html> <h1>Bottom</h1> <span style="font-size: 14px;"><p><a href="http://www.qlysou.com/">www.qlysou.com</a></p></span> <span style="font-size: 14px;"><p>Copyright <code class="xml plain">©</code><a href="http://www.qlysou.com/">www.qlysou.com</a> </p></span>
4.效果
写的不是特别好,大家可以下载源码跑起来了看看就明白了,有问题欢迎留言交流
总结