Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> SiteMesh入门示例

SiteMesh入门示例

87170 开源 | 2014-7-9

官网:http://wiki.sitemesh.org/wiki/display/sitemesh/Home
也可以下载官方的示例Demo参考和学习,这里我只做一个简单示例,演示最基本的使用

 

首先就是加Jar包,我用的是sitemesh-2.4.2.jar,然后在web.xml中增加过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <filter>
  <filter-name>sitemesh</filter-name>
  <filter-class>
   com.opensymphony.module.sitemesh.filter.PageFilter
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>sitemesh</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

 

增加SiteMesh配置文件decorators.xml,该文件放在WEB-INF下:

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/layouts/">
 <!-- 不需要过滤的请求 -->
 <excludes>
  <pattern>/static/*</pattern>
  <pattern>/remote/*</pattern>
 </excludes>
 <!-- 定义装饰器要过滤的页面 -->
 <decorator name="default" page="default.jsp">
  <pattern>/*</pattern>
 </decorator>
</decorators>

 

在根目录下新建文件夹layouts,然后新建三个JSP,一个是默认,一个输出头,一个输出尾,默认页面引用其他两个。
默认页面default.jsp:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>SiteMesh示例-<sitemesh:title/></title>
<sitemesh:head/>
</head>
<body>
 <%@ include file="/layouts/header.jsp"%>
  <div id="content">
   <sitemesh:body/>
  </div>
 <%@ include file="/layouts/footer.jsp"%>
</body>
</html>

简单说明:
引入了SiteMesh标签。
<sitemesh:title/> 会自动替换为被过滤页面的title。
<sitemesh:head/> 会把被过滤页面head里面的东西(除了title)放在这个地方。
<sitemesh:body/> 被过滤的页面body里面的内容放在这里。
头部引入js和css,都可以在其他重用。

 

头页面header.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
菜单信息

 

尾页面footer.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
版权信息

 

在根下新建一个文件夹static,用于实验是否拦截,在该文件夹下新建JSP:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()
+ ":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>有人拦截我吗?</title>
  </head>
  <body>
    有人拦截我吗?
  </body>
</html>

 

访问:http://127.0.0.1:8080/sitemesh/index.jsp这个会拦截
访问:http://127.0.0.1:8080/sitemesh/static/index.jsp则不会拦截处理
根据页面看实际效果

 

点击下载附件

推荐您阅读更多有关于“ sitemesh 框架 页面布局 开源 入门 ”的文章

上一篇:配置Jboss不输出:Parameters: Invalid chunk ignored 下一篇:Enterprise Manager 无法连接到数据库实例

猜你喜欢

发表评论: