【Spring】Spring Framework Reference Documentation中文版21

来源:互联网 发布:数控编程实例 编辑:程序博客网 时间:2024/05/18 01:38

24. Integrating with other web frameworks

集成其他的web框架

 

24.1 Introduction

引入

 

Spring Web Flow

 

Spring Web Flow (SWF) aims to be the best solution for the management of web application page flow.

swf目标是最好的解决方案用于管理web应用的页面流。

 

SWF integrates with existing frameworks like Spring MVC and JSF, in both Servlet and Portlet environments. If you have a business process (or processes) that would benefit from a conversational model as opposed to a purely request model, then SWF may be the solution.

swf集成了已有的框架类似于springmvcjsf,在Servlet和可移动的环境中。如果你有业务需要将从中获益来自对话模型作为纯请求模型的对面,swf可以是一种解决方案。

 

SWF allows you to capture logical page flows as self-contained modules that are reusable in different situations, and as such is ideal for building web application modules that guide the user through controlled navigations that drive business processes.

swf允许你来捕获逻辑页面流作为自包含模块可以重用在不同的情况,并且用于构建web应用模块指导用户通过控制导航来处理业务程序。

 

For more information about SWF, consult the Spring Web Flow website.

对于更多有关swf的信息,参考swf的官方网站。

 

This chapter details Springs integration with third party web frameworks, such as JSF.

这个章节的细节是spring集成第三方web框架,例如JSF

 

One of the core value propositions of the Spring Framework is that of enabling choice. In a general sense, Spring does not force one to use or buy into any particular architecture, technology, or methodology (although it certainly recommends some over others). This freedom to pick and choose the architecture, technology, or methodology that is most relevant to a developer and their development team is arguably most evident in the web area, where Spring provides its own web framework (Spring MVC), while at the same time providing integration with a number of popular third party web frameworks. This allows one to continue to leverage any and all of the skills one may have acquired in a particular web framework such as JSF, while at the same time being able to enjoy the benefits afforded by Spring in other areas such as data access, declarative transaction management, and flexible configuration and application assembly.

其中一个核心的价值主张来自spring就是允许选择。通常情况,spring不强制用户使用或绑定域特定的架构、技术活方法论(尽管他推荐一些)。这是自由选择和选择架构、技术和方法论相对于开发者和他们的开发组是可论证的在web领域,当spring提供了他自己的web框架(springmvc),在同时也提供了和第三方web框架的集成。这允许继续影响所有的技能需要获得来自特定的web框架例如JSF,在同时享受其他spring带来的好处在一些领域如数据访问、声明式事务管理和方便配置应用。

 

Having dispensed with the woolly sales patter (c.f. the previous paragraph), the remainder of this chapter will concentrate upon the meaty details of integrating your favorite web framework with Spring. One thing that is often commented upon by developers coming to Java from other languages is the seeming super-abundance of web frameworks available in Java. There are indeed a great number of web frameworks in the Java space; in fact there are far too many to cover with any semblance of detail in a single chapter. This chapter thus picks four of the more popular web frameworks in Java, starting with the Spring configuration that is common to all of the supported web frameworks, and then detailing the specific integration options for each supported web framework.

去除了无用的内容(之前的图表),这个章节剩余的部分将关注于有用的细节关于集成你的web框架通过spring。一件事就是评论通过开发者来自Java来自其他语言是多余的框架在Java中。这是很多的web框架在Java领域,实际上有覆盖了多个细节在单一的章节中。这一章节包含了四个主流的web框架在java领域,开始spring的配置通常是支持spring的并且有特定的集成选项对于每个支持的框架。

 

[Note]

注意

 

Please note that this chapter does not attempt to explain how to use any of the supported web frameworks. For example, if you want to use JSF for the presentation layer of your web application, the assumption is that you are already familiar with JSF itself. If you need further details about any of the supported web frameworks themselves, please do consult Section 24.6,Further Resourcesat the end of this chapter.

请注意这一章节没有试图解释如何使用任何支持的web框架。例如,如果你希望使用JSF用于web应用的表现层,你需要首先熟悉JSF本身。如果你需要更多有关支持的框架的内容,见参考24.6章节,“更多资源”在本章节的末尾。

 

24.2 Common configuration

通用的配置

 

Before diving into the integration specifics of each supported web framework, let us first take a look at the Spring configuration that is not specific to any one web framework. (This section is equally applicable to Springs own web framework, Spring MVC.)

在进入集成之前对于每个支持的特定框架,让我们首先看一下spring的配置是不针对某个web框架的。(这个章节应用于spring自己的web框架,spring mvc)。

 

One of the concepts (for want of a better word) espoused by (Springs) lightweight application model is that of a layered architecture. Remember that in a 'classic' layered architecture, the web layer is but one of many layers; it serves as one of the entry points into a server side application and it delegates to service objects (facades) defined in a service layer to satisfy business specific (and presentation-technology agnostic) use cases. In Spring, these service objects, any other business-specific objects, data access objects, etc. exist in a distinct 'business context', which contains no web or presentation layer objects (presentation objects such as Spring MVC controllers are typically configured in a distinct 'presentation context'). This section details how one configures a Spring container (a WebApplicationContext) that contains all of the 'business beans' in ones application.

内容之一(为了一个更好的说明)通过spring的轻量级应用模型是分层的结构。记住典型的层次结构,web层是许多层之一,他作为一点服务于服务器的应用并且他委托给服务object定义在服务层中处理特定的业务(和表现技术)。在spring中,这些服务object、任何其他的业务object、数据访问object等等存在于不同的业务上下文,包含非web或表现层object(表现层object例如spring mvc的控制器是通常配置在独立的表现上下文中)。这个章节的细节展示了如何配置一个spring的容器(web的上下文)包含所有应用中的业务bean

 

On to specifics: all that one need do is to declare a ContextLoaderListener in the standard Java EE servlet web.xml file of ones web application, and add a contextConfigLocation<context-param/> section (in the same file) that defines which set of Spring XML configuration files to load.

关于细节:所有需要做的是定义一个ContextLoaderListener在标准的JavaEEweb应用的Servletweb.xml文件中,并且添加一个contextConfigLocation<context-param/>(在相同的文件中)定义需要加载的springxml配置文件的集合。

 

Find below the <listener/> configuration:

发现下面<listener/>配置:

 

<listener>

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

</listener>

 

Find below the <context-param/> configuration:

发现下面<context-param/>配置:

 

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/applicationContext*.xml</param-value>

</context-param>

 

If you dont specify the contextConfigLocation context parameter, the ContextLoaderListener will look for a file called /WEB-INF/applicationContext.xml to load. Once the context files are loaded, Spring creates a WebApplicationContext object based on the bean definitions and stores it in the ServletContext of the web application.

如果你没有定义contextConfigLocation的上下文参数,ContextLoaderListener将查找文件名字为/WEB-INF/applicationContext.xml来加载。一旦上下文文件被加载,spring创建一个WebApplicationContextobject基于bean定义并且将其存储在web应用的ServletContext中。

 

All Java web frameworks are built on top of the Servlet API, and so one can use the following code snippet to get access to this 'business context' ApplicationContext created by the ContextLoaderListener.

所有的Javaweb框架都构建在ServletAPI之上,并且可以使用下面的代码片段用于获取访问业务上下文的由ContextLoaderListener创建的ApplicationContext

 

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

 

The WebApplicationContextUtils class is for convenience, so you dont have to remember the name of the ServletContext attribute. Its getWebApplicationContext() method will return null if an object doesnt exist under the WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE key. Rather than risk getting NullPointerExceptions in your application, its better to use the getRequiredWebApplicationContext() method. This method throws an exception when the ApplicationContext is missing.

WebApplicationContextUtils是方便的工具类,因此你不需要记住ServletContext属性的名字。他的getWebApplicationContext方法将返回null如果object不存在于WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE关键字的话。这样你在应用中就会有空指针的风险,最好使用getRequiredWebApplicationContext方法。这个方法会抛出一个异常如果ApplicationContext不存在的话。

 

Once you have a reference to the WebApplicationContext, you can retrieve beans by their name or type. Most developers retrieve beans by name and then cast them to one of their implemented interfaces.

一旦你有了WebApplicationContext的引用,你可以获得bean通过他们的名字或类型。大部分开发者通过名字获得bean并且将他们转换为实现的接口。

 

Fortunately, most of the frameworks in this section have simpler ways of looking up beans. Not only do they make it easy to get beans from a Spring container, but they also allow you to use dependency injection on their controllers. Each web framework section has more detail on its specific integration strategies.

幸运的是,大部分框架在这个章节中有简单的方法关于查找bean。不只是使得访问spring容器中的bean更加简单,但是也允许你使用依赖注入在他们的控制器上。每个web框架章节由关于特定集成策略的细节。

 

24.3 JavaServer Faces 1.2

 

JavaServer Faces (JSF) is the JCPs standard component-based, event-driven web user interface framework. As of Java EE 5, it is an official part of the Java EE umbrella.

JSFJCP的标准基于组件、事件驱动的web用户接口框架。由于JavaEE5,他是JavaEE官方的一部分。

 

