`
lanceverw
  • 浏览: 44205 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

SiteMesh with Struts2 and Maven

阅读更多

About SiteMesh:

SiteMesh is a web-page layout and decoration framework and web application integration framework to aid in creating large sites consisting of many pages for which a consistent look/feel, navigation and layout scheme is required.

 

配置步骤:

1. 在项目中加入SiteMesh依赖,在项目pom.xml文件中加入如下依赖:

<dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-sitemesh-plugin</artifactId>
        <version>2.3.4.1</version>
</dependency>

 这里使用的是struts2提供的SiteMesh插件,在Maven中会自动解析依赖并加入sitemesh2.4.2.jar包,另外需要确认插件和sitemesh.jar包都放置到了/WEb-INF/lib下。

2. 配置web.xml:

按照以下代码配置filter和listener

<filter>
    <filter-name>struts-prepare</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
</filter>

<filter>
    <filter-name>struts-execute</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
</filter>


<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts-prepare</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>struts-execute</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>
 

一定要注意filter-mapping的配置顺序,顺序不正确会导致sitemesh无法生效。

3. 加入SiteMesh配置文件:

在WEB-INF文件中新建decorators.xml, 这里顺便吐槽下SiteMesh官网的示例,官网上有一处写成了decorator.xml,我拷贝至项目中导致服务启动后一直报错,无法找到配置文件,因此正确的文件名是decorators.xml,希望对遇到这个问题的兄弟有帮助。

<?xml version="1.0" encoding="utf-8"?>

<!-- defaultdir指定装饰器文件所在的路径 -->

<decorators defaultdir="/layout">

	<!--excludes结点则指定了哪些路径的请求不使用任何模板 -->

	<excludes>

		<pattern>/entpLogin.jsp</pattern>

	</excludes>

	<!--decorator结点指定了模板的位置和文件名,通过pattern来指定哪些路径引用哪个模板 -->

	<decorator name="main" page="mode.jsp">

		<pattern>/*</pattern>

	</decorator>

</decorators

 4. 编写SiteMesh模板:

在项目中建立与decorators.xml配置文件中defaultdir="/layout"对应的layout目录,将模板文件放在这个文件夹中,示例模板文件mode.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

	pageEncoding="UTF-8"%>

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"

	prefix="decorator"%>

<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!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">

<link rel="stylesheet" type="text/css" href="css/style.css"

	media="screen">

<title>

   <decorator:title default="Welcome" /> <!--被装饰页面的Title-->

</title>

<head />

   <decorator:head /><!--被装饰页面的head-->

</head>

<body>

   <decorator:body /><!--被装饰页面的body-->

</body>

</html>

 5. 完成和参考:

根据一位兄弟的经验,如果在页面中增加了ajax主题的标签(局部刷新),则一定要在exclude pattern中添加请求的action名,否则局部刷新得到的innerHtml也会被添加上header导航条footer等装饰元。

 

SiteMesh配置基本就完成了,更深入的内容在今后讨论:

参考文献:

SiteMesh官方文档

Struts2 SiteMesh插件帮助文档

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics