温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SEAM IN ACTION 第二章

发布时间:2020-08-06 23:27:59 来源:ITPUB博客 阅读:121 作者:crazyscissors 栏目:编程语言

2、Putting seam-gen to work


This chapter covers
■ Setting up a project with seam-gen
■ Reverse engineering a database schema
■ Hot-deploying incremental changes
■ Choosing an IDE for development

用seam-gen建立项目
对数据库反向工程
热部署
选择一个IDE


Learning a new framework can be challenging, risky, and time consuming. You
must leave the comfort of your current tool set and venture into unknown territory.
To justify your research, you seek out early victories in the form of trivial “Hello
World” examples. After completing one, you celebrate your accomplishment.
Sadly, few others will be so impressed.

学习新的框架是有挑战的、有风险也费时间。

Thanks to seam-gen, Seam’s rapid development tool, you can skip the putt-putt
course and come out swinging on your first day with Seam. seam-gen creates for
you a functional, database-oriented application that is ready for show and tell without
requiring you to write a single line of code. The seam-gen tool first gathers
information about your application, such as the project name and database connection
properties. It then uses that information to put in place the scaffolding of a
Seam-based project. Finally, you point seam-gen at your database, which it reverseengineers
to create artifacts that serve dynamic web pages used to create, read,
update, and delete (CRUD) records in the database tables. The result? An achievement
that’s sure to impress even the toughest crowd. How’s that for in Action?

用seam-gen,SEAM的快速开发工具,你可以在第一天就有很多收获,它为你生成一个多功能的,面向数据库的应用,而不用你写一行代码。其先向你收集应用的一些信息如项目名,数据库属性,然后就生成了基于SEAM的项目。
最终,指定你的数据库,逆向工程后,你就可以用网页来建立、读取、更新、删除数据表中的记录。

In this chapter, I demonstrate how seam-gen can get you set up quickly to start developing
with the Seam framework. By the end of this chapter, you’ll have a working golf
course directory application that you can deploy to various JBoss Application Server
environments. A cookie-cutter process is going to fall short in some areas, so I also show
you ways to customize the application that seam-gen kicks out, a process that’s carried
forth throughout the book. What you’re left with is an achievement that is far more functional
and rewarding than what a typical “Hello World” has to offer. Don’t you know? For
judging the merit of a web-oriented framework, CRUD is the new “Hello World.”

本章我将用seam-gen为你演示使用Seam快速启动开发。本章结束时,你会生成一个可以部署到各类JBoss应用服务器环境的高尔夫课程目录应用程序。这种具有功能的项目例子比典型的“Hello World”式例子要好很多。要评判基于WEB的框架,测试CRUD就是新的“Hello World.”

2.1 The Open 18 prototype
In this book, you’ll be developing an application named Open 18, a community site
oriented towards golf. Golf is a tremendously rich domain model and offers a nice
opportunity to demonstrate Seam’s features. You’ll start by reverse engineering an
existing database schema to create a prototype of the application. I chose this scenario
because it demonstrates how you can use seam-gen to escape the dreaded unproductive
phase when starting a new project. You may also find these techniques useful for
producing applications that don’t aspire to be more than a CRUD front end for a database.
The remainder of the book takes a primarily free-formed approach when incorporating
additional functionality. Some of the enhancements you’ll see later in the
book include a data entry wizard, side-by-side course comparison, score tracker, favorite
courses, registration emails, and PDF scorecards.

本书中你会开发一个应用名叫Open 18,是一个面向高尔夫的社区站点。Golf是极端的富域模型(rich domain model),可以演示SEAM的各种特性。一开始是逆向工程从一个现有的数据库创建一个应用原型。我用这个例子来让你绕开项目初期的那种低产的状态。

If you want to follow along with the development of the project as you read, you’ll
need Seam and its prerequisites extracted on your hard drive. You can find instructions
on how to set up your software in appendix A. I encourage you to scan this supplementary
material before continuing so that you can take a hands-on approach to
this tutorial.

你可以在appendix A找到软件的安装指导。建议先看一眼。

Let’s start by taking a look at the initial requirements for our prototype application
and see how seam-gen can help us fulfill them.

先看一眼起步需求。

2.1.1 Consider yourself tasked
It’s 1:30 PM on Wednesday, two days before your summer vacation. Your boss taps you
on the shoulder just as you finish reserving tee times for your annual golf getaway in
“Golf Heaven.” You can’t wait. You’ve practiced all summer at the driving range so you
can top last year’s scores. Actually, let’s be honest. You seasoned your swing so that you
can look like a pro in front of your fans (*cough* friends).

You look up and realize that your boss is still standing there, waiting for you to
break out of your daze and return to the real world. He looks serious. Obviously, this
conversation is not going to be spent reminiscing about golf. With a sobering tone, he
informs you that he just ducked out of a management meeting in which he was
reminded of a web application that was supposed to have been completed months
ago. The anxiety begins to build in you.

The sales team is expecting to present an application that provides access to the
company’s extensive database of golf courses at the biggest trade show of the year,
which happens to be this coming weekend. Without this application, they won’t have
anything new to demonstrate. Showing up empty-handed would hurt the company
image and jeopardize its credibility. In truth, your manager should be sacked for letting
the situation get to this point. The sad reality is that it won’t happen. Besides,
turnover isn’t going to help you now. The deed has been done; the promise has been
made. Someone is going to have to reach into the hat and yank the rabbit out by the
scruff of its neck. That someone is you.

If this were any other week, these antics would barely register on your annoyance
meter. But this week is different. The mercury is rising. If things don’t go well, it may put
your much anticipated vacation at risk. The thought of not standing on the first tee at
the break of dawn, in complete Zen with the dew-laden landscape all around you, is just
killing you. But you also get a kick out of being a hero, so you decide to crank out a prototype
by the end of the week and save the company before seeking your leisure.

The question is, do you have something in your toolbox that can get you out of this
time crunch? A solution that you can live with when you return? You’ve read about
how quickly you can create a functional Java EE 5–based application using seam-gen,
so you decide to put it to the test. For that, you need requirements; they come from
your boss in an email:

你老板在email提出需求:

You must build a web-based directory for the company’s extensive database of golf facilities
and courses. The application’s users should be able to browse, paginate, sort, and filter all
of the entities in the schema. By selecting one of the facilities, they should be presented with
its details, as well as a list of its courses. From there, they should be able to drill down again
to see the holes and tee sets for each course. An administrative user should be able to modify
the database records. The marketing department also has some verbiage that you need to
place on the home page.