For a popular JSF runtime as well as for popular JSF component libraries, check out the Apache MyFaces project. The MyFaces project also provides common JSF extensions such as MyFaces Orchestra: a Spring-based JSF extension that provides rich conversation scope support.

对于一个流行的JSF运行时用于JSF组件库,检查Apache MyFaces项目。MyFaces项目也支持通用的JSF扩展例如MyFaces Orchestra:一个基于springJSF扩展提供了方便的范围支持。

 

[Note]

注意

 

Spring Web Flow 2.0 provides rich JSF support through its newly established Spring Faces module, both for JSF-centric usage (as described in this section) and for Spring-centric usage (using JSF views within a Spring MVC dispatcher). Check out the Spring Web Flow website for details!

springweb2.0提供了JSF支持通过他新建立的springFaces模块,对于以JSF中心的使用(描述在这个章节中)和特定的spring使用(使用springmvcdispatcher配合JSF视图)。检查springweb流网站来了解更多的细节。

 

The key element in Springs JSF integration is the JSF ELResolver mechanism.

springJSF集成的关键元素是JSFELResolver策略。

 

24.3.1 SpringBeanFacesELResolver (JSF 1.2+)

 

SpringBeanFacesELResolver is a JSF 1.2 compliant ELResolver implementation, integrating with the standard Unified EL as used by JSF 1.2 and JSP 2.1. Like SpringBeanVariableResolver, it delegates to the Springs 'business context' WebApplicationContext first, then to the default resolver of the underlying JSF implementation.

SpringBeanFacesELResolver是一个JSF1.2的符合ELResolver的实现,集成了标准的统一的EL并且使用了JSF1.2JSP2.1。就像SpringBeanVariableResolver,他委托给spring的业务上下文的WebApplicationContext,然后对于默认的解析器在JSF实现中。

 

Configuration-wise, simply define SpringBeanFacesELResolver in your JSF 1.2 faces-context.xml file:

配置简单定义SpringBeanFacesELResolver在你的JSF1.2faces-context.xml文件中:

 

<faces-config>

    <application>

        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

        ...

    </application>

</faces-config>

 

24.3.2 FacesContextUtils

 

A custom VariableResolver works well when mapping ones properties to beans in faces-config.xml, but at times one may need to grab a bean explicitly. The FacesContextUtils class makes this easy. It is similar to WebApplicationContextUtils, except that it takes a FacesContext parameter rather than a ServletContext parameter.

一个自定义的VariableResolver工作当匹配一个属性对于beanfaces-config.xml中,但是同时也需要明确获得一个beanFacesContextUtils类使得他更加简单。相似于WebApplicationContextUtils,期望他有一个FacesContext参数而不是一个ServletContext参数。

 

ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

 

24.4 Apache Struts 2.x

 

Invented by Craig McClanahan, Struts is an open source project hosted by the Apache Software Foundation. At the time, it greatly simplified the JSP/Servlet programming paradigm and won over many developers who were using proprietary frameworks. It simplified the programming model, it was open source (and thus free as in beer), and it had a large community, which allowed the project to grow and become popular among Java web developers.

Craig McClanahan发明,Struts是一个开源的项目对于Apache Software Foundation。同时他十分简单对于JSP/Servlet编程并且赢得了众多开发者使用这个框架。他简单定义了编程模式,是开源的(是免费使用的)并且他有一个庞大的社区,允许项目成长并且在Javaweb开发者中间很流行。

 

Check out the Struts Spring Plugin for the built-in Spring integration shipped with Struts.

检查Strutsspring插件对于内置的spring集成片段和Struts

 

24.5 Tapestry 5.x

 

From the Tapestry homepage:

Tapestry的主页面:

 

Tapestry is a "Component oriented framework for creating dynamic, robust, highly scalable web applications in Java."

Tapestry是一个“面向组件的框架用于创建动态、强健、大规模的javaweb应用”。

 

While Spring has its own powerful web layer, there are a number of unique advantages to building an enterprise Java application using a combination of Tapestry for the web user interface and the Spring container for the lower layers.

spring有其自己强大的web层,有一些唯一的特性来构建一个企业级Java应用使用Tapestry用于web用户接口并且spring容器用于底层。

 

For more information, check out Tapestrys dedicated integration module for Spring.

关于更多的信息,关注有关Tapestryspring集成的模块。

 

24.6 Further Resources

更多的资源

 

Find below links to further resources about the various web frameworks described in this chapter.

下面的超链接中有关于不同web框架描述的资源。

 

    The JSF homepage

    The Struts homepage

    The Tapestry homepage

 

 

阅读全文
0 0
原创粉丝点击