01 使用sitemesh完成页面中的“AOP”

所谓页面的“AOP”是指在页面中导入一些js和css依赖时不必显式导入。本节将阐述一个有趣的技术sitemesh,它可以完成页面当中的“AOP”。

1、操作步骤

        <dependency>
            <groupId>org.sitemesh</groupId>
            <artifactId>sitemesh</artifactId>
            <version>3.0.1</version>
        </dependency>
  • 在项目/src/main/webapp/文件夹下引入jquery-1.10.2.js或者其他js
  • 在项目/src/main/webapp/sitemesh文件夹下创建index.jsp
<html>
<head>
    <script type="text/javascript">
        $(function () {
            alert("sitemesh起作用了")
        })
    </script>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html>
  • 在项目/src/main/webapp/sitemesh文件夹下创建head.jsp
<script type="text/javascript" src="/jquery-1.10.2.js"></script>
  • 在项目/src/main/webapp/sitemesh文件夹下创建decorator.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><sitemesh:write property="title"/></title>
    <style type="text/css">
        body {
            font-size: 12px;
        }
    </style>
    <sitemesh:write property="head"/>
    <jsp:include page="/sitemesh/head.jsp"/>
</head>
<body>
</body>
</html>
  • 修改项目/src/main/webapp/WEB-INF/web.xml
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/sitemesh/*</url-pattern>
  </filter-mapping>
  • 在/src/main/webapp/WEB-INF添加sitemesh3.xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemesh>
    <mapping path="/sitemesh/*" decorator="/sitemesh/decorator.jsp"/>
</sitemesh>
  • 测试
    启动项目,访问index.jsp,会看到弹出“sitemesh起作用了”。
    以上就是使用sitemesh完成页面中的“AOP”。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值