Sitemesh的使用

本文详细介绍如何利用Sitemesh实现统一的前端页面布局。包括在Maven项目中引入Sitemesh依赖、配置web.xml、设置decorators.xml进行页面装饰、在JSP文件中应用装饰器等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在pom.xml加入坐标

<!-- sitemesh 依赖,前端 -->
<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>sitemesh</artifactId>
    <version>2.4.2</version>
</dependency>

2.在web.xml中添加

<!--添加sitemesh2支持 -->
<filter>
    <filter-name>SiteMeshFilter</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SiteMeshFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.在WEB-INF下添加decorators.xml文件,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 装饰器配置,装饰器或组件默认路径 -->
<decorators defaultdir="/WEB-INF/jsp/decorators">
    <!-- 不装饰的URL -->
    <excludes>
        <pattern>/login</pattern>
    </excludes>
    <!-- 主页装饰器组件 -->
    <decorator name="header" page="header.jsp" />
    <decorator name="footer" page="footer.jsp" />
    <!-- 主页装饰器 -->
    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

4.在jsp主文件中添加

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<!--。。。。省略其它代码-->
<body class="nav-md">
    <page:applyDecorator name="header" />
    <div class="right_col" role="main">
        <!--主体填充区 -->
        <decorator:getProperty property="page.body" />
    </div>
    <page:applyDecorator name="footer" />
</body>
<!--。。。。省略其它代码-->

5.其它使用

<%@ page pageEncoding="utf-8"%>
<title>主页</title>
<content tag="breadcrumb">
<li>主页</li>
</content>
<content tag="body"> <%-- 面板主体 --%>
    <img src="${APP_PATH}/static/images/welcome.gif" width="210"
        height="70" />
</content>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值