今天自学了个不错的整理页面的小框架:sitemesh.它可以实现页面布局简单化.下面贴出它的入门:
首先需要下载sitemesh的jar包,地址是
http://www.opensymphony.com/sitemesh/download.html,我下了他的最新版本2.3.然后从下载的文件中找出sitemesh.jar和它的2个tld文件,丢到web-inf下.
下一步,在web.xml中添加过滤器
<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>
假如是和struts2一起进行使用,需要再添加struts2的过滤器,这里顺序必须十分注意:
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
接下来再从下载的文件中找出2个模版,sitemesh.xml和decorators.xml,放到web-inf下,随后在webcontent下建立decorators的文件夹,里面放的是sitemesh的模版:
<%@ page contentType="text/html; CHARSET=utf8" pageEncoding="utf-8"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page" %>
<html>
<head>
<title><decorator:title default="welcome" /></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<decorator:head />
</head>
<body id="page-home"
<div id="page-total">
<div id="page-header">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div class="topFunc">
登陆|退出
</div>
</td>
</tr>
</table>
</div>
</div>
<div id="page-menu" style="margin-top: 8px; margin-bottom: 8px;">
<div>
菜单
</div>
</div>
<div id="page-content" class="clearfix">
<center>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<decorator:body />
</td>
</tr>
</table>
</center>
</div>
<div id="page-footer" class="clearfix">
底部
</div>
</body>
</html>
下面就可以写自己的网页了,比如我写个简单的:
<html>
<head>
<script type="text/javascript" src="js/index.js"></script>
<title>abc</title>
</head>
<body>
哈哈
</body>
</html>
<head>中的内容就会加到<decorator:head/>中,body一样.
下面就要说到sitemesh的一个比较严重的缺点,即中文问题.有3个方面,一个是模版中的乱码,这个需要在Servletfilter中对response也进行设置编码,第二个是jsp页面中的乱码,可以在页首加上<%@ page contentType="text/html;charset=utf-8"%>来解决,第三个是html的乱码,这个我到现在还没有找到比较可行的方法,希望哪位大侠可以告诉我.
[/size]