SEAM IN ACTION 第一章

Part 1 Teeing off with Seam
开球

Many excellent frameworks exist to support the development of webbased
Java applications. Chapter 1 presents Seam and explains how it manages to
stand above this crowd by incorporating all of your existing Java Enterprise
experience into an innovative and modernized rendition of the Java EE platform.
You learn how Seam uncovers the platform’s tremendous capabilities, buried
underneath layers of complexity for more than a decade, through the use of
annotations, interceptors, and configuration by exception. EJB 3 components,
Groovy scripts, and anything in between can participate in this lightweight, POJObased
programming model. After this introduction, you are taken through a
Seam example, emphasizing how Seam removes infrastructure code and allows
components to focus on pure business logic. The chapter also highlights ways in
which Seam improves the development process, getting you to your target sooner.

基于WEB的JAVA现在有很多优秀的框架。第一章会展示一下SEAM并解释为什么它会鹤立鸡群,使你的开发经验结合到先进的Java EE平台。你将看到SEAM超强的能力,应用annotations, interceptors, and configuration by exception. EJB 3 components,Groovy scripts及轻量的基于POJO的编程模型,封装了底层的细节。SEAM去除了繁杂的底层代码,让部件专注于业务逻辑。本章也强度SEAM对开发过程的改进,使你能快速实现目标。

In today’s fast-paced world, we often have to show results before completely
understanding what we are doing. To help you get started, chapter 2 highlights
Seam’s project generator tool and shows you how to use it to create a functional,
database-oriented application without any coding involved. You are given a
glimpse of a Seam project’s structure and get a chance to feel out the development
cycle by making a few customizations. While you won’t have a lot of opportunity
to write code in part 1, it will build up enough anticipation to prepare you
to take on the commitment of learning a new framework. The best part is, you
will have plenty of time to do so since your boss will be drooling over the application
you create in the second chapter. That same application also serves as a
working model for you as you explore Seam.

当今这快速的世界中,我们常要在完全理解我们在做的事之前就要先列出结果。第二章强调SEAM的项目生成工具,展示如何不敲一行代码就可以建立具有一定功能的,面向数据库的应用。了解一下SEAM项目的结构及开发周期。


======
1、Seam unifies Java EE

This chapter covers
■ Lightweight Enterprise Java
■ Seam as an application stack
■ Simplified configuration using annotations
■ Tools that enable agile development

一统JAVA

本章包括:
轻量企业框架
包含各类应用
用annotations来简化配置
快速开发的工具


Is JSF worth a second look? Is EJB really fixed? Is it worth sticking with Java rather
than jumping ship for Ruby on Rails?

JSF还理不理?EJB合不合适?还要不要坚守JAVA阵地?

With the release of Seam 2.0, you can now confidently answer yes to all of these
questions. Seam is a progressive application framework for the Java Platform, Enterprise
Edition (Java EE) that makes writing web-based applications simple by finally
delivering on the promise of a unified component architecture. Seam builds on the
innovative changes in Java EE 5 brought about primarily by the Enterprise JavaBeans
(EJB) 3 specification. These changes include favoring annotations over container
interfaces and relying on configuration by exception rather than verbose and laborious
XML descriptors. Seam tears down Java EE’s remaining heavyweight legacy by
spreading EJB 3’s pivotal changes across the platform. Seam also extends the platform
as designed by weaving additional functionality into the JavaServer Faces (JSF)
life cycle and taps into the unified Expression Language (EL) to allow a wide range
of technologies to communicate. With Seam, the pain typically associated with using
Java EE has vanished and JSF, in particular, appears completely revamped and worthy
of attention.

Seam 2.0一出,就不用再顾盼神离了。SEAM是简化基于WEB开发的统一部件架构。是构建在Java EE 5的EJB3规范之上。这些先进的规范包括annotations、configuration by exception而不是XML描述符,对JSF生命周期进行了扩展,用EL来让更多的技术进行通讯。SEAM的出现结束了Java EE的苦难,使JSF也重受关注。

In this chapter, you discover why Seam is the most exciting technology in Java right
now and the reasons why you should make Seam your framework of choice. I demonstrate
how Seam solves your current problems with the Java EE platform by blending
innovation with existing standards. In a world inundated with frameworks, Seam is the
unframework. It does not prescribe a new programming model that you must adopt.
Seam simply pulls together the standard Java EE APIs, most notably EJB 3, JSF, Java Persistence
API (JPA)/Hibernate, and Java Authentication and Authorization Service
(JAAS), and makes them more accessible, functional, and attractive. Seam finishes off
these improvements with modern upgrades such as conversations, page flows, business
processes, rule-based security, JavaScript (Ajax) remoting, PDF rendering, email
composition, charting, file uploads, and Groovy integration. Like a classic car, Seam
sports the muscle of Java EE under the hood, but on the surface it appears stunning
and elegant.

SEAM说是框架也不是框架,因为它不会让你遵循新的编程模型。它只是将标准的Java EE APIs(一般指EJB3)、JSF、JPA、Hibernate、JAAS这些当前最热最熟悉的技术整合起来,另外一些先进的技术还包括conversations,page flows, business processes, rule-based security, JavaScript (Ajax) remoting, PDF rendering, email composition, charting, file uploads, and Groovy integration.


Putting Seam’s strengths aside, the fact remains that you can choose among many
qualified frameworks. In the next section, I provide you with advice that can hopefully
put an end to your search and move you toward developing your application. Despite
the fact that no one can tell you what framework is right for you, you’re probably
going to ask anyway, right? Don’t worry—I came prepared.

1.1 Which framework should I use?
In a world full of framework
options, how do you choose one?
There are so many frameworks
available for the Java platform,
some proven, some promising,
that the decision is downright
agonizing! Does figure 1.1 speak
to you?

用哪个框架?
在这个框架满天飞的时代,你怎么选呢?

The choice is so bewildering
that the framework inquiry is
now the dominant greeting
exchanged between developers
at conferences. While the question
“What do you do?” may
have traditionally served in the role of sizing up a person’s abilities, these days you are
judged based on the merit of what framework you use for software development (or
the advice that you can give pertaining to that choice). Just when you’ve made a decision,
a new framework arrives on the scene promising to bury its predecessors.

也许你刚做出选择,后浪就把前浪拍地沙滩上了。

These choices can be harmful, especially to productivity. Barry Schwartz argues in
The Paradox of Choice (Ecco, 2003) that having a bewildering array of options floods
our already exhausted brains. The result is that your ability to write a quality application
stalls. You keep believing that the best framework is the one you haven’t tried yet.
As a consequence, you spend more time researching frameworks than you do designing
functional applications. The search consumes you. You develop a false sense of how
busy you are.