你必须为公司高尔夫设施和课程的数据库建立一个基于WEB的应用,用户可以浏览、分页、排序、过滤所有实体,当选中一个设施,可以显示细节,也可列出课程列表,一个管理员可以修改数据库记录。市场部可以在主页上放一些推广信息。

There you have it: the only task standing between you and 18 holes of serenity. The first
step in building the prototype is getting your hands on the database. You and the database
administrator (DBA) need to sit down and have a chat to settle the age-old debate
between developers and DBAs for this application: Which comes first, the entity or the schema?

第一步是看看数据库,你和DBA可能会坐下来,来一个谁应先行的大讨论,是实体还是数据库记录?

2.1.2 Mapping entities to the database schema
In this book, you’ll encounter two development scenarios used by the sample application:
bottom-up and top-down. The difference is a matter of which one comes first: the
database schema or the Java entity classes.

映射实体到数据库记录
本书中你也会遇到常见的两个开发策略:自上而下及自下而上。差别就是哪个先行,是数据库还是JAVA实体。

If the database schema arrives into the world first, that is bottom-up development.
The schema dictates, to a large degree, how the Java entity classes are formed. On the
other hand, if the Java entity classes show up first, that is top-down development. The
classes have free rein over how the database schema is designed. Object-relational
mapping (ORM) tools, such as Hibernate, give you some wiggle room in how the Java
entity classes are mapped to the database. For instance, it’s possible to change the
name of a column mapped to a property of a Java entity class without having to alter
the property’s name. However, there are limits to how much the schema and the
entity classes can deviate. The one that shows up first is the master and the follower
must adapt. As a developer, you have to be familiar with how to work in both cases.

如果先有数据库,则是自下而上的,数据库决定着JAVA类的形成;JAVA类先出现,则可决定数据库的设计。对象-关系映射(ORM)工具,如Hibernate,可帮助你将JAVA实体类映射成数据库。如可以改变列名而不用改属性名。
上与下的关系就是谁先出现谁为主。作为开发者要对两种情况都了解。

NOTE
Technically speaking, there is a third scenario, meet-in-the-middle, where
you have existing Java entity classes and an existing database. In this case,
you’re at the mercy of the mapping tool’s capabilities. If you have
stretched it to its limit and still can’t cover the mismatch, you have to
refactor the Java class or the database table, bringing you back to bottomup
or top-down semantics.

技术上讲,还有第三种情况,就是在中间会合。。。。

seam-gen has tasks that support both bottom-up and top-down development. In this
chapter, we take the bottom-up approach by using seam-gen to reverse-engineer the
database schema. In chapter 4, we reverse the process, taking the top-down approach
to extend the schema to include golfer profiles.

seam-gen的任务可以支持两种情况。本章我们用自下而上的。第4章用自上而下的。

BOTTOMS UP!
You’ll be performing bottom-up development to create the golf course directory outlined
earlier. Using bottom-up development, as illustrated in figure 2.1, you will convert the five

tables in the golf course directory schema (FACILITY, COURSE, HOLE, TEE_SET, and
TEE) into the five Java entity classes (Facility, Course, Hole, TeeSet, and Tee)1 that
map to them. Mapping tables to Java classes sounds like it requires a lot of effort. Don’t
worry; working with existing database tables is where seam-gen really shines. Before running
seam-gen, though, you need to get your hands on the schema and put the database
in place.

要转换5个表(FACILITY, COURSE, HOLE, TEE_SET, andTEE)到5个实体类:(Facility, Course, Hole, TeeSet, and Tee)

You bug the DBA (perhaps yourself in another role) to prepare you a Hypersonic 2
(H2) database loaded with the golf course directory schema and some sample data. H2
bootstraps from a directory on the file system and thus the entire H2 database can be
handed to you as a compressed archive. You’ll use H2 in embedded mode, which
allows the database to be started with the application, linked to the application’s runtime.
The embedded mode of H2 is ideal for rapid prototyping since it requires no
installation and you don’t need to run a separate server. Having one less server to
worry about is a good thing, especially for the sales team.

你找DBA(有时你自己就是)准备一个Hypersonic2数据库,加载高尔夫课程及一些数据,H2可以使用单纯文件的方式,所以整个H2数据库可以用一个压缩包来处理。你可以用嵌入式的H2,这可以让数据库在应用程序中使用。这种嵌信模式很适合于快速的原型开发,因为不需要安装,也需要分离的服务器,少管一个服务器总是好事。

Section A.3 of appendix A introduces the H2 database further and explains where
to find the database archive used for creating the prototype. Once you have the database
in place, you can use the H2 administration console to poke around in the
schema. This step isn’t required to generate a CRUD application with seam-gen, but
it’s important for understanding the application you’re building.

附录的第三节介绍H2,一旦就位,你可用H2管理控制台来操作了。这步不必用seam-gen生成CRUD操作,但对你理解你正建立的这个应用很有帮助。

INSPECTING THE SCHEMA
You can connect to the provided database using the H2 administration console that’s
included with the H2 JDBC driver JAR. Start the admin console by executing the Console
class from the H2 JAR file:

你可以用包含在H2 JDBC driver JAR中的H2管理控制台来连接到提供的数据库。使用下面的命令来执行管理控制台:

java -cp /home/twoputt/lib/h3.jar org.h3.tools.Console
This command instructs you to visit the H2 console URL in your browser, http://
localhost:8082. That URL brings up the database connection screen in figure 2.2.
Enter the connection URL and credentials shown in this figure and click Connect.

TIP
The connection screen lets you save your connection profiles, useful
when you are managing multiple databases. To create a new profile,
replace the setting name “Generic H2” with a name for your connection
and click Save. The H2 administration console is capable of connecting
to any database with JDBC support. To use another database, you must
add the appropriate JDBC driver (JAR file) to the classpath argument of
the java command that starts the console. Refer to the H2 manual for
more information.

连接页面让你保存你的连接概要。当你操作多个数据库时,这很重要。建立一个新的概要,换掉Generic H2。
管理控制台可以连接任何JDBC支持的数据库,为了支持新的数据库,你则要增加新的JDBC 驱动。

Once the connection is established, a schema browser/query console appears, as shown
in figure 2.3. This database console should explain why I chose to use the H2 database
for the example application: it packs a lot of functionality in a surprisingly small JAR file.

一旦建立了连接,一个浏览/查询的控制台就会出现 ,这个控制台会告诉你为什么我会用H2来示例。这个小jar中含有相当多的功能。

Now that you know the database is set up properly, it’s time to turn the job over to
seam-gen. Let’s discover why I recommend using seam-gen to get started.

现在数据库被正确的建立了,该是回到seam-gen上,再看看为什么我建议用seam-gen来起步。

