tiles是一种JSP布局框架,主要目的是为了将复杂的JSP页面作为一个页面的部分机能,然后组合成一个最终的页面,这种做法便于对各个页面机能的变更和维护,减少代码量,实现代码的重用。
tiles最早是被用作struts的组件(struts-tiles)来使用的,发展到现在已经可以和各种框架(spring,struts)整合使用。可参看其官网:官网
1.相关jar包引用(这里是整合spring,spring mvc,maven)
pom.xml
<properties> <spring.version>4.1.6.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <cglib.version>2.2.2</cglib.version> <hibernate.version>4.3.7.Final</hibernate.version> <aspectj.version>1.8.4</aspectj.version> <mysql.version>5.1.29</mysql.version> <slf4j.version>1.7.7</slf4j.version> <logback.version>1.1.2</logback.version> <profile.id>${build.profile.id}</profile.id> <tiles.version>3.0.5</tiles.version> </properties> <dependencies> <!--tiles系列--> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-api</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--Cglib代理,Spring依赖--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <!--aspect,织入库,Spring依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.2</version> </dependency> <!--aspect库,Spring依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <!-- aop联盟定义的一组关于AOP的公共接口,Spring Aop依赖它 --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!--java ee--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!--spring data jpa--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.0.0.RELEASE</version> </dependency> <!--hibernate系列--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!--jpa support--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- JSR303规范及实现 --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <!-- JSR303规范的hibernate实现 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.3.Final</version> </dependency> <!--hibernate 的二级缓存 ehcache--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> </dependency> <!--ehcache核心包--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.9</version> </dependency> <!--dbcp依赖--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!-- Slf4j + logback的日志方案 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies>
2.springmvc的配置文件中的解析器等的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--配置自动扫描包--> <context:component-scan base-package="com.ycitcxl.sssp" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!--<!–配置视图解析器–>--> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"--> <!--p:prefix="/WEB-INF/jsp/"--> <!--p:suffix=".jsp">--> <!--</bean>--> <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:viewClass="org.springframework.web.servlet.view.tiles3.TilesView" p:order="1"/> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" p:preparerFactoryClass="org.springframework.web.servlet.view.tiles3.SpringBeanPreparerFactory" p:definitions-ref="definitions"/> <util:list id="definitions"> <value>classpath:tiles.xml</value> </util:list> <!--视图解析配置--> <mvc:view-resolvers> <mvc:tiles view-names="tilesViewResolver"/> <mvc:jsp prefix="/WEB-INF/jsp" suffix=".jsp" view-class="org.springframework.web.servlet.view.JstlView"/> </mvc:view-resolvers> <!--处理静态资源的默认servlet--> <mvc:default-servlet-handler/> <!--spring会自动注册一些bean来处理请求--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
3.定义layout以及layout的component
layout/classic.jsp()
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <tiles:insertAttribute name="head" ignore="true"/> <title><tiles:insertAttribute name="title"/></title> </head> <body> <tiles:insertAttribute name="header"/> <tiles:insertAttribute name="menu"/> <tiles:insertAttribute name="main"/> <tiles:insertAttribute name="footer"/> </body> </html>
header.jsp(页面的头部)
<%-- Created by IntelliJ IDEA. User: 111 Date: 2016/2/6 Time: 13:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <head> <nav> <ul style="list-style-type: none"> <li style="display: inline;"><span>微网站</span></li> <li style="display: inline;"><span>微活动</span></li> <li style="display: inline;"><span>微商城</span></li> </ul> </nav> </head>
menu.jsp(左侧菜单页面)
<%-- Created by IntelliJ IDEA. User: 111 Date: 2016/2/6 Time: 13:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <ul style="list-style-type: none"> <li> <span>砸金蛋</span> </li> <li> <span>刮刮卡</span> </li> <li> <span>大转盘</span> </li> </ul>
footer.jsp(页面底部)
<%-- Created by IntelliJ IDEA. User: 111 Date: 2016/2/6 Time: 14:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <footer> <p>2010-2014 信息导航有限公司 京ICP备1200342号-1 京公网安备1101006035号</p> </footer>
tiles.xml(tiles的配置文件)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> <tiles-definitions> <definition name="layout.classic_act" template="/WEB-INF/jsp/lay/classic.jsp"> <put-attribute name="title" value="微活动"/> <put-attribute name="menu" value="/WEB-INF/jsp/lay/menu.jsp"/> <put-attribute name="header" value="/WEB-INF/jsp/lay/header.jsp"/> <put-attribute name="footer" value="/WEB-INF/jsp/lay/footer.jsp"/> </definition> </tiles-definitions>
home.jsp(页面调用)
<%-- Created by IntelliJ IDEA. User: 111 Date: 2016/2/6 Time: 11:56 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <tiles:insertDefinition name="layout.classic_act"> <%--<tiles:putAttribute name="head"></tiles:putAttribute>--%> <%--<tiles:putAttribute name="app"></tiles:putAttribute>--%> <tiles:putAttribute name="main"> <span>hello world!</span> <span> 恭祝大家猴年大吉! </span> </tiles:putAttribute> </tiles:insertDefinition>