SiteMesh简单应用

1.在javaweb中加入 sitemesh-2.4.2.jar
2.在webroot下建立decorators 文件夹
3.建立 装饰器文件
leftRight.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/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><decorator:title default="装饰器页面"/></title>
    <decorator:head/>
  </head>
  
  <body>
    <div>
    		<ul style="list-style: none">
    			<li style="float: left;width:100px;height:180px;border-right:solid 1px gray;padding-left:10px;text-align:left;">
    				<font style="font-weogjt:bold;">歌曲列表</font>
    				<br/><br/>
    				<a href="text.jsp">浪子心声</a><br/>
    				<a href="#">如果有一天</a><br/>
    				<a href="#">跟我两辈子</a><br/>
    				<a href="#">流浪</a><br/>
    				<a href="#">天意</a><br/>
    				<a href="#">爱火烧不尽</a><br/>
    				<a href="#">兄弟</a><br/>
    				<a href="#">暗里着迷</a><br/>
    				<a href="#">来生缘</a><br/>
    			</li>
    			<li>
    				<decorator:body/>
    			</li>
    		</ul>
    	</div>
   </body>
</html>




topbottom.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/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><decorator:title default="装饰器页面"/></title>
    <decorator:head/>
  </head>
  
  <body>
     <center>
		<div>
			<page:applyDecorator page="/test.jsp" name="leftRight"/>
		</div> 
		<hr/>
		<div>
			<decorator:body/>
		</div>    
     </center>
  </body>
</html>




test.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>使用SiteMesh框架</title>
  </head>
  
  <body>
    <center>
    	<div>
    		<span style="color:green;font-weight:bold;">
    			浪子心声<br/>
    			刘德华<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    			难分真与假人面多险诈<br/>
    			几许有共享荣华檐畔水滴不分差<br/>
    		</span>
    	</div>
    </center>
  </body>
</html>



moreSongs.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>SiteMesh框架</title>
    
  </head>
  
  <body>
     <center>
     	<div>
     		<a href="#">
     			更多歌曲点击这里<br/>
     			中国最大的音乐网站
     		</a>
     	</div>
     </center>
  </body>
</html>



5.在web-inf 下建立 decorators.xml 配置装饰器文件的修饰的请求
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
  <decorator name="topBottom" page="topBottom.jsp">
	<pattern>/*</pattern>  	
  </decorator>	
  <decorator name="leftRight" page="leftRight.jsp"/>
</decorators>


6.在web.xml中加入sitemesh的框架拦截器
<?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">
  <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>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


7.访问 http://localhost:8080/SiteMesh/moreSongs.jsp

你可能感兴趣的:(sitemesh)