If any of these choices were truly satisfying, then you probably would not be reading
this book. You would already have a set of tools that you know, beyond all doubt,
allows you to be highly productive. But you don’t, do you? You’re still searching for a
framework that is new, yet familiar. Lightweight, yet powerful. You are in need of a
platform that integrates the vast landscape of Java technologies into a unified stack.
Seam might be just the framework you are looking for.

如果你也希望找到的框架够高产,同时还够熟、够轻、够强悍,那SEAM就正是你要找的。

1.2 Choosing Seam
You might be tempted to think that Seam is just another web framework, competing
in an already flooded market. In truth, to tag Seam as a web framework is quite unfitting.
Seam is far broader than a traditional web framework, such as Struts, and is better
described as an application stack.

说SEAM是一种框架不太恰当,因为它的外延大大超出的传统的框架如Struts,一个恰当一点儿描述是“应用套餐”。

1.2.1 A complete application stack
Let’s consider the distinction between an application stack and a web framework. Web
frameworks are analogous to the guests who show up just in time for dinner and then
leave immediately after eating. They entertain and soak up the limelight, but they are
mostly unhelpful. They go out the same way they arrived: with lots of flair. An application
stack, in contrast, is like the people who help to plan the dinner party, shop for the
groceries, cook, set up, serve, make the coffee,
and then ultimately clean up when it is all over.
They are steadfast and resourceful. Sadly, their
work goes mostly unrecognized.

看看“套餐”与WEB框架的差别,WEB框架就像是晚餐前才出现,吃完就走的明星;而套餐则是和你一起准备晚餐的朋友。

In a world where everyone wants to be a rock
star (i.e., web framework), Seam is your practical
sidekick, your sous-chef. The Seam application
stack includes the framework, the libraries, the
build script and project generator, the IDE integration,
a base test class, the Embedded JBoss
container, and integrations with many technologies.
Seam is certainly a hard worker. Figure 1.2
gives a sample cross section of the technologies
that Seam is capable of pulling together in a typical
application.

SEAM套餐中有包括框架、库、编译脚本、项目生成器、IDE集成、基本测试类、嵌入式JBOSS容器,还有一些其它技术。

While this stack gives you an idea of the technologies
used in a Seam application, it does not
give you a clear picture of Seam’s purpose and

why it exists. To understand why Seam was created, you have to recognize the challenge
that it faced. Although the Java EE 5 release took a gigantic step toward establishing an
agile platform for enterprise Java development, it left behind a rather significant gap
between the component-based web tier managed by JSF and the component-based
business-tier managed by EJB 3. A bridge was needed.

为了理解为什么SEAM会出现,你要认识其所面对的挑战。虽然Java EE 5的发布前进了一大步,但其身后,在JSF与EJB3之间,却有一个很大的鸿沟,需要有一个桥梁。

1.2.2 Why Seam was created
The Java EE 5 specification incorporates two key component architectures (specifications
for creating reusable objects) for creating web-based business applications: Java-
Server Faces (JSF) 1.2 and Enterprise JavaBeans (EJB) 3. JSF is the standard
presentation framework for the web tier that provides both a user interface (UI) component
model and a server-side event model. EJB 3 is the standard programming
model for creating secure and scalable business components that access transactional
resources. EJB 3 also encompasses the Java Persistence API (JPA), which defines a standard
persistence model for translating data between a relational database and Java
entity classes.

Java EE 5引入了两个重要的用来建立基于WEB的业务应用的部件架构:Server Faces (JSF) 1.2 and Enterprise JavaBeans (EJB) 3。JSF是标准和显示层框架,同时提供UI部件及服务器端事件模型。EJB 3是标准和编程模型,可以建立安全的,易扩展的业务部件以访问事务资源。EJB 3已包含JPA,其定义了标准的数据存储模型,用来作关系数据库与JAVA实例类的对接。

Aside from their residence in the Java EE 5 specification, the two architectures just
mentioned share little resemblance, their backs facing each other like two sides of a
coin. This communication barrier casts a shadow on the tremendous potential of each
technology. While it’s true that developers are able to get these two Java EE tiers to
work together, it requires a lot of “glue” code. Seam absorbs that responsibility and fits
JSF and EJB 3 together, thus ironing out one of the roughest spots in the Java EE 5
specification and completing the missing link in the evolution of the Java EE platform.
As such, Seam has positioned itself as the prototype for future Java EE specifications.
So far, three Java Specification Requests (JSRs) have been accepted: JSR 299 (Web
Beans), JSR 314 (JavaServer Faces 2.0), and JSR 303 (Bean Validation). Seam isn’t married
to JSF or EJB 3, as figure 1.2 suggests. You can swap in alternative view technologies
such as Wicket, Tapestry, GWT, and Flex in place of JSF, though understandably
with less accord. In the business tier, Seam supports the use of JavaBeans as transactional
components and also boasts integration with the Spring container, both of
which are arguably better choices than EJB 3.

要不是在同一个规范中,这两个架构也许会老死不相往来,就像硬币的两个面一样。这种障碍为两个技术都投下了阴影。开发者是可以将两个层结合起来工作,但这要用大量的胶水代码。SEAM为我们做了所有这些接合工作。实现了三个规范:JSR 299 (Web Beans), JSR 314 (JavaServer Faces 2.0), and JSR 303 (Bean Validation)。SEAM也并不是非JSF不娶,也可用Wicket, Tapestry, GWT, and Flex来替代JSF的位置。在业务层,SEAM也可以使用JavaBeans来做事务部件,或集成Spring容器,这两个都扬言不比EJB3差。

With that said, becoming an important part of Java EE’s future and an integration
point for many open source technologies is not what sparked Seam. That’s just what
Seam has managed to accomplish. As with most software projects, Seam was started to
scratch a single developer’s itch.

THE REAL STORY
As the story (really) goes, Gavin King was fed up with developers using Hibernate
improperly by trapping it inside of the stateless design proliferated by the Spring
Framework. Recognizing that the missing integration between JSF and EJB 3 would
only lead to further abuse of Hibernate as a JPA provider, he decided to step up and
build a foundation that would allow the persistence context (Hibernate Session or
JPA EntityManager) to transcend layers and would permit stateful session beans to
respond directly to JSF UI components. To support this vision, Seam encourages the
adoption of a stateful, yet efficient, architecture. As a result, applications built on
Seam have effortless continuity from one user interaction (or event) to the next, a feature
that is labeled a web conversation. The keen focus on variable scoping is what
makes Seam contextual.

Gavin King(SEAM及Hibernate的作者)受到很多抱怨,原因是开发者不正确地在Spring框架的无状态模式下使用Hibernate。如果不集成JSF and EJB 3,则Hibernate作为JPA的实现者会进一步地被乱用。所以他决定建立新模式,让持久化上下文(Hibernate Session or JPA EntityManager)越层,让有状态session beans直接响应JSF UI部件。

