Preface
This tutorial is based on Springmvc. Spring MVC is the best MVC framework at present. Since the release of Spring 2.5 version, the ease of use has been greatly improved due to the support of annotation configuration. Spring 3.0 is more perfect, achieving the transcendence of Struts 2. Now more and more development teams have chosen Spring MVC.
The Tiles framework thoroughly reveals the concept inside jsp:includes, thus allowing you to create reusable pages more flexibly. Using the Tiles framework, developers can build pages by combining reusable tiles. You should think of tile as a visual component.
The following article will briefly talk about the integration of the tiles framework.
Post the source code first:
http://xiazai.VeVB.COM/201712/yuanma/springmc(VeVB.COM).rar (Idea, eclipse are used here, and there may be some differences when importing)
1. The jar package that tiles depends on:
maven code:
<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. Configure the Tiles framework in Spring mvc (springmvc-servlet.xml)
<bean id="viewResolver"> <property name="viewClass"> <value> org.springframework.web.servlet.view.tiles2.TilesView </value> </property> </bean> <bean id="tilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> </bean>
3. Configure 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>
Among them, templet.jsp(base.definition) is used as a template, and the defined header, menu, body, and footer need to configure the corresponding jsp files themselves. Generally, the body is variable, and the others are fixed jsp.
templet.jsp code:
<%@ 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 cellpadding="2" cellpacing="2" align="center"> <tr> <td colspan="2"><tiles:insertAttribute name="header" /> </td> </tr> <tr> <td><tiles:insertAttribute name="menu" /></td> <td><tiles:insertAttribute name="body" /></td> </tr> <tr> <td 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>©</code><a href="http://www.qlysou.com/">www.qlysou.com</a> </p></span>
4. Effect
The writing is not very good. You can download the source code and run it and then understand. If you have any questions, please leave a message and communicate.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.