1.安装sitemesh插件
与整合其他框架类似,struts2与sitemesh框架的整合也使用了插件方式进行管理。将struts2-sitemesh-plugin-2.0.6.jar文件复制到WEB-INF/lib下,为了整合sitemesh框架,必须在web.xml中配置sitemesh过滤器,让该核心过滤器来过滤所有的用户请求。但我们知道,struts2的所有值一旦访问该stack context或ValueStack后,里面对应的数值将会被清除掉,如果先使用了struts2的FilterDispather来过滤用户请求,则sitemesh的过滤器将无法取得Stack context或者ValueStack中的数据
为了解决这个问题,struts2提供了ActionContextCleanUp类,在struts2的架构中,标准的过滤器一般以ActionContextCleanUp开始,后面跟着其他需要的过滤器,最后,由FilterDispatcher来处理请求,FilterDispatcher通常是将请求传递给ActionMapper
ActionContextCleanUp的一个重要作用是整合sitemesh页面装饰器,它通知FilterDispatcher在正确的时间清除ActionContext中的请求数据,所以正确的排序如下:
(1)ActionContextCleanUp过滤器
(2)SiteMesh核心过滤器
(3)FilterDispatcher过滤器
web.xml
<?xmlversion="1.0"encoding="GBK"?>
<web-appid="jsf"version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--��struts2�ܹ��У����Ĺ�����tһ����ActionContextCleanUp��ʼ���������������Ҫ�Ĺ����������FilterDispatcher��������-->
<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>struts</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>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

<!--JavaServerFacesServletConfiguration,notuseddirectly-->
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!--JavaServerFacesServletMapping,notcalleddirectly-->
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

</web-app>
spring配置文件
<?xmlversion="1.0"encoding="GBK"?>
<!--指定Spring配置文件的Schema信息-->
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

<beanid="bs"class="lee.service.BookService"/>

</beans>

sitemesh装饰配置文件
<?xmlversion="1.0"encoding="GBK"?>

<decoratorsdefaultdir="/decorators">
<!--在excludes元素下指定的页面将不会由SiteMesh来装饰-->
<excludes>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</excludes>