The name Seam was chosen for the project because it provides a foundation that
brings JSF and EJB 3 together and teaches them to play nicely together in the same
sandbox. In the process of solving the mismatch between JSF and EJB 3, the Seam
architects broadened the solution to include any Plain Old Java Object (POJO) acting
as a business component, not just the EJB 3 variety. Seam’s universal component
model brings the implicit and declarative services provided by the EJB 3 programming
model, such as transactions, interceptors, threading, and security, to non-EJB
components such as JavaBeans and Spring beans. For non-EJB components, Seam
takes on the role of processing the Java EE 5 annotations—or synonyms of these
annotations from the Seam API—and weaves in the managed services. What this
means is that you do not have to rely on an EJB 3 container to leverage the benefits
that EJB 3 provides. You may even want to reconsider the use of EJB 3 unless you have
a specific need for it, choosing to go with JavaBeans instead. Regardless of your
choice, you aren’t required to deploy a Seam application to the JBoss Application
Server, despite what you may have heard.

对于non-EJB部件,SEAM担起了处理annotations的担子,将其纳入被管理的服务。这样你就不用依赖EJB 3容器来使用那些好处。可以使用JavaBeans,也不必非要部署应用到JBoss Application Server。

1.2.3 Debunking the “vendor lock-in” myth
I don’t want to be shy about addressing the myth that Seam is a JBoss-focused technology
or that by using Seam, you get locked into JBoss. The Seam development team
isn’t hesitant about making recommendations against the JBoss party line. The Seam
application stack is an aggregation of best-of-breed technologies known to work well
together. Seam is no more a JBoss technology than Struts is an Apache technology or
Spring is a SpringSource technology. An examination of the most successful complex
projects in enterprise Java outside of JBoss, such as Spring, Hibernate, Eclipse, and the
Java EE platform itself, reveals that these projects are supported by organizations with
paid developers. Seam is open source and can be whatever you, the community,1 drive
it to be. Although the projects may be hosted in the JBoss Labs under the roof of
JBoss/Red Hat, the source code is yours to copy, share, and modify. Specifically, JBoss
Seam is licensed under the Lesser GNU Public License (LGPL), which is considered
one of the more flexible options.

“卖家依赖”神话
你也可依赖JBoss。JBOSS是Lesser GNU Public License(LGPL)的,你可以自行修改,但前提条件是你真的可以。

Seam was designed to be container agnostic and much effort has gone into ensuring
that Seam is compatible with all major application servers, including BEA WebLogic,
IBM WebSphere, Oracle Containers for Java EE (OC4J), Apache Tomcat, and GlassFish.
But the compatibility runs deeper than deployment. The improvements that Seam has
introduced into Java EE are being contributed back into the platform as standards using
the Java Community Process (JCP) as a vehicle and captured in JSR 299: Web Beans, as
mentioned earlier. The goal of this JSR is to unify the JSF managed bean component
model with the EJB component model, resulting in a significantly simplified programming
model for web-based application development. The effect of this JSR is that it will
foster alternative implementations of Seam’s innovations.

因为SEAM是标准的,所以付出了很多努力以适合多种应用服务器:BEA WebLogic,IBM WebSphere, Oracle Containers for Java EE (OC4J), Apache Tomcat, and GlassFish。

With an understanding of why Seam exists, and faith that you are not getting
locked into JBoss by choosing this technology, you now need to consider whether
Seam is the right framework for you based on technical merit. After all, Seam may
have saved Java EE, but can it fit the bill as your development framework of choice?

1.2.4 Making the case for Seam
Is there really a need for another application framework? Wasn’t Spring supposed
to be the one framework to rule them all? I’ll let the success of Ruby on Rails, and
the wave of Java developers flocking to it, prove that the need for a suitable Java
application framework—or, in some developers’ minds, an entire programming
environment—remains. So, should you follow the crowd? My advice is to look before
you leap.

是不是还需要新的框架,是不是Spring已一统天下,是不是应该随大溜?我建议你跳前还是先看看。

Promising that a framework will make the job of developing applications simpler is
lip service. Just because you are able to create a throwaway blog application with a
framework doesn’t make it viable. To earn the right to be called enterprise software,
the framework has to stand up to the challenges of the real world, warts and all, and
help the developer create well-designed, robust, and readable code. That is Seam’s
goal. Seam eliminates complexity and makes proven libraries more accessible. Seam
doesn’t turn its back on the pervasive Java EE platform, but rather serves as the glue
that makes it truly integrated. Rather than encourage you to forget everything you
know, Seam finds a way to allow you to use the Java EE services in a more agile way,
while also providing enough new toys, in the form of extensions and third-party integrations,
to make using it fun and interesting.

Here is a small sampling of the many improvements that Seam brings to the Java
EE platform, all of which succeed in making the platform simpler:
■ Eliminates the shortcomings in JSF that have been the subject of countless rants
■ Mends the communication between JSF and transactional business components
■ Collapses unnecessary layers and cuts out passive middle-man components
■ Offers a solution for contextual state management, discouraging the use of the
stateless architecture (i.e., procedural business logic)
■ Manages the persistence context (Hibernate Session or JPA EntityManager) to
avoid lazy initialization exceptions in the view and subsequent requests
■ Provides a means for extending the persistence context for the duration of a
use case
■ Connects views together with stateful page flows
■ Brings business processes to the web application world
■ Plugs in a POJO-based authentication and authorization mechanism backed by
JAAS that is enforced at the JSF view ID level, accessible via the EL, and can be
extended using declarative rules and ACLs
■ Provides an embedded container for testing in non-Java EE environments
■ Delivers more than 30 reference examples with the distribution

下面是SEAM为Java EE平台带来改进的几个小例子:
去除了JSF的缺点
修补了JSF与业务部件间的通讯
去除了不必要的层和中间人部件
提供了上下文状态管理,放弃无状态结构
管理存储上下文(Hibernate Session or JPA EntityManager)以避免懒加载异常
提供了扩展的存储上下文
将页面流连接到views
将“业务过程”引入到WEB应用世界
加入了基于POJO的JSF view级别的授权机制,可以使用声明的规则和ACL
提供了嵌入式容器,用来测试非Java EE环境
带了30个以上的例子


As you can see, Seam isn’t shy about addressing problems in the platform, particularly
those with JSF. For existing JSF developers, the first bullet point is enough to justify
the need for this framework. They can attest to that fact that JSF can be quite
painful at times. That is no longer true with Seam’s aid. The second point justifies
Seam’s usefulness in standards-based environments, where Seam fits in quite naturally.
But Seam doesn’t stop there. It encourages developers to collapse unnecessary
layers to achieve simpler architectures and promotes the use of long-running contexts
to relieve the burden of state management. Aside from improving the programming
model, Seam provides a tool that prepares the scaffolding of a Seam-based
project; generates a create, read, update, delete (CRUD) application from an existing
database schema; makes integration testing easy; and serves up Ajax in a variety
of ways.