2.2 Letting seam-gen do the initial work
seam-gen gives you the opportunity to learn how the Seam creators prefer to organize
a Seam-based project. It’s also a great way to become familiar with Seam’s moving
parts so that you know what you’re doing when you take Seam out on the course. If
you are a do-it-yourselfer, you may be uneasy about letting seam-gen do the work. I
recommend that you leave your reservations at the door, step out of your “working”
environment if necessary, and observe Seam in its “natural” environment at least once.
From the words of Seam’s founder, Gavin King:

让seam-gen来完成初始化工作。作者Gavin King如是说:

There really are a LOT of advantages to starting with the seam-gen structure. I did a lot of
work on things that will take you a while to reproduce if you try to do it all from scratch
(like, weeks of work!). However, there’s nothing magical about it, and other structures can
work just as well.
—Gavin King, JBoss Seam Forums2

用seam-gen来建立结构有很多好处,如果你从最开始做起,会有很多事要做,那些事没什么特殊的,别的结构也可以做。

I, too, was hesitant to use seam-gen at first. I viewed the tool as handholding for
a novice developer. After spending a lot of time using Seam (like, months of time!)
I have found seam-gen to be a huge timesaver; I strongly encourage you to give it
a try.

最开始我也不用seam-gen,我认为那是初学者的玩意儿,但在用了一段时间SEAM后(数个月),我发现seam-gen真的能省很多时间。所以我强烈建议你也试试。

2.2.1 seam-gen’s specialty
seam-gen is an application factory, as depicted in figure 2.4. At the very basic level it
creates a skeleton project, allowing you to focus on application development with little
time spent doing setup. From there, it can take you all the way to a functional
application using its code-generation tasks. The code that it lays down can also serve
as a demonstration for how to use several of Seam’s features.

seam-gen是一个应用工厂,建立骨架项目,让你在项目设置上少花时间。之后,又会用代码生成任务来带你一路走到功能应用上。

I’ll admit that I haven’t been a huge fan of code generation in the past, mainly
because it leaves behind lots of code that’s scary to touch. With seam-gen, I have come
to realize that creating the initial ORM mappings and CRUD screens is something I’m
willing to delegate, precisely the tasks that seam-gen handles. I find that the code
seam-gen produces is quite workable and likely equivalent to what I’d have written as a
first cut anyway. Don’t assume, though, that seam-gen is for “green-field” projects only.
You can generate code in a sandbox project and then steal the code for use in other
applications. In that regard, think of seam-gen as a tool that you can call upon when
needed. If you don’t like the output that seam-gen produces, you can even tailor it by
modifying its templates.

我承认我不喜欢代码生成,主要是因为代码多了后,代码稍一变就会有问题。用了seam-gen后,我发现这些ORM及CRUD页面我还是很喜欢借用的。如果不喜欢seam-gen产生的输出,还可以自行修改模板。

TIP
seam-gen produces Java classes and Facelets view templates from Free-
Marker templates, which can be found in the seam-gen folder of the
Seam distribution. If you plan to customize the generated application,
you may want to consider making changes to these templates. This
approach allows you to customize the code without losing the ability to
reverse-engineer.

seam-gen has also proved to be immensely helpful in figuring out how to define the JPA
mappings for an existing database schema. Setting up the associations between entities
can be an extremely tedious task. Just the other day I got stumped trying to remember
how to map a complex table relationship. Is it one-to-many or many-to-one? Do I need
a composite key? To avoid wasting time, I pointed seam-gen at my database and
inspected the result. seam-gen bailed me out and helped get me back on the right page.
If you’ve spent any length of time poring through the JPA or Hibernate reference documentation
in search of a solution to these questions, seam-gen can help.

That’s just the beginning of what a seam-gen project is capable of. Let’s look at
what other features it sets up for you.

2.2.2 Features that seam-gen provides
In addition to producing CRUD applications from an existing database schema, seamgen
can work from an existing set of entity classes. In that case, the database schema can
be created retroactively by Hibernate when the application starts, thus making seamgen
attractive even when an existing database hasn’t been dropped on your desk.

seam-gen goes far beyond creating a CRUD prototype application. It sets up an
extensive set of configurations and resources that are important as you begin customizing
the application. Here’s a combined list of features, many of which are covered in
this chapter:

■ Incremental hot deployment of static resources, view templates, and page
descriptors
■ Incremental hot deployment (dynamic reloading) of JavaBean and Groovy
components (excludes entity classes and EJB components)
■ Ready-made project files for quick import into Eclipse, NetBeans, and IntelliJ
IDEA
■ Facelets view templates (a JSF-centric view technology; alternative to JSP)
■ RichFaces or ICEfaces UI components (panels, tables, and menus)
■ A custom stylesheet that reinforces the look and feel of the rich UI components
■ Bookmark-friendly search and pagination of records on the entity listing pages
■ RESTful entity detail screens; includes tabs showing the parent and child
entities
■ A lookup routine for establishing a link to a related entity in the entity editor
■ Entity model validations enforced in the UI with instant feedback using Ajax
(Ajax4jsf)
■ Basic page-level authorization that requires users to authenticate before performing
write operations (the default authentication is a stub that accepts any
credentials)
■ A component debug page, developer-friendly error page, and user-friendly
error page
■ JCA data source, JPA persistence unit, and JPA entity manager configured for target
database (Hibernate 3.2 is the default JPA provider)
■ Seeding of database from the import.sql script on classpath (Hibernate
feature)

表态资源、view模板、页描述符的热部署
JavaBean and Groovy components 的增量热部署,(动态重载)(不包括实体类和EJB部件)
生成的项目可直接导入到Eclipse, NetBeans, and IntelliJ IDEA
Facelets view模板
RichFaces or ICEfaces UI部件,(panels, tables, and menus)
UI部件风格可以自定
在实体编程器中,用回路路径来建立到相关实体的链接
实体模型校验
页面级别的用户权限验证
debug页面、开发者友好的出错页面、用户友好的出错页面
JCA数据源,JPA存储单元、JPA实体管理器
用import.sql脚本产生数据库

While seam-gen does crank out a working application, by no means is it going to put
you out of a job. Although the CRUD application it creates is very capable, which you’ll
see later, it’s no replacement for hand-crafted work. You have to take it for what it is: a
prototype. It lacks the business knowledge necessary to properly categorize the entities,
instead treating every entity with equal significance. This limitation is especially
noticeable if you have a large schema with lots of lookup tables. seam-gen also falls
short on rendering binary data properly in the user interface because there’s no way
for seam-gen to know if that data represents an image, a document, or an encrypted
stream of data. But these shortcomings just give you room to add your personal
touch—and give you a job.

