首页 > 网站 > WEB开发 > 正文

spring mvc4集成tiles3

2024-04-27 15:09:23
字体:
来源:转载
供稿:网友

1.给项目添加tiles依赖

<dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-api</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>3.0.7</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>3.0.7</version> </dependency>

2.配置一个TilesConfigurer bean 该bean负责定位和加载Tile定义并协调生成tiles.

@Bean public TilesConfigurer tilesConfigurer(){ TilesConfigurer tiles = new TilesConfigurer(); //指定tile定义的位置 tiles.setDefinitions(new String[]{"/admin/layout/tiles.xml"}); //启用刷新 tiles.setCheckRefresh(true); return tiles; }

3.配置tiles的视图解析器

@Bean public ViewResolver viewResolver(){ return new TilesViewResolver(); }

4.定义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="template" template="/admin/layout/template.jsp"> <put-attribute name="title" value=""/> <put-attribute name="header" value="/admin/layout/header.jsp"/> <put-attribute name="menu" value=""/> <put-attribute name="body" value=""/> </definition> <definition name="base" extends="template"> <put-attribute name="menu" value="/admin/layout/menu.jsp"/> </definition> <definition name="login" extends="template"> <put-attribute name="title" value="登录页面"/> <put-attribute name="body" value="/admin/login.jsp"/> </definition> <definition name="regist" extends="template"> <put-attribute name="title" value="注册页面"/> <put-attribute name="body" value="/admin/regist.jsp"/> </definition> <definition name="home" extends="base"> <put-attribute name="title" value="主页"/> <put-attribute name="body" value="/admin/index.jsp"/> </definition></tiles-definitions>

5.创建相应的jsp文件,下面是template.jsp文件的写法。

<%@ taglib uri="http://tiles.apache.org/tags-tiles" PRefix="tiles" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><tiles:insertAttribute name="title" ignore="true"/></title> <link href="/assets/bootstrap3/CSS/bootstrap.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]--></head><body> <tiles:insertAttribute name="header"/> <div class="container-fluid"> <div class="row"> <div class="col-xs-8 col-md-3"> <tiles:insertAttribute name="menu"/> </div> <div class="col-xs-12 col-md-9"> <tiles:insertAttribute name="body"/> </div> </div> </div> <!-- jQuery (necessary for Bootstrap's Javascript plugins) --> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="/assets/bootstrap3/js/bootstrap.js"></script></body></html>

6.写一个控制器,访问相关页面即可。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表