在SEAM以前,JSF曾一度让人痛苦。
SEAM提供了工具,可以生成基于SEAM的项目,可以CRUD现有的数据库,集成测试,支持Ajax。

1.3 Seam’s approach to unification
Seam revitalizes the standard Java EE platform by putting an end to its divergence and
unifying its components, filling in the voids for which it is often criticized, making it
more accessible, extending its reach to third-party frameworks and libraries, and
form-fitting them all together as a well-integrated and consistent stack. While the features
of Seam are vast, Seam’s core mission is getting JSF, JPA, and POJO components
to work together so that the developer’s focus can be placed on building the application,
not on integrating unallied technologies.

SEAM集成了很多东西,但核心任务是JSF, JPA, and POJO部件的协同。

1.3.1 Seam integrates JSF, JPA, and POJO components
Getting technologies to work with one another is more than just having them pass
messages back and forth. It’s about creating an interaction that blurs the boundary
between them, making them act as a single, unified technology. Seam achieves this
integration by fitting EJB 3 up against the web tier, finding a place for JPA, and scrapping
the ineffectual JSF managed bean container. After reviewing how Seam tackles
these challenges, you get a chance to determine which Seam stack is right for you.

让各技术协同不是简单地在其间传递信息,而是要建立交互以消除它们之间的边界。使其看作一个单独的、统一的技术。

HELPING OUT A WEB-CHALLENGED EJB 3
By design, EJB components cannot be bound directly to a JSF view. It’s great that EJB
components are scalable, transactional, thread-safe, and secure, but it doesn’t do much
good if they are completely isolated from the web tier, accessible only through a JSF
backing bean acting as an intermediary. This isolation makes them of limited use in web
applications because of the complexity involved to integrate them. They are not able to
access data stored in any of the web-tier scopes (request, session, and application) or
the JSF component tree, thus impairing their insight into essential parts of the applica
tion. (The goal here is really just to give EJB 3 components access to Seam’s stateful
scopes.) Also, it’s easy to get into trouble with concurrency when using EJB components
from the web tier. For instance, the Java EE container is not required to serialize access
to the same stateful session bean, leaving it up to the developer to take care of this
task or catch the exception that can result. Also, complexities arise when dealing with
non-thread-safe resources such as the JPA EntityManager. The only way the developer
can safely use EJB components in the web tier is by interfacing with an adapter layer.

最初设计时,EJB是不能直接绑定到JSF view中的。它本身不错,可扩展、有事务、线程安全、有安全检查,但如果孤立于WEB层,只作为JSF的支持bean,则作用会发挥不出。它没办法访问任何WEB层范围(scopes of request, session, and application)的数据。JSF部件树。让用户自己去处理各种关联及异常。同时,引起复杂的还有处理非线程安全的资源如JPA EntityManager。开发者唯一可以安全使用EJB部件于WEB层的方式是借用适配器层。

Seam gives EJB 3 components access to web-tier scopes, offers a way to manage the
state of EJB 3 components so that they can be used safely in the web tier, and even serializes
access to stateful components to make concurrency issues a responsibility of the
infrastructure and not the developer. Also, there is never a question about accessing
non-thread-safe resources since Seam handles the scoping properly.

SEAM搞定了这一切。

Turning the tables, JSF faces equivalent challenges accessing business-tier
components.

HOOKING JSF TO A BETTER BACK END
JSF has its own “managed” bean container that is configured using a verbose XML
descriptor, as opposed to the annotation-based configuration in EJB 3, and has a limited
dependency injection facility. While JSF managed beans can be stored in the webtier
contexts, they are barren objects, lacking scalability, transaction atomicity, and
security (probably why they are termed beans and not components). They must reach
out to an EJB 3 component to attain these business services. What you find is that
you’re stuck creating this façade layer to bridge EJB 3 components to the UI that acts
on them.

JSF有自己的被管理的BEAN容器,但是用冗长的XML描述的。JSF被管理的BEAN可以存储WEB层的上下文。但这些是穷对象,规模小,原子事务。必须求助EJB3才能获得业务服务。


To correct this mismatch, Seam enables JSF UI components to tap right into the
EJB layer by allowing EJB 3 components to stand in as JSF “backing” beans and action
listeners. There’s no longer a need for the managed bean façade layer and its verbose
XML descriptor. By eliminating the complexity caused by the mismatch, it encourages
developers to relax stringent mandates on overarchitected designs.

为解决这一问题,SEAM直接让EJB来作支持BEAN。

WHICH SEAM ARE YOU?
Seam is not just a collection of classes and artifacts that get dropped on your desk with
the disclaimer “Some assembly required.” The key to Seam’s success is that it offers a
handful of well-tested bundles that operate fluently. These bundles include compatible
versions of many third-party libraries. You can liken the offering to the simplicity
of buying a Mac when compared to buying a Dell. When you buy a Dell, you can customize
the assembly down to the last stick of RAM. You get a product customized
exactly to your needs, but getting there requires a lot of thought and effort on your
part. Buying a Mac is much simpler in comparison. You choose between a laptop and
a notebook, and then you select a screen size. Everything else is just details that Apple
works out for you. Seam has a comparable set of options. You choose a state provider
and a persistence provider (and, down the road, a web framework). Everything else is
just details that the Seam developers work out for you. By removing the burden of too
many choices, Seam can make life for the developer simpler.

SEAM不是简单地拼凑一些类,这些第三方库都经过了百经考验。这有些像买电脑时对MAC和DELL所作的选择。

The two main technology choices in a Seam application, summarized in figure 1.3, are
the state provider and the persistence provider. The state provider is the technology
that handles the application logic and responds to events in the UI. The persistence
provider transports data to and from persistence storage. Seam manages the persistence
provider to allow for the persistence context to be extended across a series of
pages and shared among multiple components.

As mentioned earlier, Seam does not require you to use EJB 3. You have the option
of using basic JavaBeans along with Hibernate without fear that you are losing out on
functionality. The term JavaBean broadly encompasses all non-EJB components, so
Spring beans apply here as well. Another popular choice is to partially adopt EJB 3 by
combining JPA with JavaBeans, which is the bundle used by the example application in
this book.

Prior to Seam, getting these technologies to work together meant integrating the
containers that manage them. EJB 3 has its container. JSF has one too. Spring is yet
another. Once again, the task of writing this glue code fell on the shoulders of the
developer. The need for a central integration point gave rise to Seam’s contextual
component model.

1.3.2 The contextual component model
At the heart of Seam is the contextual component model. Before your eyes gloss over,
give me three short sentences to make this term meaningful to you. (1) Seam is a factory
that constructs objects according to component definitions. (2) After creation,
each object is stored in the container under one of several contexts (i.e., variable
scopes) with varying lifetimes, making the objects contextual and capable of holding
state (i.e., stateful). (3) Seam promotes the interaction of these stateful objects across
contexts, assembling them together according to metadata associated with their
respective classes. Chapter 4 explores components and contexts in depth and gives
you an opportunity to learn how they are used in an application.