seam-gen生成了应用之后 ,并没有把你排除到项目之外,手工打造还是无可替代的。原型是没有具体业务的知识的。另外,如果你有很多的查询表,seam-gen也会不适应。对于二进制文件,seam-gen也难于处理,因为它不知道该如何重画二进制文件如图像、文档及加密的数据流。

The point to take away from this section is that seam-gen eliminates all the tedious
work that you don’t want to do anyway. It sets you up with a foundation and turns the
project over to you, letting you rapidly develop and customize the application as you
see fit. Let’s now follow Gavin’s advice and use seam-gen as the starting point for the
example application. At last, we put seam-gen to the test.

2.3 Kick off your project with seam-gen
There are two versions of seam-gen, a command-line script and an IDE plugin. The
command-line version, which is a wrapper around Ant, has the advantage that both it
and the project it produces can be driven from the command line or from an IDE.
The prominent IDE version of seam-gen is an Eclipse plug-in that’s part of the JBoss-
Tools suite. The JBossTools project hosts a variety of standalone Eclipse plug-ins that
support Java EE development. These plug-ins get bundled with the JBoss Developer
Studio (JBDS),3 a set of Eclipse-based development tools preconfigured for the JBoss
Enterprise Middleware Platform and Red Hat Enterprise Linux. This section primarily
focuses on the command-line version of seam-gen, with a brief discussion of the
Eclipse plug-in. I don’t go into much detail about the IDE version since user interfaces
tend to fall out of date quickly. You can find references to tutorials for using the
Eclipse plug-in on the book’s online companion resources.

有两个版本的seam-gen,一个是命令行,另一个是IDE插件。命令行的是用ANT的。插件已纳入
JBoss-Tools suite,这些部件包含在JBoss DeveloperStudio (JBDS)中,本结主要涉及命令行的。我不想讲太多IDE版的是因为用户界面变更太快。

I begin by giving you an overview of the seam-gen command-line script; then we
plunge into executing the seam-gen commands.

2.3.1 A look at the seam-gen commands
You should have already downloaded the Seam distribution. If you haven’t, please
refer to appendix A, section A.2. You should also have the example H2 database prepared
as described in section 2.1.2. Once you have all of the prerequisites, change
into the directory where you extracted Seam. In that directory you’ll find two scripts,
seam and seam.bat. The former is used on Linux/Unix platforms and the latter on
the Windows platform. To perform seam-gen operations, you type seam followed by
the name of a command from the root directory of the Seam distribution.4

如果你还没有下载SEAM,请参考附A,

Let’s begin by asking the seam script what it can do. The script’s capabilities are
provided by the help command. In your console, type seam help and press the Enter
key. The output of this command gives a brief description of seam-gen and a long list
of all the commands it supports. That description is shown here:
seam (aka seam-gen) - Execute seam code generation.
The seam.bat (Windows) and seam (Linux/Unix) scripts support
commands that use Ant build targets to set up a Seam project and
generate source code. Ant is not required to be on your path to
use this script.
JBoss AS must be installed to deploy the project. If you use EJB3
components, the JBoss server must have EJB 3 capabilities.
(JBoss AS 4.2 is strongly recommended)

The list of commands that the help command spits out can be divided into two categories.
The first set of commands, shown in table 2.1, are used to set up, manage, and
deploy a seam-gen project.

The second set of commands, shown in table 2.2, is used for generating code for a
project that seam-gen is currently managing.


前两个反而不说了,太常用了。
update-project:用来用最新的依赖库更新项目。
delete-project:撤部署并删除项目
deploy:将项目包及数据源部署到JBoss AS
undeploy:与deploy反向
explode:比deploy多了个附加的热部署
restart:重启前面部署的项目,不是重启JBoss AS
archive:生成项目包并放到项目的根目录

test:在生成的项目中进行测试
settings:显示当前seam-gen/build.properties中的设置
reset:清除seam-gen/build.properties中的设置,回到出厂设置

generate:从现存的数据库生成JPA实体类,并产生CRUD用户界面来显示、管理它们
generate-ui:产生CRUD用户界面来显示、管理它们
generate-model:从现存的数据库生成JPA实体类


Don’t worry right now about understanding each and every command. You’re going
to get an opportunity to study most of the commands individually throughout the
course of the book. These two tables should give you a feel for the capabilities of seamgen
before we take it into battle.

现在不完全懂不要紧,本书中会有机会用到这里的大部分。

TIP
If you’re super shell savvy, you can source the bash completion script in
seam-gen/contrib/seam-completion to get tab completion for the seamgen
commands in Unix.

Table 2.3 lists the steps that you will take to create the Open 18 prototype application.


Once you’ve completed the steps in table 2.3 and started JBoss AS, you’ll be ready to
show the Open 18 prototype to your boss. If he asks for changes, you won’t be at a loss
for words. Customizing a seam-gen application is very straightforward. Later in the
chapter you learn how to get your hands dirty with the application by checking out
the “instant change” development environment that seam-gen sets up and by deploying
to multiple environments. Before you can do any of that, you have to inform seamgen
about your project so that it knows what to create for you.

2.3.2 A Q&A session with seam-gen
Enough watercooler talk. Let’s get to work! You begin by running the seam setup command.
When you run this command, it launches a series of questions that allow seamgen
to gather the information it needs to create your project. Each line consists of
three parts: the question, the current value, and a list of valid responses (if applicable).
For each question, enter a response and press the Enter key to continue to the
next question. To create a working application, this is the only real work that you have
to do: seam-gen takes over once it has the information it needs.

Listing 2.1 shows the full seam-gen setup questionnaire along with the responses
used for creating the Open 18 prototype in the WAR archive format. Anytime you see
/home/twoputt in a response, replace it with your development folder (according to
the layout explained in the README.txt file of the book’s source code).

NOTE
If you’re using a Windows OS, use the forward slash (/) when entering
file paths (e.g., C:/twoputt) in the seam-gen interview, particularly when
entering the path to the H2 database. The default file separator in Windows
is the backslash, which in Java is the escape character. For Java to
accept a literal backslash, you have to escape it with another backslash
(). You can avoid this whole issue by always using forward slashes in file
paths, regardless of the operating system. Java is smart enough to convert
the forward slash to the correct file separator for the host platform.

Let’s start the interview by typing the following:

