SITEMESH牛刀小试

项目中需要做界面处理,于是研究了一下SITEMESH,发现其还是相当的强大,大大便利了开发.
(主页:http://www.opensymphony.com/sitemesh/)

往工程中导入所需要的包:sitemesh-2.4.1.jar
一.配置web.xml,添加
 <!-- sitemesh -->
 <filter>
    <filter-name>sitemesh</filter-name>
      <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
   </filter> 
 <!-- sitemesh  -->
   <filter-mapping>
      <filter-name>sitemesh</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

二.在WEB-INF目录下加入文件decorators.xml,sitemesh.xml
两个文件也就是所说的装饰器配置文件,
我的decorators.xml
   <!-- not menu content jsp -->   
    <decorator name="main_index" page="/web/template/main_layout.jsp">
        <pattern>*</pattern>       
    </decorator>     
    
    <!-- menu content jsp --> 
    <decorator name="main_menu" page="/web/template/main_menu_layout.jsp"  />
   
    <!-- do not need template --> 
    <decorator name="blank" page="/web/template/blank.jsp"  />

 

 指定了三个模板,默认为main_layout.jsp,匹配所有的URL,当然不是所有的页面都是用同一个模板,所以

因此在具体的页面head中再从新指定模板<meta name="decorator" content="main_menu" />,这样既可统一配置又灵活指定,强大呀,赞一个.

 


sitemesh.xml文件
<sitemesh>
 <property name="decorators-file" value="/WEB-INF/decorators.xml" />
 <excludes file="${decorators-file}" />

 <page-parsers>
  <parser content-type="text/html"
   class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
  <parser content-type="text/html;charset=utf-8"
   class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
 </page-parsers>

 <decorator-mappers>
  <mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
   <param name="property.1" value="meta.decorator" /><!--meta name="decorator" content="main_menu" 才起作用-->
   <param name="property.2" value="decorator" />
  </mapper>
  <mapper
   class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
   <param name="config" value="${decorators-file}" />
  </mapper>      
 </decorator-mappers>
</sitemesh>


再加上main_layout.jsp模板文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<head>
 <title><decorator:title default="Welcome to HITACHI" /></title>
</head> 
<body> 
<div id="mainhead">
  <span>head content</span>
</div>
 
    <!-- main-content-->  
    <decorator:body />  
   <!-- end-content --> 
 
 
<div id="mainfoot">
  <span>footer content</span>
</div>

</body>
</html>
这样所需要的元素就齐了

一个test.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>

  This is Test content!

</body>
</html>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值