<!--创建一个名为main的装饰器,该装饰器页面为main.jsp,
用于装饰pattern指定的URL的所有页面-->
<decoratorname="main"page="main.jsp">
<pattern>/*</pattern>
</decorator>

<!--定义一个装饰器,但该装饰器默认不装饰任何页面-->
<decoratorname="panel"page="panel.jsp"/>
</decorators>
装饰器decorators/main.jsp

<%...@pagecontentType="text/html;charset=GBK"%>

<%...@tagliburi="http://www.opensymphony.com/sitemesh/decorator"prefix="decorator"%>

<%...@tagliburi="http://www.opensymphony.com/sitemesh/page"prefix="page"%>
<html>
<head>
<title><decorator:titledefault="SiteMesh的装饰器页"/></title>
<linkhref="decorators/main.css"rel="stylesheet"type="text/css">
<decorator:head/>
</head>
<body>
<tablewidth="100%"height="100%">
<tr>
<tdvalign="top">
<!--引入一个页面,临时指定所用的装饰器-->
<page:applyDecoratorpage="/book.html"name="panel"/>
<page:applyDecoratorpage="/link.html"name="panel"/>
</td>
<tdwidth="100%">
<tablewidth="100%"height="100%">
<tr>
<tdid="pageTitle">
<decorator:title/>
</td>
</tr>
<tr>
<tdvalign="top"height="100%">
<decorator:body/>
</td>
</tr>
<tr>
<tdid="footer">
<b>被包含的内容</b><br>
SithMesh提供页面装饰支持
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
装饰器decorators/panel.jsp

<%...@pagecontentType="text/html;charset=GBK"%>

<%...@tagliburi="http://www.opensymphony.com/sitemesh/decorator"prefix="decorator"%>
<p>
<tablewidth=250border=0cellpadding=0cellspacing=0>
<tr>
<thclass="panelTitle">
<decorator:titledefault="小面板页面"/>
</th>
</tr>
<tr>
<tdclass="panelBody">
<decorator:body/>
</td>
</tr>
</table>
</p>

装饰器样式decorators/main.css
body,td,p{
font:normalx-smallverdana,arial,helvetica,sans-serif;
}

.panelTitle{
background-color:#003399;
color:#eeeeee;
font-weight:bold;
border-color:#3366ff#000033#000033#3366ff;
border-width:1;
border-style:solid;
padding:1;
}

.panelBody{
background-color:#eeeeee;
border-color:black;
border-width:0111;
border-style:solid;
padding:2;
}

#pageTitle{
background-color:#003399;
color:#eeeeee;
font-weight:bold;
font-size:large;
border-color:#3366ff#000033#000033#3366ff;
border-width:1;
border-style:solid;
padding:1;
text-align:center;
}

#footer{
background-color:#eeeeee;
font-size:9pt;
text-align:center;
color:black;
border-color:#666666#cccccc#cccccc#666666;
border-width:1;
border-style:solid;
padding:1;
}

被装饰页面book.html
<html>
<head>
<title>作者图书</title>
</head>
<body>
<center>
Spring2.0宝典<br>
轻量级J2EE企业应用实战<br>
基于J2EE的Ajax宝典
</center>
</body>
</html>
被装饰页面link.html
<html>
<head>
<title>友情链接</title>
</head>
<body>
<center>
<ahref="http://www.nit-pro.org">NIT-PRO考试中心</a><br>
<ahref="http://www.oneedu.cn">新东方IT培训中心</a><br>
<ahref="http://www.oneedu.cn">东方标准人才服务公司</a><br>
</center>
</body>
</html>
JSF功能页面list.jsp

<%...@pagelanguage="java"contentType="text/html;charset=GBK"%>

<%...@taglibprefix="f"uri="http://java.sun.com/jsf/core"%>

<%...@taglibprefix="h"uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<title>Struts2+MyFaces+Spring整合</title>
</head>
<body>
<f:view>
<h3>Struts2+MyFaces+Spring整合</h3>
<h3>列出所有图书</h3>
<h:dataTablevalue="#{action.allBook}"var="b"style="text-align:center;width:500px"border="1">
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书ID"/>
</f:facet>
<h:outputLinkvalue="edit.action">
<f:paramname="editId"value="#{b.id}"/>
<h:outputTextvalue="#{b.id}"/>
</h:outputLink>
</h:column>
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书名"/>
</f:facet>
<h:outputTextvalue="#{b.name}"/>
</h:column>
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书简介"/>
</f:facet>
<h:outputTextvalue="#{b.desc}"/>
</h:column>
</h:dataTable>
<p>
<h:outputLinkvalue="edit.action">
<h:outputTextvalue="新增图书"/>
</h:outputLink>
</p>
</f:view>
</body>
</html>
JSF功能页面edit.jsp

<%...@pagelanguage="java"contentType="text/html;charset=GBK"%>

<%...@taglibprefix="f"uri="http://java.sun.com/jsf/core"%>

<%...@taglibprefix="h"uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<title>Struts2+MyFaces+Spring整合</title>
</head>
<body>
<f:view>
<h3>Struts2+MyFaces+Spring整合</h3>
<h3>修改/保存图书</h3>
<h:form>
<h:inputHiddenvalue="#{action.editId}"/>
<h:panelGridcolumns="3">
<h:outputTextvalue="图书ID"/>
<h:inputTextid="id"size="5"value="#{action.currentBook.id}"required="true"/>
<h:messagefor="id"/>
<h:outputTextvalue="图书名:"/>
<h:inputTextid="name"size="30"value="#{action.currentBook.name}"required="true">
<f:validateLengthminimum="2"maximum="100"/>
</h:inputText>
<h:messagefor="name"/>
<h:outputTextvalue="图书描述:"/>
<h:inputTextid="desc"size="30"value="#{action.currentBook.desc}"required="true">
<f:validateLengthminimum="2"maximum="100"/>
</h:inputText>
<h:messagefor="desc"/>
</h:panelGrid>
<h:commandButtonvalue="保存"action="#{action.save}"/>
<br/>
</h:form>
</f:view>
</body>
</html>
struts.xml
<?xmlversion="1.0"encoding="GBK"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constantname="struts.custom.i18n.resources"value="messageResource"/>
<constantname="struts.i18n.encoding"value="GBK"/>

<packagename="jsf"extends="jsf-default">
<interceptors>
<interceptor-stackname="jsfFullStack">
<interceptor-refname="params"/>
<interceptor-refname="basicStack"/>
<interceptor-refname="jsfStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-refname="jsfFullStack"/>
</package>

<packagename="lee"extends="jsf">
<actionname="list"class="lee.action.BookAction">
<resultname="success"type="jsf"/>
</action>
<actionname="edit"class="lee.action.BookAction">
<resultname="success"type="jsf"/>
<resultname="list"type="redirect">list.action</result>
</action>
</package>

</struts>
BookService
packagelee.service;

importjava.util.*;
importlee.model.Book;

publicclassBookService

...{
privateSet<Book>bookDb;

publicBookService()

...{
bookDb=newHashSet<Book>();
bookDb.add(newBook(1,"Spring2.0宝典","全面介绍了Spring各个知识点"));
bookDb.add(newBook(2,"轻量级J2EE企业应用实战","介绍实际企业的J2EE开发过程"));
}

publicSet<Book>getAllBook()

...{
returnbookDb;
}

publicBookgetBookById(intid)

...{
for(Bookb:bookDb)

...{
if(b.getId()==id)

...{
returnb;
}
}
returnnull;
}


publicvoidaddBook(Bookb)

...{
bookDb.add(b);
}
}
Book
packagelee.model;


publicclassBook

...{
privateintid;
privateStringname;
privateStringdesc;

publicBook()

...{
}

publicBook(intid,Stringname,Stringdesc)

...{
this.id=id;
this.name=name;
this.desc=desc;
}

publicvoidsetId(intid)

...{
this.id=id;
}
publicintgetId()

...{
returnthis.id;
}

publicvoidsetName(Stringname)

...{
this.name=name;
}
publicStringgetName()

...{
returnthis.name;
}

publicvoidsetDesc(Stringdesc)

...{
this.desc=desc;
}
publicStringgetDesc()

...{
returnthis.desc;
}

publicinthashCode()

...{
returnid;
}
publicbooleanequals(Objecttarget)

...{
if(targetinstanceofBook)

...{
Bookb=(Book)target;
if(b.getId()==this.id)

...{
returntrue;
}
}
returnfalse;
}
}
BookAction
packagelee.action;

importcom.opensymphony.xwork2.ActionSupport;

importjava.util.*;
importlee.model.Book;
importlee.service.BookService;

publicclassBookActionextendsActionSupport
{
privateBookcurrentBook;
privateinteditId;

privateBookServicebs;
publicvoidsetBs(BookServicebs)
{
this.bs=bs;
}

publicvoidsetCurrentBook(BookcurrentBook)
{
this.currentBook=currentBook;
}
publicBookgetCurrentBook()
{
//如果editId请求参数不为空,则currentBook也不为空
if(editId!=0)
{
this.currentBook=bs.getBookById(editId);
}
elseif(currentBook==null)
{
currentBook=newBook();
}
returnthis.currentBook;
}

publicvoidsetEditId(inteditId)
{
this.editId=editId;
}
publicintgetEditId()
{
returnthis.editId;
}

publicList<Book>getAllBook()
{
List<Book>result=newArrayList<Book>();
for(Bookb:bs.getAllBook())
{
result.add(b);
}
returnresult;
}

publicStringsave()
{
bs.addBook(currentBook);
return"list";
}

}
struts.properties
struts.i18n.encoding=gb2312
struts.objectFactory.spring.autoWire=type
如果web应用是test.,则运行
http://localhost:8080/test/list.action 则会出现如下被装饰过的页面

版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
不用多说了,Struts 2权威指南--基于WebWork核心的MVC开发(高清完整版),解压出来有200多M,因为权限不怎么够,我一共分了12卷,是一本不可多得的好书。第一卷附目录: 第1章 Struts 2概述,第2章 Struts 2下的Hello...
"Struts+JSF+filter+Myfaces+A4j+Spring+hibernate+Mysql整合一个项目"就是一个典型的例子,它涉及到前端展现、业务逻辑处理、数据持久化以及数据库管理等多个层面。以下是对这些技术的详细说明: **Struts**:...
JSF+Spring+Ibatis示例,对学习JAVA企业应用开发有巨大的帮助!
Struts、Spring、Hibernate 和 JavaServer Faces (Jsf) 是Java Web开发中四个非常重要的框架。这四个框架的集成使用可以构建出高效、模块化且易于维护的企业级应用。 Struts 是一个开源的MVC(Model-View-...
在这个"jsf+hibernate+spring集成案例"中,我们将看到: 1. **配置集成**:首先,我们需要在Spring配置文件中定义数据源、SessionFactory(Hibernate的核心组件)以及JSF所需的Managed Beans。这通常涉及到XML配置...
**Ajax、JSF、Spring和Hibernate是四种在Java Web开发中广泛应用的技术,它们共同构建了高效、灵活且功能强大的Web应用程序。** **Ajax(Asynchronous JavaScript and XML)** 是一种在无需重新加载整个网页的情况...
【JSF+Spring+Hibernate整合开发】 JSF (JavaServer Faces)、Spring 和 Hibernate 是 Java 开发中的三大核心技术,常用于构建企业级的 Web 应用程序。它们各自扮演着不同的角色,共同构建了一个强大的后端架构。 1...
**JSF+Spring+Hibernate整合应用详解** 在Java Web开发中,JSF(JavaServer Faces)、Spring和Hibernate是三个常用的技术栈,它们分别负责视图层、业务逻辑层和服务数据持久化层。这个"JSF+Spring+Hibernate小例子...
在IT行业中,构建高效、可扩展的Web应用程序是至关重要的,而"jsf+spring+hibernate+ajax4jsf"的组合提供了一个强大的框架集合来实现这一目标。这个整合涉及四个关键组件:JavaServer Faces (JSF),Spring框架,...
将JSF与Spring集成可以让开发者充分利用两者的优点,实现更高效、灵活的应用开发。 集成JSF和Spring的核心在于使两个框架能够共享Bean并协调工作。由于它们都遵循Servlet规范,可以通过Servlet上下文...
SWF为JSF提供了更强大的流程控制能力,而SpringFaces则是Spring社区为了更好地集成JSF而创建的项目。 **4. 整合步骤** - 配置Spring:在Web应用的`web.xml`中配置Spring的DispatcherServlet,并添加Spring的上下文...
Spring 3.0.5是Spring框架的一个稳定版本,它支持JSF集成,允许开发者利用Spring的强大功能来处理服务层和数据访问层的逻辑。Spring的IoC(Inversion of Control,控制反转)容器使得对象之间的依赖关系可以通过配置...
1)JSP+javabean+DAO(Ajax:anywhere) 2)Struts+spring+hibernate3(AJax:DOJO) 3)JSF+richfaces+seam+EJB 总共3个完整的实例,并配有需求分析~~~~~~~~,绝对经典!
《Struts2权威指南》是李刚所著的一本详细解析Struts2框架的书籍,其完整版涵盖了Struts2的核心概念、配置、实践应用以及与其他流行技术如Spring、JSF、Hibernate的整合。 首先,让我们深入理解Struts2的基础知识。...
### Struts2 权威指南 #### 一、引言 **Struts2**作为一款优秀的开源框架,为Java Web应用程序提供了强大的支持。它继承和发展了Struts1的优点,并吸收了WebWork等其他优秀框架的设计理念,使得Struts2成为当今最...
细说如何整合spring+hibernate +jsf,细说如何整合spring+hibernate +jsf,细说如何整合spring+hibernate +jsf,细说如何整合spring+hibernate +jsf,细说如何整合spring+hibernate +jsf,
根据提供的标题、描述、标签及部分内容,我们可以总结出以下与Java、Java EE、Struts2、Spring、Hibernate相关的学习知识点: ### Java基础 - **Java语言特性**:介绍Java的基本语法,包括变量、数据类型、流程...
2. **集成 Spring**:通过在 JSF 的 Managed Beans 中注入 Spring 的 Bean,可以利用 Spring 的依赖注入特性。这需要配置 Spring 的上下文文件,声明 Bean 及其依赖,并启用 JSF-Spring 桥接器。 3. **整合 ...
以下是对"JSF+Spring+Hibernate"整合的详细过程的阐述: 1. **JavaServer Faces (JSF)**:JSF是一种基于组件的MVC(模型-视图-控制器)框架,主要用于构建企业级的Web应用程序。它提供了一套预定义的UI组件,使得...