The goal of the seam setup command is to populate seam-gen/build.properties—
overwriting it if it already exists—with the settings used to create the seam-gen
project, which is the next step. The build.properties file stores your responses in the
typical Java properties file format, using key-value pairs separated by an equals sign (=).
If you mess up any of the questions, don’t fret—you can always run through the setup
again. Seam will remember the responses from your previous run through, as long as
you ran it to completion. You accept your previous response for a question by pressing
Enter. If you’d rather not use the setup wizard, you can just edit the seam-gen/
build.properties file manually. The wizard just makes the task more user-friendly.

The responses in listing 2.1 prepare seam-gen to create a WAR project. You choose
the WAR format if you plan on developing JavaBean components rather than EJB 3
components and you want to take advantage of incremental hot deployment. If you
want to create EJB 3 components, the EAR format is the required choice. You’ll learn
about both of these archive formats, as well as incremental hot deployment, later in
the chapter.

Choosing ICEfaces over RichFaces
ICEfaces and RichFaces are JSF component libraries for building rich, Web 2.0 user
interfaces based on Ajax. seam-gen is capable of generating projects that use either
ICEfaces or RichFaces, made possible by two equivalent sets of view templates that
are maintained under the seam-gen folder of the Seam distribution. seam-gen selects
the appropriate template folder based on your response in the setup questionnaire
and caters the application to that JSF component library.
If you choose ICEfaces, seam-gen automatically uses the bundled version, thus
requiring no setup on your part. If you’d like to have seam-gen use the version of your
choosing, you specify the root folder of the ICEfaces binary distribution when you’re
prompted for it in the seam-gen setup.
Because RichFaces is a JBoss project, you can understand why RichFaces is the
default choice in seam-gen, but the ICEfaces contributors ensure that the generated
application works equally well in ICEfaces. I encourage you to give both a try and
decide for yourself.

In addition to creating and populating the seam-gen/build.properties file, the setup
command copies the JDBC driver to JBoss AS to allow the database connection to be
defined as a JCA data source. In our case, the H2 JDBC driver, h3.jar, is copied to
${jboss.home}/server/default/lib. The JCA data source is registered in the Java Naming
and Directory Interface (JNDI) upon deployment of the project, where the JPA
persistence unit can access it. The configuration of the data source and the persistence
unit are tasks handled by the create-project command, covered next.

2.3.3 Creating a basic project structure
The setup command merely prepares seam-gen to create a project. To actually have
Seam transform its templates into a newly forged project, you must execute

建立一个基本的项目结构
生成的基于SEAM的项目,具有Ant的编译、测试、打包、部署功能。


seam create-project
When you execute this command, Seam will create a new project in your Java workspace
directory and populate it with everything you need to start developing a Seambased
project, complete with an Ant-based build for compiling, testing, packaging,
and deploying the application. Listing 2.2 shows the output of the create-project
command. You’ll see a lot of other output fly by, which you can ignore.

The project is now ready. As you can see in this output, seam-gen provides instructions
to keep you moving right along. It tells you to run create-project after running the
setup command and seam explode after running the create-project command. However,
you’ll likely agree that setting up a project, particularly filling out the questionnaire
shown here, is best suited for a windowing environment. If you’re turned off by the command-
line interface, I have good news for you. The JBossTools plug-in for Eclipse wraps
these steps into the Seam project creation wizard, which allows you to create a seam-gen
project just like you would any other Eclipse project. One of the screens from this wizard
is shown in figure 2.5. This wizard provides a much more interactive experience and lets
you leverage existing servers, runtimes,
and database connection profiles that
you have set up in Eclipse.
A step-by-step tutorial of the JBoss-
Tools plug-in won’t be covered here. If
you’re interested in using it, you should
be able to apply the foundation knowledge
that you have learned in this chapter
to that wizard. Be aware that the project
the JBossTools wizard creates is not
exactly the same as a project created by
the command-line script. It’s designed to
be used with an IDE (i.e., Eclipse) and
only an IDE—meaning it doesn’t have a
universal build script. My preference is to
stick with the the command-line script
(herein referred to as seam-gen) because
it allows me to use the generated project
from the command line or in the IDE of
my choice (any IDE that can drive an
Ant build). This flexibility is demonstrated
repeatedly throughout the rest of
this chapter.

JBossTools可以让你使用Eclipse已有的服务器、运行时、数据库连接。
这些在这里不多说了。

2.3.4 Generating the CRUD
You now have a basic project scaffolding that’s ready to deployed. But let’s keep the code
generation going by doing some reverse engineering first. That is the function that the
seam generate command5 performs. This command generates JPA entity classes from
an existing database schema and a CRUD user interface to manage these entities rendered
from Facelets view templates and supported by JavaBean action classes.

seam generate命令从已有的数据库生成JPA实体类,并生成一个CRUD的用户界面,结全Facelets view模板和JavaBean action类来管理这些实体


NOTE
By supplying values for the schema and catalog in seam setup, we ensure
that the @Table annotation placed on each JPA entity class remains agnostic
to these data-specific settings. The schema and the catalog are more likely
to change than the table and column names, so it’s best not to tie your
entity classes to these values. If necessary, the default schema and catalog
can be set in the Hibernate or JPA configuration using the properties
hibernate.default_schema and hibernate.default_catalog.

JPA实体类上加的@Table到目前为止依然是不为所知的。这些数据比表名或字段名更易变更,所以不要将其绑定到你的实体类,如果有必要,则可用hibernate.default_schema and hibernate.default_catalog属性在Hibernate or JPA配置中进行设置。

Next, kick off the reverse engineering:
seam generate
The output for the generate command is quite extensive as Hibernate feels the need
to keep us informed of its reverse-engineering progress every step of the way. A truncated
version of that output is shown in listing 2.3.

下面就用seam generate来做反向工程

Believe it or not, that’s it! The prototype application is built. Ah, but we aren’t quite
ready for deployment. If you were to run seam restart now, as the generate command
instructs, you would find that unless you already had JBoss AS running, the
request for the URL http://localhost:8080/open18 would simply give you a 404 error
page. To put the application in motion, you need to boot up JBoss AS. In the next section,
you learn about the two options you have for deploying the application, how it
affects development, and finally how to start JBoss AS so that you can see the application
that seam-gen has prepared for you.

注:
restart不会帮你启动JBoss AS

2.4 Deploying the project to JBoss AS
As I mentioned earlier, seam-gen produces
projects that are configured to
deploy to JBoss AS out of the box. For
now, let’s stick with the convention to
keep the effort of getting the application
running to a minimum. Once
you’re comfortable with that, you can
explore alternatives.

You can take one of two paths when
deploying your application to JBoss AS.
You can either deploy it as a packaged
archive:

seam deploy
or you can deploy it as an exploded
archive:
seam explode
These two alternatives are illustrated in
figure 2.6.

NOTE
Despite its alarming name, the explode command will not perform a destructive
process on your project. It simply copies the deployable files to the application
server without packaging them. The name is meant to indicate that the
archive is broken open as if it had been “exploded.” A more reassuring name
for this command might have been “unfurl” (like a sail). But explode just sort
of stuck.

explode只是暴露出去,优点是可以热部署。

Let’s weigh the attributes of each command and decide which one is best to use.


2.4.1 To deploy…
If you use the deploy command, the build will create an archive that uses one of two standard
Java EE package formats, Web Archive (WAR) or Enterprise Archive (EAR), depending
on which one you chose during the seam-gen setup routine. If you plan to use only
JavaBean components, like the example application presented in this chapter, your best
choice is the WAR format. In this case, the deploy command packages the application
into the WAR file open18.war. If you intend to use EJB components in your application,
you must use the EAR format. In that case, the deploy command will create the EAR file
open18ee.ear for an application named open18ee. The sample code that accompanies
the book includes projects for both archive formats. Note, however, that once you make
a choice between WAR and EAR, you’re stuck with it. There is no built-in seam-gen command
to toggle a project between the two archive formats.

Packaging Java EE applications
There are three prominent archive types that can be used to deploy components to
a Java EE–compliant application server. A Web Archive (WAR) is the format with
which most people are familiar. A WAR bundles servlets, JSPs, the archive is static
resources, and supporting classes for the purpose of serving dynamic web pages.
EJBs are deployed in the pervasive Java Archive (JAR) format, except that the
archive is augmented with a special XML deployment descriptor that identifies it as
an EJB JAR. Both the WAR and JAR formats are Java EE modules that can be bundled
inside an Enterprise Archive (EAR), which deploys the containing modules
under the same classloader, presenting them as a single application. The EAR is
the standard packaging unit for a Java EE application. Basic servlet containers,
such as Tomcat, can only handle WAR packages.

WAR打包servlets, JSPs,包是表态的,支持服务于动态页面的类。EJB用JAR来部署,EAR是标准的Java EE application的打包方式。基本的servlet容器,如Tomcat,只能处理WAR包。

Once the archive has been packaged, seam-gen moves it to the deployment directory
in the default domain of the JBoss server, located at ${jboss.home}/server/default/
deploy. The JBoss server monitors this directory for changes. When a new or changed
archive is detected, the application is “reloaded.” This feature is known as “hot
deploy,” but it’s not just about deployment. The server also recognizes when an
archive has been removed from this directory and subsequently unloads the application.
You can remove the deployed archive by running

一旦打包完成,seam-gen将其部署到JBoss服务器的默认目录${jboss.home}/server/default/
deploy。JBoss监视这一目录的变化,当有新内容时,变化就被加载。这被称为热部署。当发现有被删除的,也会马上生效。

seam undeploy
The downside to using a packaged archive is that every change you want to apply
requires a complete build, package, and deploy cycle. At the end of this cycle, a fresh
version of the archive is shipped off to the server, the old application is unloaded,
and finally, the new application is loaded. In this case, we’re talking about hotdeploying
the application as a whole rather than incremental changes, which is covered
later. It’s all or nothing. During a reload, the JBoss server shuts down all the services
that were started by your application. These services include the database
connection, the JPA EntityManagerFactory (or Hibernate SessionFactory), the
Seam container, and perhaps others. When the new application is deployed, all of
these services are started again under the new deployment. This process can be very
time consuming—and that time increases as your project grows and more services
have to be stopped and started.

使用打包的部署的不便之处是每次变化都要经过一个完整的部署周期,这里我们谈的热部署则是完全部署,不是增量部署。在这个过程中,JBoss服务器停止所有你的应用的相关服务,包括数据库链接,JPA实体管理工厂EntityManagerFactory(or Hibernate SessionFactory),新的应用部署后,所有服务重新开启。这个过程很可能很耗时,当你的项目越来越大,服务越来越多时,更是这样。

To make matters worse, reloading an application terminates all of its active HTTP
sessions. If the page you’re testing requires you to be authenticated, or otherwise
holds state, you’ll be forced to reestablish your place. Needless to say, the packaged
deployment isn’t recommended for development. The good news is that there’s a better
way.

更糟的是会停止所有活动的HTTP sessions.如果你的测试页是要授权的,或操存着状态的,则必须以后再重建,已不用多说了,这种打包式部署在开发阶段不宜使用。

2.4.2 …or to explode
The alternative to a packaged deployment is an exploded archive. An exploded
archive is a WAR or EAR that has been extracted into a directory that has the same
name as the archive. You can deploy the application as an exploded archive using the
seam explode command.

或只是简单暴露?
这种方式是以与包同名的目录出现,用seam explode命令来做。

The explode command is a slight variation on the deploy command. Like the
deploy command, the explode command assembles the deployable files into the structure
of the archive in the build directory. But the explode command skips the bundling
step and just copies the build directory in its raw form to the server. Subsequent calls to
seam explode synchronize the changed files to the exploded archive directory on the
server, thus supporting incremental updates. How the application deals with its
changes is determined by its hot deployment capabilities. Section 2.5.1 examines incremental
hot deployment and what it means in terms of development.

所有新的变化是部署到服务器的这个目录中,用这各方式来实现增量更新。

To force a reload of the application when using an exploded archive deployment,
mimicking the behavior that occurs when a package archive is dropped into the hot
deployment directory of JBoss AS, you use the restart command. As its name may
imply, this command doesn’t restart the JBoss AS—it just reloads the application.

The restart command begins by mirroring the work done by the explode command.
As a final step, it updates the timestamp of the application deployment descriptor,
which is application.xml, in the case of an EAR deployment, or web.xml, in the
case of a WAR deployment. When the application server detects that the timestamp of
the application deployment descriptor has changed, it triggers a reload. To remove an
exploded archive, and have it undeployed, you execute
seam unexplode
The explode command is best suited for development. With support for incremental
updates, it gives you the “instant change” capabilities that you may be familiar with if
you’ve ever developed in a scripting language like PHP. The degree to which a Seam
application supports instant change will be put to the test later.

restart做类似explode的工作,在最后,对于EAR部署,更新application.xml的时间戳,对于WAR部署,更新web.xml,当应用检测到有变化时,则触发重新加载。

explode是最适用于开发的。支持增量更新。可以立刻更新,

To summarize your options, you can use the deploy command to deploy a package
archive or you can use the explode command to deploy an exploded archive. The
undeploy and unexplode commands serve as their complements by removing the
archive from the server. That covers the deployment life cycle to a single environment.
seam-gen sets up your project with additional deployment profiles, which allow you to
tune the archive based on environment, possibly even deploying it to a different server
instance. Let’s take one more tangent to learn about deployment profiles before we
move on to starting JBoss AS.

