项目中需要做界面处理,于是研究了一下SITEMESH,发现其还是相当的强大,大大便利了开发.
(主页:http://www.opensymphony.com/sitemesh/)
往工程中导入所需要的包:sitemesh-2.4.1.jar
一.配置web.xml,添加
<!-- sitemesh -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<!-- sitemesh -->
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二.在WEB-INF目录下加入文件decorators.xml,sitemesh.xml
两个文件也就是所说的装饰器配置文件,
我的decorators.xml
<!-- not menu content jsp -->
<decorator name="main_index" page="/web/template/main_layout.jsp">
<pattern>*</pattern>
</decorator>
<!-- menu content jsp -->
<decorator name="main_menu" page="/web/template/main_menu_layout.jsp" />
<!-- do not need template -->
<decorator name="blank" page="/web/template/blank.jsp" />
指定了三个模板,默认为main_layout.jsp,匹配所有的URL,当然不是所有的页面都是用同一个模板,所以
因此在具体的页面head中再从新指定模板<meta name="decorator" content="main_menu" />,这样既可统一配置又灵活指定,强大呀,赞一个.
sitemesh.xml文件
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml" />
<excludes file="${decorators-file}" />
<page-parsers>
<parser content-type="text/html"
class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
<parser content-type="text/html;charset=utf-8"
class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" /><!--meta name="decorator" content="main_menu" 才起作用-->
<param name="property.2" value="decorator" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
再加上main_layout.jsp模板文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<head>
<title><decorator:title default="Welcome to HITACHI" /></title>
</head>
<body>
<div id="mainhead">
<span>head content</span>
</div>
<!-- main-content-->
<decorator:body />
<!-- end-content -->
<div id="mainfoot">
<span>footer content</span>
</div>
</body>
</html>
这样所需要的元素就齐了
一个test.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
This is Test content!
</body>
</html>