sitemesh 就是一个freme 、tils 框架 差不多的一个东西吧 就是为了统一这个网站的整体风格
不用像以前jsp 开发中的困惑 为统一风格就include,如果页面很多呐怎么搞啊……
你说是不是啊,说就我这次做项目的体验来说吧,只要领会了他的实现思路就可以很好的运用到实际开发中的!
我还是贴个比较简单的例子吧 看着很丑,没写什么样式,但是写了布局的思想啊…… 代码来了啊,下边在做解释啊
jar包自然要的啊可以下载的 就我的资源里边有的
decorators.xml web.xml 模版页mode.jsp(主要的显示页面了[top,left,mian,footer---------这个要看情况而定了])
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">
<!-- 定义ActionContextCleanUp过滤器 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<!-- 定义SiteMesh的核心过滤器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<!--FilterDispatcher-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts-configs/struts.xml</param-value>
</init-param>
</filter>
<!-- 定义过滤器链 -->
<!-- 排在第一位的过滤器是:ActionContextCleanUp过滤器。 -->
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在第二位的过滤器是:SiteMesh核心过滤器。 -->
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 排在第三位的过滤器是:FilterDispatcher过滤器。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--欢迎页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
decorators.xml ----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
<!--excludes结点则指定了哪些路径的请求不使用任何模板-->
<!--/index.jsp和凡是以/login/开头的请求路径一律不使用模板-->
<!-- 这个节点下就是不需要修饰的页面啊 也可以写目录-->
<excludes>
<pattern>/index.jsp*</pattern>
<pattern>/login/*</pattern>
</excludes>
<!--decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板-->
<decorator name="main" page="mode.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
模板页 mode.jsp------------------------------------------------------------------------------------------
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="decorator"
uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><decorator:title default="嘻嘻哈哈" />
</title>
<decorator:head />
</head>
<body id="page-home">
<div style="background-color: red;width:100%;height: 100px;">
头
</div>
<div><jsp:include page="../left.jsp"></jsp:include></div>
<div style=" position:absolute; background-color: lime; width: 300px; height:200; top:110px; left:190px;">
<!-- 这里的内容由引用模板的子页面来替换 -->
<decorator:body />
</div>
</body>
</html>
---------------------------------------------------------------------
其他的页面都随便写啊看你要什么样式了
top.jsp 我就是没写了 直接在mode.jsp 定义了一个层
left.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>My JSP 'left.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">
-->
</head>
<body>
<div style="background-color: green; width: 180px; height:100%";>
left
<b style=" background-color: black; font-family: 行书 ;font-size: medium;">
<div><a href="1.jsp">111111111111111111111</a></div>
<div><a href=2.jsp>222222222222222222222</a></div>
<div><a href="3.jsp">333333333333333333333</a></div>
<div><a href="4.jsp">444444444444444444444</a></div>
<div><a href="1.jsp">111111111111111111111</a></div>
<div><a href=2.jsp>222222222222222222222</a></div>
<div><a href="3.jsp">333333333333333333333</a></div>
<div><a href="4.jsp">444444444444444444444</a></div><div><a href="1.jsp">111111111111111111111</a></div>
<div><a href=2.jsp>222222222222222222222</a></div>
<div><a href="3.jsp">333333333333333333333</a></div>
<div><a href="4.jsp">444444444444444444444</a></div>
</b>
</div>
</body>
</html>
1.jsp 2.jsp 3.jsp 4.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>My JSP '1.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">
-->
</head>
<body>
This is my JSP page. <br>
<h1>111111111111111111111111111111111111111111111111</h1>
<br>
</body>
</html>
<%@ 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>My JSP '1.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">
-->
</head>
<body>
This is my JSP page. <br>
<h2>2222222222222222222222222222222222222222222222222222222</h2>
</body>
</html>
这个拿过去可以直接看到效果的 ,虽然很丑,但可以一目了然的,让你很快掌握sitemesh布局,这个就是免去了include,
首先<!-- 排在第二位的过滤器是:SiteMesh核心过滤器。 -->
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 所有的请求被他拦截 ,获取 title head body ,自动为body套上 模板页的样式 就是这个原理,主要是看sitemesh的配置文件,和模板页 mode.jsp !
好了,希望你们看了我的这个帖子可以很快的掌握sitemesh的用法。 谢谢大家的支持,我想只有通过自己的总结有了自己的理解让别人少走弯路,共享自己的思想,希望大家都能这样做啊,中国的软件业才会更上一层楼啊……,你们说是不是啊!