2.4.3 Switching between environments
The build that seam-gen creates for your project supports the concept of deployment
profiles. Profiles can be used to customize deployment descriptors and configuration
settings for different environments. To cite a common case, you may need to use a different
database in production than the one used in development. You certainly don’t
want to be burdened with having to switch the database connection settings every time
you cut a production release. Instead, you can set up a profile for that environment,
which you can activate with a single flag when executing the production build. The
appropriate database settings, and any other settings specific to that environment, will
then be applied.

Seam configures three profiles out of the box: dev, prod, and test. The test profile
is a special case, which we look at next. By default, Seam uses the dev profile when commands
are executed. You can enable a different profile by changing the value of the Ant
profile property. Adding the following key-value pair to the build.properties file in the
root of the project activates the prod profile, effectively deactivating the dev profile:

SEAM配置了三个样本,dev, prod, and test。test是一种特殊的情况,我们后面再讲,SEAM默认使用dev,你可以使用Ant的profile来对此作出修改,增加如下的键值对,
profile=prod
到build.properties文件,
如果改build.properties文件感觉太麻烦,可以在命令行直接加参数如下:
seam clean deploy -Dprofile=prod

profile=prod
Having to change the build.properties file feels too manual for my taste. As another
option, you can set the profile property from the seam command:
seam clean deploy -Dprofile=prod

TIP
When you switch profiles, it’s a good idea to run clean—and possibly
even undeploy or unexplode—prior to running deploy or explode to be
sure that settings from the previous profile do not linger.

当你改样本时,在运行deploy or explode之前,最好运行clean,甚至undeploy or unexplode,

As I mentioned, the test profile is handled as a special case. It’s not activated using the
profile property. Instead, there are special Ant targets for running tests that know to
use the test versions of the environment-specific files. Writing and executing tests will
be covered in chapter 4.

test样本是用于测试的, 这在第四章再讲。

Table 2.4 itemizes the files that are selected according to the active profile, indicating
whether the file is used in the case of the test profile. These files contain settings
and configurations that you have control over between environments. You can introduce
your own profile, such as qa, by creating each of the files listed in table 2.4.
When naming the file, replace the token %PROFILE% with the name of the profile.
For instance, the build properties file for the qa profile would be build-qa.properties.


If you want to change the location of the JBoss AS deploy directory for a production
build, you can set the jboss.home property in build-prod.properties. You probably
want to disable debug mode as well, since it’s only desirable in development:
jboss.home=/opt/jboss-production
debug=false

You now know how to deploy and undeploy the application to the JBoss AS deployment
directory, both as a packaged archive and an exploded directory structure. You
can also control the settings and configurations for the target environment, such as
development, QA, or production, by toggling the value of the profile property. Without
further ado, let’s fire up JBoss AS and see what the application looks like.

2.4.4 Launching JBoss AS
Without any customization, JBoss AS runs on port 8080, so first ensure this port isn’t
already in use (be aware that Apache Tomcat also runs on port 8080 by default). In your
console, navigate to the JBoss AS installation directory, ${jboss.home}, and then descend
into the bin directory. If you’re using a Unix platform, execute the command:
/run.sh
If you’re on Windows, execute the command:
run
These scripts start the default domain of the JBoss AS server. As an alternative to using
the console, you may also choose to launch JBoss AS from your IDE. Eclipse, NetBeans,
and IntelliJ IDEA all support launching the JBoss Application Server.

Keep an eye on the console while the server is starting to watch for any exceptions
that may occur. When the console output settles down, the final line should give you
the thumbs up that JBoss AS is running:
00:00:00,426 INFO [Server] JBoss (MX MicroKernel) [4.2.2.GA (build:
[CA]SVNTag=JBoss_4_2_2_GA date=200710221139)] Started in 17s:14ms
If you’re using the Sun JVM, I can almost guarantee that when you start hot-deploying
applications to JBoss AS, you’re going to get out-of-memory errors using the default JVM

Sun JVM options when running JBoss AS
While you’re on the task of setting up the JBoss AS runtime, I recommend a set of
JVM options to use when running JBoss AS with the Sun JVM. The default memory
allocation settings in Java are extremely conservative. On top of that, Sun’s JVM has
a concept of PermGen space, a separate allocated memory region from the heap.6
Even though the JVM garbage collector frees up memory automatically, there are certain
objects (such as class and method objects) that can evade removal because
they are in this isolated memory region. A flag must be provided that enables garbage
collection for the PermGen space. In addition, the garbage collector has trouble keeping
up when memory is tight. The limited resources quickly become a problem when
running a hefty application like JBoss AS that has high memory demands.

当用Sun JVM时,建议改一下JVM参数,默认的极为保守,JVM有一个PermGen概念,heap之外的一个内存区,虽然有垃圾收集器,有些对象如类和方法对象,可以逃过此劫,就是因为它处于此区域。这一区域要处理的话必须在设立一个标志。当内存很少时,就要常调动垃圾收集器,有限的资源很快就会成为一个问题,因为像JBoss AS这样的程序会要求较大的内存。

To avoid an untimely death of the JVM, and in turn, JBoss AS, you should supply the
following parameters in the VM options of the JBoss AS runtime configuration in your
IDE (or bin/run.conf in the JBoss AS home directory). I have never experienced an
out-of-memory error when using these settings.7
-Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcInterval=3600000
➥-Dsun.rmi.dgc.server.gcInterval=3600000
➥-XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled
➥-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m
➥-Xverify:none
There are ramifications of using the Concurrent Mark Sweep (CMS) garbage collector,
such as a longer startup time, so I wouldn’t blindly apply these settings to production.
However, I have found them to be sufficient for development purposes. The other

为了避免JVM不适时地死掉,可用如下的参数。
在工厂环境我不会用这种参数,但开发时,这很有用。

options. I strongly urge you to follow the advice in the accompanying sidebar. These settings
can be applied to the JBoss AS runtime configuration either by adding them to the
${jboss.home}/bin/run.conf or by using the configuration screen in the IDE.
If6 JBoss7 AS started cleanly, you can open your browser and point it to http://localhost:
8080/open18 to see the result of your work. In the next section, I walk you
through the application and point out the work seam-gen has done for you. I then
demonstrate how you can continuously make changes to the application without having
to suffer the wait of restarting either the application or the server.

如果干净地启动,你就可以在http://localhost:8080/open18
看到你的工作成果了。
随后我展示是怎样在不断增加改进而不用重启服务器的。

