sitemesh框架对页面进行布局和装饰

在实际看法中每写出的页面都需要页头和页脚,我们之前使用include将页面引用过来,这样如果页面一多得话,引用起来非常麻烦,所以这里使用sitemesh框架可以在页面中不用任何引入就可以加上页头页脚,这简化了我们的开发,sitemesh框架使用拦截器的思想来实现的,以后做开发只关注页面有什么数据,而网页的公共部分由装饰器来装饰装饰了之后再输出。下面是sitemesh框架的工作原理:



使用sitemesh的4个步骤:

1.导入架包,sitemesh-2.4.1.jar

2.在web.xml文件里边配置拦截器

3. sitemesh对谁进行装饰,要将decorators.xml配置文件加到工程中

4.写模板文件


web.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern> //拦截所有请求
  </filter-mapping>
</web-app>


decorators.xml文件的配置如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">//模板的所在文件夹
    <decorator name="main" page="template.jsp">//模板文件
        <pattern>/*</pattern>    //对所有页面进行装饰
    </decorator>
</decorators>

template.jsp模板:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator"  prefix="decorator"%>
<html>
  <head>
	<title><decorator:title/></title>
  </head>
  <body>
     header.....<br/>
	 <decorator:body/>
     footer....<br/>
  </body>
</html>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傅荣康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值