sitemesh 配置手稿

来源:互联网 发布:知柏地黄丸儿童性早熟 编辑:程序博客网 时间:2024/06/09 23:57

使用的sitemesh包是sitemesh-2.4.jar现将这个包加入到WEB-INF/lib下也可以加入sitemesh-page.tld , sitemesh-decorator.tld两个文件(没有也没有影响)然后在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"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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 放在WEB-INF根目录下,配置内容提供以下示例

<?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/decorators">    <decorator name="mai" page="mai.jsp">        <pattern>/mai.html</pattern>    </decorator></decorators>


在WebRoot下面新建一个decorators文件夹 然后将装饰页面放在该目录如图所示

mai.jsp.内容如下所示

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %><%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %><%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>My JSP 'mai.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->    <decorator:title></decorator:title>  </head>    <body>  there is a decorator begin!<hr /><decorator:body></decorator:body>hello world <hr/>  </body></html>

引入了两个标签库<decorator:title></decorator:title>用来将被装饰页面的title放在此处。<decorator:body></decorator:body>用来将被装饰页面的body内容放在此处。

在WebRoot根目录下面新建一个mai.html  示例内容如下

<!DOCTYPE html><html>  <head>    <title>maqiang</title>    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>    <body>    This is my HTML page. <br>  </body></html>
这样就ok了
原创粉丝点击