上下文部件模型
SEAM是依部件定义而可以建立对象的工厂
建立后,对象保存在具有特定上下文的容器中
SEAM对这些有状态对象进行管理

In this section, you learn how this model provides the basis for the unification of
the technologies previously discussed. The unification is facilitated by a combination
of the component registry, annotations, configuration by exception, method interceptors,
and the unified Expression Language (EL).

A CENTRAL COMPONENT REGISTRY
Seam rakes in all of the Java EE components into a central registry, whether they are
EJB session beans, JavaBeans, Spring beans, or JPA entities. Any technology incorporated
into the Seam stack can look to the Seam container to retrieve instances of the
components by name and collaborate with the container to exchange state. Technologies
that have access to the container include Seam components, JSF view templates,
Java Business Process Management (jBPM) process definitions, Java Process Definition
Language (jPDL) page flow definitions, Drools rules, Spring beans, JavaScript, and
more. Seam’s container also unifies the variable scopes of the Servlet API while introducing
two of its own stateful scopes, conversation and business process, that are better
suited to support user interactions.

不管是哪类部件,要集中注册。

Of course, components aren’t just going to fall into this registry; they have to be
recruited. Seam scours the classpath and enlists any class that contains a marker annotation,
discussed next, that identifies it as a Seam component.

ANNOTATIONS OVER XML
One way that Seam cuts down on the configuration
overhead of Java EE is by eliminating needless
XML. Although once thought to be desirable
because of is flexibility, XML is external configuration
and quickly becomes out of sync (and out
of touch) with the application logic. Seam brings
configuration back in line with the code where it
is easier to locate and can be refactored.

When the temptation arises to define JSF managed
beans in XML, Seam just says “No!,” a tenet
that is captured in figure 1.4. Seam reduces the
declaration of a component to a single annotation,
@Name, placed above the class definition.
Seam components can take the place of JSF managed
beans.


With enough dedication, you can avoid the use of XML in Seam altogether, which
is quite surprising given the number of places it could be warranted. Seam only resorts
to XML when annotations do not suffice or to isolate deployment overrides. If you are
not a fan of annotations, don’t go running for the door just yet. Seam still allows you
to define components using XML, which is the main topic of chapter 5. Annotations
are just more concise and easier to maintain, in my opinion.


Moving to annotations is more than just improving the efficiency of keystrokes.
Annotations are the central piece of Seam’s configuration by exception strategy, conserving
keystrokes until they are really necessary.

使用annotations不单是为了减少打字,它也是SEAM的异常策略的配置方式。

CONFIGURATION BY EXCEPTION
A good way to describe configuration by exception is by saying that the software is
“opinionated.” The general idea is that the framework happily prefers to operate as
designed. The more you embrace the defaults, the less work you have to do. You are
only required to step in and play a part when the software needs to do something different
than the typical behavior.

软件都是傻的。只会按当初设计的路线走。你设置的默认越多,你其它的工作就越少。你只需要在少数需要介入的情况参与一下。

In Seam, configuration by exception goes hand in hand with annotations. The
annotations give Seam a hint to apply behavior and Seam tries to assume as much as
possible about the declaration by relying on sensible defaults and standard naming
conventions to keep your load light. In this way, Seam offers a nice balance between
explicit declarations and assumed functionality.

SEAM中configuration by exception是与annotations结伴而来的。

While annotations cut down on keystrokes, there’s more to annotations than just
the elimination of XML. Annotations supply extra metadata to the class definition,
where it is easier to find and refactor than metadata stored in external descriptors.

DECORATING COMPONENTS WITH SERVICES
Since components are requested through the Seam container, Seam has an opportunity
to manage the instances throughout their life cycle. Seam wires the object with
interceptors, wrapping it in a shell known as an object proxy, before handing down
the newly created instance. This allows Seam to act as the object’s puppeteer, pulling
on its strings during each method call to add behavior, as depicted in figure 1.5. Interceptors
account for much of the implicit logic in Seam that makes it “just work.”
Examples include beginning and committing transactions, enforcing security, and
getting objects to socialize with one another. Annotations on the class definition give
the interceptors a hint of how to apply the extra functionality, if for some reason it
can’t be implied or needs to be different than the default behavior.

SEAM部件是通过SEAM容器来访问的。SEAM用拦截器串起对象,包装起来作为对象代理。

The final piece to the unification puzzle is to give the application a way to access
components in the container using a universal syntax. That’s the role of the unified
EL.

最后,需要进行统一访问部件的工具是统一的语法,即EL。

EXTENDING THE REACH OF THE UNIFIED EL
The unified EL is an expressive syntax used to resolve variables and bind components
to properties and methods on JavaBeans. It was first introduced to better integrate JSF
with JavaServer Pages (JSP), to look up managed beans and other objects stored in
web-tier scopes, and to serve as the basis for the JSF binding mechanism. Its impact,
however, is far more widespread, thanks to its pluggable design.

The EL is an open API that allows custom resolvers to be registered, thus turning
the EL into a variable hub. Consequently, any layer of the code that wants to tap into
the EL unified variable context can do so using the public API. Thus, the EL frees you
from having to develop a custom bridge between the variable contexts used by the different
technologies in your application. Although you’re used to seeing the EL only in
the view, there isn’t anything web specific about it.

EL是开放式API,允许自定义解释器的注册。

Seam takes advantage of the EL in two ways. First, it registers a custom EL resolver
that is aware of the Seam container. This allows Seam components to be accessed
using EL notation from anywhere in the application where the EL is available (which is
pretty much everywhere). Second, Seam makes heavy use of the EL under the covers,
allowing EL notation to be used in annotations, configuration descriptors, log and
message strings, Java Persistence Query Language (JPQL) queries, page flow definitions,
and even business processes. With Seam, the EL truly is unified.

SEAM在两方面利用EL,第一,让SEAM部件可以用EL访问。第二,SEAM到处都用EL,annotations,
configuration descriptors, log and message strings, Java Persistence Query Language (JPQL) queries, page flow definitions,and even business processes中,到处都用。

Despite all that has been said about Seam, nothing speaks to a programmer like
lines of code. To help demonstrate why Seam is a sound choice and how it saves you
valuable development time, I am going to whet your appetite with a brief example. In
chapter 2, you’ll get a chance to sink your teeth into Seam by building an entire
application with just a couple of commands.

1.4 Your first swings with Seam
To demonstrate some of the core principles of Seam, I’m going to step you through a
basic application that manages a collection of golf tips. Don’t worry about trying to
understand everything that you see here. Instead, focus on how Seam relies on annotations
to define components, how the layers of the application are pulled together
through the unified component model, and the high signal-to-noise ratio in the business
logic thanks to configuration by exception. I demonstrate a densely packed set of
features in this example, so don’t think that you have to use all of these techniques in
order to use Seam.