2.5 Show and tell, change, and repeat
You might recall the activity of “show and tell” from your school days. In today’s show
and tell, I will be demonstrating the Open 18 prototype application that you have
created thus far using seam-gen, both from outside and the inside. This exercise is
important because it’s necessary to know how seam-gen assembled the application so
that you’re familiar with how to make modifications to it. In this section, you learn
how you can alter the application by controlling the reverse-engineering process. In
the next section, you learn how to make changes after the fact. Before we get to that,
it’s time for the unveiling.

Ta-da! The opening screen of the Open 18 application is shown in figure 2.7.

本段,我会学会如何用控制反向过程的方式来改变你的应用。

I’ll admit, the home page isn’t all that interesting. At the very least, it’s clear that the
application has a nice look and feel, courtesy of RichFaces, which is typically rare for
starter applications. The folks on the floor of the trade show pavilion probably don’t
care to read about the benefits of seam-gen, though. Thankfully, there is an email
from the marketing team waiting for you in your inbox with flashier markup for this
page. You’ll soon learn how changes such as these can be deployed in real time during
development, as if you were editing the page live.
Notice at the top of the page there are links for each of the entities in the data
model and another link encouraging you to authenticate. Clearly there is some substance
behind this curtain. Let’s dig deeper to find out what’s inside.

2.5.1 Walking the course
From the top-level menu in figure 2.7, you can verify that seam-gen produced five
entities: Facility, Course, Hole, TeeSet, and Tee. The List suffix on each link indicates
that these pages are listings. We’ll focus on these pages first and then see where else
they take us.

图中可以看到5个实体:Facility, Course, Hole, TeeSet, and Tee.

ENTITY LISTINGS
Clicking one of the entity links at the top of the page brings up the listing for that
entity. In this walkthrough, we’ll focus on the course listing, shown in figure 2.8. However,
the tour I am about to give applies to the other entities as well.

实体列表
点击顶端的实例列表,会显示出实体的列表,本例中我们关注课程列表,

The listing screen has two parts. The featured content is a table of all the courses
found in the database, seen in the bottom half of the screen. This table has two key
features. It can be sorted and paginated. The result set is broken into pages, truncated
at a page size of 25 by default. Links at the bottom of the screen allow you to flip
through the pages in the result set. The columns of the table are also sortable. The
heading of each column is rendered as a link, allowing you to sort the table by that
column. Now, tell me, how much time have you burned implementing sortable and
paginated tables in an application in your career? If you winced at that question, you
should be happy to see that seam-gen takes care of all that for you. So go ahead and
revel in it by clicking on a couple of the column headers and flipping through the
pages. You should notice that the sort is not lost when you paginate, the offset is not
lost when you sort, and every page can be bookmarked. Another seam-gen special.

这个表有两个特点,可以排序及分页。另外这个页还是可以收藏为书签的。这是SEAM另一个特别之处。

The page also includes a search feature. The search form, shown in the top half of figure
2.8, includes an input field for every string property of the Course entity class. You
can type text in any of the fields that appear above the table and Seam will use that
text to perform a search to filter the rows displayed in the table. The best part is that
when you sort and paginate, your search filter is not reset. In the next chapter you
learn the technique that powers this functionality.

这页还包含了一个搜索功能。最好的地方在于,当你排序和分页的时候,搜索过滤器内的内容没有被清除。下一章你如学到如何利用这一招。

From the course list page, you can either create a course or you can view the details
of a particular course. Let’s drill down to see the details of one of the courses.

DRILL-DOWNS
In the last column of the course listing is a link to view the course. When you click this
link, it brings up a page showing the details of the course, shown in figure 2.9.
The course detail screen shows all the properties of the course at the top of the screen
and each of the course’s related entities at the bottom. Since each course has 18 holes,

最后一列是查看细节,点击后进入细节页面。

there are 18 rows in the table in the holes tab pane. The teeSets tab pane shows a table
with rows for each set of tees (the colored markers at the start of each hole). Each course
is also associated with a golf facility, the details of which are shown in the facility tab pane.
You can continue to drill down further by clicking the View link in the row of any related
entity. But the application really gets interesting when you add a new record or edit an
existing one.

可以点击以到处看看,不过最有趣的是加入新记录或编辑一个现有的。

ENTITY EDITORS
There are buttons for both editing and adding entities. However, pages that alter the
state of the database require you to be logged in. When you click one of these buttons,
you’ll be directed to the login screen, shown in figure 2.10. The login requirement is
enforced in the page descriptor that coincides with the JSF page. For the Course
entity, the editor page is CourseEdit.xhtml and its page descriptor is CourseEdit.
page.xml. You’ll learn about page descriptors in chapter 3.

这些按钮可以编辑或增加实体。不管怎样,一个动了数据库的操作是需要先登录的。当你点击按钮后,你被带到登录页面。这一限制由JSF的页描述符来决定 。对于课程实体,编辑页是CourseEdit.xhtml。页描述符是CourseEdit.page.xml。页描述符在第三章中讲。

Any credentials will do since the authentication module is just a stub. You learn how
to tie authentication to the database and how to further lock down pages in chapter 11.
Once you authenticate, you’re taken to the course editor, shown in figure 2.11. If
you’re updating an existing record, Seam will apply the changes to it when you click
Save. Otherwise, a new record will be inserted into the database.

因为这里的论证还只是个架子 ,可以随意通过。在第11章后你就会增加真正的限制了。
在编辑页面点击SAVE后,SEAM将变化存入数据库,没有时则建立一个。

At first glance, the forms for editing an entity appear straightforward, perhaps even
a bit dull. A closer look reveals that they are enriched with subtle features that would be
very time consuming to prepare. The * in figure 2.11 indicates a required field or relationship.
At the bottom half of the page is a button that lets you select a facility to satisfy

一开始看时,感觉直接编辑实体显得有些呆,其实这里已提供了精巧的特性。


the foreign key relationship. If you delete a record, it will cascade, deleting child records
as well. All of these features are a direct reflection of the database constraints. Let’s take
a closer look at how the column constraints are translated into form requirements,
which happens when seam-gen reverse-engineers the database schema.

下半页的按钮让你解决有外键时的情况。如果你删除一条记录,则会级联删除其子记录。

DATABASE COLUMN TO FORM FIELD
The seam generate command delegates most of its work to Hibernate’s reverse-engineering
tool. In that step, the database tables are translated into entity classes and the
columns become properties on the respective classes. The properties on the entity
class manifest as form fields in the entity’s editor during the creation of the CRUD
screens. To get from the database to the UI, the database column names

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI