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:

 
		   org.sitemesh
		   sitemesh
		   3.0.0
	  


3 . 配置 Sitemesh 3 过滤器

在 web.xml 中添加 Sitemesh Filter: 



  ...

  
    sitemesh
    org.sitemesh.config.ConfigurableSiteMeshFilter
  
  
    sitemesh
    /*
  
  


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

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




    内容页的标题


    内容页的body部分



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





    <sitemesh:write property='title' /> - ltcms




    
header

demo.html的title将被填充到这儿:
demo.html的body将被填充到这儿:
footer


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



    
    

    
    


6 . 运行效果

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

7 . sitemesh3.xml 配置详解


    
  text/html
  application/vnd.wap.xhtml+xml
  application/xhtml+xml
  ...
  
  
  
  
  
  
  

  
  
    /articles/*
    /decorators/article.html
    /decorators/two-page-layout.html
    /decorators/common.html
  

  
  
  
  
  
  
    
    
  
  ...



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 中配置即可:


 

    
    

    
    
     
             
     


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+"/";
%>



  
    
    
  <sitemesh:write property='title' /> 
    
	
	
	    
	
	
	
 
  
  
  
    This is my JSP page.aaa 


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+"/";
%>



  
    
    
    My JSP 'b.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
   ffffffffffffffffffffffffffffffffffffff
    This is my JSP page.bbb 


你可能感兴趣的:(sitemesh)