你的第一次挥杆
这个例子用来管理golf tips。不用强求这时就明白所有的。只要看清SEAM是怎样定义部件的就行了。

We all want to be better golfers (at least, those of us who torture ourselves with
the sport). Focusing on a simple golf tip can help shave off a couple of strokes from
your round. To keep track of the tips that you collect from the pros, buddies, and
articles, you’re going to slap together a Seam application that reads and writes these
tips to a database. Aside from the deployment artifacts, which aren’t considered in
this example, there are only a handful of files that you need to produce a functional
application.

1.4.1 Entity classes serving as backing beans
I’ll start by discussing the GolfTip JPA entity class, shown in listing 1.1. In a Seam application,
entity classes serve two purposes. Their primary role is to carry data to and from
the database. The object-relational mapping (ORM) mechanism, as this is called, is not
part of Seam per se. That work is handled either by JPA (the standard Java persistence
framework) or Hibernate, though you discover in chapter 8 how Seam can bootstrap the
ORM runtime and regulate the lifetime of the ORM’s persistence manager.

我以GolfTip JPA实体类来开始。这个实体类有两个用处,主要目的是与数据库交互。

The second role of entity classes in a Seam application is to serve as form “backing”
beans (akin to a Struts ActionForm) to capture input from the user, thus replacing the
need for a shallow “backing” bean class. An entity class becomes a candidate for use in
a JSF view if it has a @Name annotation on its class definition, a condition that is satisfied
by the GolfTip class in listing 1.1. You then bind the form inputs directly to properties
on the entity class and JSF handles the necessary conversions and validations.

实体类的第二个作用是用作“backing”beans。如同Struts ActionForm。

有@的是Java 5 annotations。粗体字的@Name表明GolfTip作为命名为tip的SEAM部件。不管什么时候由SEAM容器调用这一上下文变量,SEAM会建立一个新的GolfTip类实例。绑定实例变量到会话上下文的tip上下文变量。
@Entity关联GolfTip类到数据库的同名表。@Id指主键。@GeneratedValue表自增。属性会自动映射到GolfTip表的同名列。

As you can see, using the @Name annotation gives you one less file to worry about
(that of the JSF managed bean facility and its verbose XML dialect). Staying away
from the managed bean configuration is one of the early benefits of moving to Seam
components. Another compelling advantage of adopting Seam is being able to bind
the action of the UI command component to a method on a transactional business
object.

你也看到了,用@Name annotation,我们又可以少关心一个文件了(相对于JSF冗长的XML)。

1.4.2 An all-in-one component
As with entity classes, there’s no need to create a dedicated managed bean to act as a
mediator between the JSF page and the service object in Seam. Instead, the service
object can respond directly to an action invoked in the UI. At first, that might sound
like a bad idea because it appears to cause tight coupling between the UI and
the application logic. Seam prevents this coupling by acting as the mediator. As a
result, the action component does not have to contain a single reference to a JSF
resource. In fact, in chapter 3 you discover that the return value of the method need
not serve as a logical outcome for a navigation rule—a typical requirement of JSF
managed beans—since Seam can evaluate an arbitrary EL value expression for this
purpose. This example relaxes the separation from JSF to keep the number of classes
to a minimum.

有了实体类,就不再需要中间类。服务对象可以直接受UI调用。最初可能会认为这是个坏主意,因为会出现 紧耦合于UI及业务逻辑,其实SEAM在中间充当着调节者。所以一个action部件不必属于一个JSF资源。

In the golf tips application, the TipAction class, shown in listing 1.2, is declared
as a Seam component using the @Name annotation and is thus capable of having its
methods bound to UI controls. It handles the add and delete operations in the golf
tips interface.

TipAction 被声明为了SEAM部件,可以处理golf tips界面的增加、删除操作。

本次的范围为事件上下文(默认的JavaBean部件),不同于实体类的是拥有@In annotation用于注入。有两个独立的部件JPA EntityManager 和内建的 JSF messages manager。这个部件也为JSF view的应用而准备了一套GolfTip对象,从用户在界面的选择中得到GolfTip,使其可以用于事件处理或后续显示,同时操作关于GolfTip的JSF状态信息。

The TipAction component packs a lot of functionality in a limited amount of
space. What I want you to recognize is that, aside from the annotations, there’s practically
no evidence of infrastructure code in this class. Apart from creating the status
messages, the only code that you’re required to write is code that reads, persists, and
removes tips from the database using the JPA EntityManager instance. It’s probably
best to push this code into a data access object (DAO), which may also be a Seam component,
but Seam doesn’t impose this architectural requirement on you. Seam’s focus
is on frugality, as demonstrated in this example. Absent are any Servlet API calls that
read request parameter values or set request or session attributes. Instead, the component
consists solely of business logic.

简单并且完成了多个功能,除了annotations,没有其它代码,除了建立状态信息,其它需要你写的代码就是用JPA EntityManager 实例读、存、删tips数据。可能用个DAO更好,DAO也可做成SEAM部件。但SEAM不强加于你。SEAM强调的是朴素,省下的主要是Servlet API 调用,如读请求参数及设置请求或session的属性。部件保持独立于业务逻辑。

1.4.3 Binding components to the view
Seam bridges the layers in the golf tips application by binding both the properties of
the entity class and the methods of the action component to elements in the JSF view.
Figure 1.6 shows the golf tips user interface. Behind this rendered page is a Facelets

桥接了实体类的属性和action部件的方法,
在这个页面的背后,是一个Facelets模板。golftips.xhtml

template, golftips.xhtml, which associates value- and method-binding expressions to
elements on this page to output data, capture form input, and respond to user
actions. Use this figure to follow along with the discussion of how the JSF view interacts
with the Seam components in the server.

使用值或方法表达式来输出数据,获得输入,回复用户。

NOTE
The file extension .xhtml indicates that this file is a Facelets template.
Facelets is an alternative view handler for JSF that was created to escape
the mismatch between the JSF and JSP life cycles. Facelets is the preferred
view technology for Seam applications and is used throughout the book.

扩展名xhtml表示这是Facelets template模板,Facelets在页面处理方面是JSF的替代品,用来回避JSF and JSP 生命周期的不匹配。Facelets是SEAM推荐使用的技术。

