Sitemesh 3 模板框架使用

简介: 1 . Sitemesh 3 简介 Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。参考:百度百科,相关类似技术:Apache Tiles。 官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home 。 2 . Sitemesh 3 下载 最新

1 . Sitemesh 3 简介

Sitemesh 是一个网页布局和修饰的框架,基于 Servlet 中的 Filter,类似于 ASP.NET 中的‘母版页’技术。参考:百度百科,相关类似技术:Apache Tiles

官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home 。

2 . Sitemesh 3 下载

最新版本:3.0.0-SNAPSHOT

① GitHub 地址:https://github.com/sitemesh/sitemesh3

② maven:

 <dependency>
		   <groupId>org.sitemesh</groupId>
		   <artifactId>sitemesh</artifactId>
		   <version>3.0.0</version>
	  </dependency>


3 . 配置 Sitemesh 3 过滤器

在 web.xml 中添加 Sitemesh Filter: 

<web-app>

  ...

  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>


4 . 准备两个页面:demo.html 和 decorator.html

① demo.html - “被装饰的页面”,实际要呈现的内容页。

<!DOCTYPE html>
<html>
<head>
    <title>内容页的标题</title>
</head>
<body>
    内容页的body部分
</body>
</html>


② decorator.html - “装饰页面”,所谓的“母版页”。

<!DOCTYPE html>
<html>
<head>
<title>
    <sitemesh:write property='title' /> - ltcms
</title>
<sitemesh:write property='head' />
</head>
<body>
    <header>header</header>
    <hr />
    demo.html的title将被填充到这儿:
    <sitemesh:write property='title' /><br />
    demo.html的body将被填充到这儿:
    <sitemesh:write property='body' />
    <hr />
    <footer>footer</footer>
</body>
</html>


5 . 添加 /WEB-INF/sitemesh3.xml

<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 -->
    <mapping path="/*" decorator="/WEB-INF/views/decorators/decorator.html" />

    <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 -->
    <mapping path="/exclude.jsp*" exclue="true" />
</sitemesh>


6 . 运行效果

访问 demo.html 页面,实际效果如下:

7 . sitemesh3.xml 配置详解

<sitemesh>
    <!--默认情况下,
        sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,
        我们可以添加更多的 mime 类型-->
  <mime-type>text/html</mime-type>
  <mime-type>application/vnd.wap.xhtml+xml</mime-type>
  <mime-type>application/xhtml+xml</mime-type>
  ...
  
  <!-- 默认装饰器,当下面的路径都不匹配时,启用该装饰器进行装饰 -->
  <mapping decorator="/default-decorator.html"/>
  
  <!-- 对不同的路径,启用不同的装饰器 -->
  <mapping path="/admin/*" decorator="/another-decorator.html"/>
  <mapping path="/*.special.jsp" decorator="/special-decorator.html"/>

  <!-- 对同一路径,启用多个装饰器 -->
  <mapping>
    <path>/articles/*</path>
    <decorator>/decorators/article.html</decorator>
    <decorator>/decorators/two-page-layout.html</decorator>
    <decorator>/decorators/common.html</decorator>
  </mapping>

  <!-- 排除,不进行装饰的路径 -->
  <mapping path="/javadoc/*" exclue="true"/>
  <mapping path="/brochures/*" exclue="true"/>
  
  <!-- 自定义 tag 规则 -->
  <content-processor>
    <tag-rule-bundle class="com.something.CssCompressingBundle" />
    <tag-rule-bundle class="com.something.LinkRewritingBundle"/>
  </content-processor>
  ...

</sitemesh>


8 . 自定义 tag 规则

Sitemesh 3 默认只提供了 body,title,head 等 tag 类型,我们可以通过实现 TagRuleBundle 扩展自定义的 tag 规则:

import org.sitemesh.SiteMeshContext;
import org.sitemesh.content.ContentProperty;
import org.sitemesh.content.tagrules.TagRuleBundle;
import org.sitemesh.content.tagrules.html.ExportTagToContentRule;
import org.sitemesh.tagprocessor.State;

public class MyTagRuleBundle implements TagRuleBundle {
    public void install(State defaultState, ContentProperty contentProperty,
            SiteMeshContext siteMeshContext) {
        defaultState.addRule("myContent", new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("myContent"), false));
        
    }
    
    public void cleanUp(State defaultState, ContentProperty contentProperty,
            SiteMeshContext siteMeshContext) {
    }
}


最后在 sitemesh3.xml 中配置即可:


  
  
 <?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <!-- 指明满足“/*”的页面,将被“/WEB-INF/views/decorators/decorator.html”所装饰 -->
    <mapping path="/*" decorator="/a.jsp" />

    <!-- 指明满足“/exclude.jsp*”的页面,将被排除,不被装饰 -->
    <mapping path="/a.jsp*" exclue="true" />
 <span style="color:#FF0000;">    <content-processor>
             <tag-rule-bundle class="MyTagRuleBundle" />
     </content-processor></span>
</sitemesh>


a.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
  <title><sitemesh:write property='title' /></title> 
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 
  </head>
  
  <body>
    This is my JSP page.aaa <br>
    <sitemesh:write property='myContent' />
  </body>
</html>


b.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'b.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <myContent>ffffffffffffffffffffffffffffffffffffff</myContent>
    This is my JSP page.bbb <br>
  </body>
</html>


目录
相关文章
|
开发框架 Java .NET
Sitemesh3使用及配置
Sitemesh3使用及配置
233 0
|
2月前
|
存储 人工智能 测试技术
手把手带你入门AI智能体:从核心概念到第一个能跑的Agent
AI智能体是一种能感知环境、自主决策并执行任务的人工智能系统。它不仅能生成回应,还可通过工具使用、计划制定和记忆管理完成复杂工作,如自动化测试、脚本编写、缺陷分析等。核心包括大语言模型(LLM)、任务规划、工具调用和记忆系统。通过实践可逐步构建高效智能体,提升软件测试效率与质量。
|
前端开发 应用服务中间件 API
VUE+websocket编写实现PC web端控制摄像头
WebSocket是一种全双工通信的数据通信协议。WebSocket的主要功能用处是允许服务器主动地向客户端推送数据信息,使得客户端和服务端之间的数据交换变得更加的简单。
VUE+websocket编写实现PC web端控制摄像头
|
JavaScript
vue路由导航守卫(全局守卫、路由独享守卫、组件内守卫)
vue路由导航守卫(全局守卫、路由独享守卫、组件内守卫)
622 0
|
人工智能 机器人 开发者
实现定制化 AutoGPT 实战
在前期学习基础上,本文指导如何运用AutoGPT完成如生成文件及查询信息并输出到文件等ChatGPT难以实现的任务。首先确保拥有稳定网络、已配置好的AutoGPT环境及可用token。
|
搜索推荐 UED
【Prompt Engineering:自我一致性、生成知识提示、链式提示】
自我一致性是提示工程技术之一,旨在改进链式思维提示中的解码方法。通过少样本CoT采样多个推理路径并选择最一致的答案,有助于提升涉及算术和常识推理任务的性能。例如,在解决年龄相关问题时,通过多次采样并挑选多数答案来提高准确性。此外,生成知识提示技术可预先生成相关信息辅助模型做出更准确预测,进一步优化模型表现。链式提示则通过将复杂任务分解为多个子任务来逐步处理,从而提高模型的透明度和可靠性,便于定位和改进问题。
517 0
【Prompt Engineering:自我一致性、生成知识提示、链式提示】
|
SQL 数据采集 弹性计算
DataWorks产品使用合集之怎么修改生命周期
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
编译器 Shell C++
在编译的两个.o文件中有对同一个头文件的引用,因此在链接时出现结构体重复定义的问题怎么解决
在编译的两个.o文件中有对同一个头文件的引用,因此在链接时出现结构体重复定义的问题怎么解决
413 2