sitemesh 使用方法

一、简介

  SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。
  它能通过创建一个包装对象,也就是装饰来包裹的对象。尽管它是由Java语言来实现的,但是它能与其他Web应用很好的集成。

二、使用步骤

1、下载sitemesh jar包

jar包下载官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home

2、把sitemesh jar 导入项目中

创建decorators.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- defaultdir 是模板所在的文件夹路径 -->
<decorators defaultdir="/layouts">

    <!-- 下面是过滤css和js文件,按实际情况写 -->
    <excludes>
        <pattern>/assets/*</pattern>
    </excludes>

    <!-- /* 所有页面,按模板 phone_template.jsp 来修饰 -->
    <decorator name="phone" page="phone_template.jsp">
        <pattern>/*</pattern>
    </decorator>

</decorators>

web.xml文件配置

    <!-- 定义过滤器-->
    <filter>
        <!-- 定义过滤器的实现类 -->
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>

    <!--  定义过滤器拦截URL地址 -->
    <filter-mapping>
        <!-- 过滤器的名称 -->
        <filter-name>sitemesh</filter-name>
        <!-- 过滤器负责拦截的URL,如下定义会拦截所有  -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 模板(phone_template.jsp):

<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
    <!--  sitemesh:title 的作用是,将要修饰的页面的title引入 -->
    <title><sitemesh:title/></title>
    <!-- 不用在意下面的样式引入  -->
    <link href="/js_css/css/bootstrap.css" rel="stylesheet">
    <link href="/js_css/css/offcanvas.css" rel="stylesheet">
</head>

<body class="bg-light">

    <%@include file="phone_head_nva.jsp"%>

    <%@include file="phone_body_nva.jsp"%>


    <main role="main" class="container">
            <!--  这里会把要修饰的页面的body部分引入 -->
            <sitemesh:body></sitemesh:body>
    </main>

    <%@include file="phone_footer.jsp"%>

</body>
</html>

总之,定义模板的作用是,让指定的网页用模板修饰,比如模板的上面有一个导航栏,则被模板修饰的网页都有导航栏。

原文地址:https://www.cnblogs.com/ldl326308/p/9509339.html