Start by focusing your attention on the form that is used to submit a new tip at the bottom
of the page. Each input element is bound to properties on the GolfTip entity class using
EL notation (e.g., #{tip.author}). When used in the value attribute of an input element,
the EL notation acts as a value-binding expression. It captures the form value and
transfers it to an instance of the GolfTip entity class as part of the JSF life cycle. Here’s
the (slightly trimmed-down) fragment of the JSF template that renders the form:

Seam makes the association between the value-binding expressions used by the input
fields and the GolfTip entity class through the context variable tip. The @Name annotation
on the GolfTip class binds the class to the tip context variable. When the tip context
variable is referenced by a value expression in the JSF template (#{tip.*}), Seam
instantiates the GolfTip class and stores the instance in the Seam container under the
variable name tip. All the value expressions that reference the tip context variable are
bound to that same instance of the GolfTip class. When the form is submitted, the input
values are transferred to the properties of the unsaved entity instance.

Let’s consider what happens when the form is submitted. With Seam working in
conjunction with JSF, any interaction with the Servlet API is abstracted away. Instead,
you work through declarative bindings. The method-binding expression specified in
the action attribute of the submit button, #{tipAction.add(tip)}, indicates that the
TipAction component serves as the action component for this form and that when
the button is activated, the add() method is invoked. Notice that this method expression
actually passes the GolfTip instance associated with the tip context variable
directly into the action method as its sole argument, which effectively makes the form
data available to the method. Seam provides parameterized method-binding expressions
as an enhancement to JSF. When the method completes, the list of tips is
refreshed and the page is once again rendered.

1.4.4 Retrieving data on demand
What makes Seam so powerful is that it includes a mechanism for initializing a variable
on demand. The top half of the screen in figure 1.6 renders the collection of tips
in the database using the following markup:

应招得数


The focal point of this markup is the #{tips} value expression. Notice that tips is not
the name of one of the Seam components in the golf tips application. However, it is
referenced in the value attribute of the @Factory annotation above the
retrieveAllTips() method of the TipAction class from listing 1.2. The purpose of
this method is to initialize the value of the tips context variable when it’s requested.
Subsequent requests for the same variable return the previously retrieved value rather
than triggering the method to execute again.

But hold on a minute! The retrieveAllTips() method doesn’t return a value.
How is the value passed back to the view renderer? That’s where things get a little
tricky. After executing this method, Seam exports properties of the component that
are annotated with either @Out or @DataModel to the view. Seam notices that the
@DataModel annotation is assigned to the tips property on the TipAction component.
That tells Seam not only to export its value to the tips context variable, but also
to wrap the value in a JSF DataModel instance. The view iterates over this wrapped collection
to render the data grid. The reason the collection is wrapped in a DataModel is
to enable clickable lists to support the delete functionality.

且慢,retrieveAllTips()方法并不返回值,值是怎么返回到视图重画的呢?是使用@Out or @DataModel。@DataModel不但要输出值到tips上下文变量,还要赋给JSF DataModel实例。将数据放入DataModel的原因是要使用可点击列表以支持删除功能。

1.4.5 Clickable lists
The scope specified on the annotation is ScopeType.PAGE, which instructs Seam to
store the collection of tips in the JSF component tree. Since the data model is being
stored in the JSF component tree, it is made available to any JSF action that is invoked
from that page (resulting in a “postback”).

可点击列表
annotation 指定到 ScopeType.PAGE是将tips集合保存到JSF部件树。之后,JSF的所有方法就可以从页面中调用。(这就是postback。)

The #{tipAction.delete} method expression, bound to the delete link adjacent
to each golf tip, benefits from the propagation of the tips data model through the
JSF component tree. When the user clicks one of the delete buttons, the data model is
restored along with the JSF component tree. When JSF processes the event, the internal
pointer of the data model is positioned to the index of the activated row. This is
where the complement to the @DataModel annotation, the @DataModelSelection
annotation, is used. This annotation reads the current row data (the instance of
GolfTip) from the data model and injects it into the property over which the annotation
resides. All the action method has to do is pass the instance of the selected
GolfTip to the JPA EntityManager to have it removed from the underlying database.
Once again, the action component remains void of infrastructure code. Compare that
to the JSF blueprints.2

#{tipAction.delete}用于删除,也受益于JSF部件树的tips数据模型的传播。当用户点击按钮时,数据模型被存储到JSF部件。@DataModelSelection是作为@DataModel的补充。类似于指向数据行的指针。Action方法只需要做的是将被选择的GolfTip实例传给JPA EntityManager,将其从数据库中删除。

All that’s left is to write a quick end-to-end test to ensure that we can save a new tip
and that it can be subsequently retrieved.

剩下的就是写一个end-to-end测试,以保证我们可以存一个新的tip并在以后可以被检索。

1.4.6 Integration tests designed for JSF
The area of development that has routinely slowed down Java EE developers most
often is testing. Even if you’ve never written a test, you’re still testing. You test your
code every time you redeploy your application or restart the application server to view
the result of your latest modifications. It’s just slow and boring to do it that way. These
days, testing is an integral part of any application development, and no framework is
complete without an environment that allows you to test “outside of the container.”
Seam once again demonstrates its simplicity by exposing a single test class that can
handle all of the integration testing needs in a Seam-powered application.

测试是最可能拖们Java EE的环节。即使你从没写过测试,你还是在测试,你启动服务器,看输出,就是在测试。不过这样效率会很低。如今,测试是所有项目的一个重要组成。SEAM再次显示了其简便,用一个简单的测试类来处理所有应用的测试需求。

To make integration testing of JSF actions a breeze, Seam provides a base test class
that sets up a stand-alone Java EE environment and executes the JSF life cycle within
the test cases. The test infrastructure is driven by TestNG,3 a modern unit-testing
framework that can be configured using annotations. Although TestNG doesn’t
require you to inherit from a base test class, Seam’s testing framework uses this
approach to set up the fixture needed to bootstrap the embedded Java EE environment
and the JSF context.

SEAM提供了一个基本测试类,建立了一个独立的Java EE环境,在测试用例中,执行JSF生命周期。测试框架是使用TestNG,其也是使用annotations.TestNG不需要你继承测试的基类。SEAM用这种方式建立起设施以开始嵌入的Java EE环境及JSF上下文。

The test class GolfTipsTest in listing 1.3 simulates the initial request for the golf
tips page and the subsequent form submission to add a new tip. The code in the test is
invoked nearly identical to when it’s used in the deployed application.

列表 1.3模拟tips页面请求及提交。在测试用例中,调用和销毁几乎是同时完成。

Listing 1.3 tests both the initial rendering of the JSF view and the subsequent JSF
action triggered from the rendered page. The first request is an HTTP GET request,
which simulates the user requesting the golf tips page in the browser. This part of the
test verifies that when the tips are retrieved in the Render Response phase, Seam properly
resolves a DataModel, but the collection underlying that model is empty. The second
part of the test simulates the user submitting the form to create a new tip. The
Update Model Values phase performs the work JSF does to bind the input values to the
value expressions. The method expression that is bound to the submit button is then
explicitly invoked. Because Seam automatically wraps the Invoke Application phase in a
transaction, there is no need to worry about beginning and committing the transaction.
Finally, in the Render Response phase, the test verifies that when the tips are
retrieved this time, exactly one tip is found and that the author’s name has been interpolated
properly in the message displayed to the user. This test is intentionally terse.
There are, of course, many other scenarios that could be verified. Focus instead on
how easy it is to exercise a Seam application using this simple test framework and how
you can leverage EL notation to perform assertions.

列表中,既测试了JSF view的生成,也有后续触发的action。第一个请求是HTTP GET request,模拟浏览器中的请求,用来测试tips在Render Response phase中被检索的情况。SEAM正确解析DataModel,但这一模型下得到的数据集则是空。
第二部分模拟用户提交以建立新的tip。Update Model Values phase阶段用来做JSF的工作,绑定到按钮的方法被执行。SEAM在Invoke Application phase中自动包含了事务,所以不用操心开始及提交事务。
最终,Render Response phase阶段,测试验证tips是否被检索到,准确的说有一条被检索到,就是作者的名字,被放入了显示给用户的信息。
这个测试特意非常精炼,还有很多其它的验证可以加入进来。这里只关注SEAM是如此简单地使用测试框架,EL是怎样用于断言。

Hopefully the golf tips application has given you a general understanding of how
Seam simplifies your application and saves you time by relying on a centralized container,
annotations, configuration by exception, and the unified EL. That’s the
essence of Seam. I now want to give you an idea of what else Seam offers before you
begin your journey down the road to becoming a Seam master.

1.5 Seam’s core competencies
Throughout this chapter, there has been a lot of discussion about how Seam resolves
issues in Java EE. I want to leave you with an understanding of how Seam is going
to help your development process. Given how much Seam has to offer, this was a
challenging exercise, but I’ve been able to summarize its benefits into three core
competencies. Seam offers a better JSF, allows you to get rich quick, and fosters an
agile environment.

1.5.1 Turns JSF into a pro
Although JSF isn’t without flaws, it was selected as the main presentation framework in
Seam because of its extensible request life cycle and strong UI component model.
Realizing its potential, Seam taps into this design to strengthen JSF, making it a compelling
and modern technology for creating web-based interfaces. While it’s true that
Seam supports alternative view technologies, this book primarily focuses on using
Seam with JSF. Much of this coverage comes in chapter 3, which covers Seam’s extension
to the JSF life cycle.

将JSF变成专家
虽然JSF有自己的毛病,但SEAM选择它来作表示层框架,是因为它有可扩展的生命周期及强大的UI部件模型。正因为这些,SEAM强化JSF,使其成为先进的基于WEB的界面,虽然SEAM也支持其它显示框架,但本书专注于讲JSF。

ENHANCING JSF
Seam’s most recognizable improvement to JSF is eliminating the requirement to
declare managed beans in the JSF descriptor. In addition, Seam adds a rich set of
page-oriented functionality, covered in chapter 3, that makes the navigation rules in
the JSF descriptor obsolete as well. These features include
■ Prerender page actions
■ Managed request parameters (for a given page)
■ Intelligent stateless and stateful navigation
■ Transparent JSF data model and data model selection handling
■ Fine-grained exception handling
■ Page-level security (per view ID)
■ Annotation-based form validation
■ Bookmarkable command links (solving the “everything is a POST” problem)
■ Entity converter for pick lists
■ Conversation controls
■ Support for preventing lazy initialization exceptions and nontransactional data
access in the view

最明显的改进是不再需要在JSF描述文件声明被管理bean。同时,SEAM增加了大套的面向页面的功能。
如:
提前重画的页actions
被管理的请求参数
智能的无状态和有状态的导航
透明的JSF数据模型及数据模型的选择处理
细粒度的异常处理
页级别的安全
基于Annotation的验证
可存为书签的命令按钮(解决了“所有都是POST”的问题)
pick lists的实体转换器
会话控制
防止懒加载异常及view中的非事务数据访问

Part of the cleaning-out process of JSF involves purging passive connector beans that
do nothing more than adapt UI events to back-end business components.

ELIMINATING CONNECTOR BEANS
Any Seam component can be connected to a JSF view using EL bindings. Figure 1.7
shows the design of an interaction between a UI form and an EJB 3.0 session bean (or
regular JavaBean) that completely eliminates the need for the legacy connector bean.
The form inputs are bound directly to the entity class and the session bean is bound to
the Save button to handle the action of persisting the data.

删除连接BEAN
任何Seam部件可以使用EL绑定来连接到JSF view中,下图显示了UI表单与EJB 3.0 session之间的交互。
从而不再用中间连接BEAN。

By cutting out the middleman, not only does Seam allow you to eliminate a class that
you have to write and maintain, but it allows you to cut back on the number of layers,
thus allowing your applications to become more lightweight.

去除了中间人,SEAM让你少写及维护一个类,让你的类更轻量级。

Aside from providing universal access to components, the Seam container augments
the coarsely grained scopes in the Java servlet specification—request, session,
and application—to include scopes that make more sense from the perspective of the
application user. Seam offers two “stateful” contexts that are used to support single
and multiuser pages flows in an application.

除了提供通用的访问部件的方法,SEAM质疑servlet规范的粗粒度scopes(—request, session,and application)。提供了两个有状态的上下文以支持单页和多页流。

INTRODUCING STATEFUL VARIABLE SCOPES
One of the main challenges with developing applications that are delivered over the
web is learning how to efficiently propagate data from one page to the next—so-called
state management. The two go-to options are hidden form fields or the HTTP session.
The first is cumbersome for the developer, and the second eventually eats through
precious server resources and hurts an application’s ability to scale.

引入有状态上下文
在WEB应用开发中,一个主要的挑战就是高效地将数据从一页传到另一页,也叫状态管理。一个过度的方法是使用隐藏域,或HTTP session。这些方式,首先是开发者受累,其次是占用宝贵的服务器资源同时使应用的能力很难扩展。

Seam addresses need for stateful variable scopes whose lifetime aligns with user
interactions by adding the conversation context and business process context to the
standard web scopes. The conversation scope, covered in chapter 7, maintains data
for a single user across a well-defined series of pages while the business process scope,
covered in chapter 14 (online), is used to manage data that supports multiuser flows
complete with wait states. The relationship between the lifetime of the scopes managed
by the Seam container is illustrated in figure 1.8.

SEAM使用会话级和业务过程级的上下文来解决这些问题。用于业务处理的一系列页面,可以支持多用户流程,

The conversation context is tremendously important in Seam not only because it is so
unique and gives the user a better experience, but because it makes working with an
ORM tool easy on the developer.

会话上下文对SEAM极为重要,不但是因为给了用户好的体验,而且容易用ORM来映射。

E

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21802202/viewspace-1023400/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21802202/viewspace-1023400/

你可能感兴趣的:(ui,数据库,java)