1. Trang chủ
  2. » Công Nghệ Thông Tin

Seam Framework Experience the Evolution of Java EE 2nd phần 10 pot

50 822 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 3,16 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

• Change the application name as follows: APPENDIX B USING EXAMPLE APPLICATIONS AS TEMPLATES 428 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com... The differenc

Trang 1

B.1 Simple EJB3-Based Web Applications

The integration example is the best starting place for an EJB3-based Seam web

application This is the directory structure of the source project:

To customize the project for your application, follow these steps:

• Add Seam components and other classes in the src directory.

• Add web pages, images, and other web resources in the view directory.

• Edit the build.xml to include any third-party library files required for your

appli-cation through either the war or eartask For instance, you can include the Ajax4jsf

JARs, as we did in Chapter 20.

• Change the resources/WEB-INF/navigation.xml file to define the navigation

rules (i.e., the pageflow) in the new application.

• Edit the resources/WEB-INF/pages.xml file to include page parameters for

RESTful pages (see Chapter 15), page actions, and stateful navigation rules (see

Section 24.5).

• Change the resources/META-INF/persistence.xmlfile to specify custom

persis-tence options for the new application, if any (see Chapter 28 for some examples).

• Change the application name as follows:

APPENDIX B USING EXAMPLE APPLICATIONS AS TEMPLATES

428

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 2

• Change the project name "integration" in the build.xml file to your own

project name (e.g., "mywebapp").

• Change the resources/META-INF/application.xml file to reflect your

application’s context root URL.

• Change the class loader name in resources/META-INF/jboss-app.xml to a

unique name that fits your application.

• Change the JNDI name pattern in the resources/WEB-INF/components.xml

file to match your application name (i.e., "mywebapp").

JSP versus Facelets XHTML

The integration project template uses Facelets as the presentation technology We

highly recommend using Facelets in your Seam applications (see Section 3.1) Still, if you

really want to use JSP for web pages, you can use the helloworld example as the template.

The setup is similar to the integration project setup we discuss here.

Then run ant in the project directory to build the application The build result is in the

build/jars/mywebapp.ear file This is the structure of the EAR archive:

If you have unit tests or integration tests for the application, you can put the test cases

(the.java files) and the testng.xml file in the test directory in the project An

alter-native components.xml file is already in the test directory The difference between

test/components.xml and resources/WEB-INF/components.xmlis that the test version

429

B.1 SIMPLE EJB3-BASED WEB APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 3

does not have the application name in its JNDI pattern (see Section 26.4)—because

the tests are run outside the application server container So if you customize the

resources/WEB-INF/components.xml file in your application, you must make

the same changes to the test/components.xml file This is an example test/

Likewise, there is a test version of the persistence.xml file, which explicitly specifies

the JBoss Transaction Manager lookup in the testing environment You probably also

want to have Hibernate create and drop the database tables on the fly when you start

and finish your tests.

When you run ant test in the project directory, the build script runs all the tests defined

in the test/testng.xml file and outputs the test results both to the console and to the

build/testout directory.

For your reference, we list the complete build.xml script here:

<project name="HelloWorld" default="main" basedir=".">

<property name="lib" location="${seam.home}/lib" />

<property name="applib" location="lib" />

APPENDIX B USING EXAMPLE APPLICATIONS AS TEMPLATES

430

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 4

<path id="lib.classpath">

<fileset dir="${lib}" includes="*.jar"/>

<fileset dir="${applib}" includes="*.jar"/>

</path>

<property name="testlib" location="${seam.home}/lib/test" />

<property name="eejb.conf.dir" value="${seam.home}/bootstrap" />

<property name="resources" location="resources" />

<property name="src" location="src" />

<property name="test" location="test" />

<property name="view" location="view" />

<property name="build.classes" location="build/classes" />

<property name="build.jars" location="build/jars" />

<property name="build.test" location="build/test" />

<property name="build.testout" location="build/testout" />

<target name="test" depends="compile">

<taskdef resource="testngtasks" classpathref="lib.classpath"/>

<fileset dir="${build.classes}" includes="**/*.*"/>

<fileset dir="${resources}" includes="**/*.*"/>

</copy>

<! Overwrite the WEB-INF/components.xml >

<copy todir="${build.test}/WEB-INF" overwrite="true">

<fileset dir="${test}" includes="components.xml"/>

</copy>

431

B.1 SIMPLE EJB3-BASED WEB APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 5

<! Overwrite the META-INF/persistence.xml >

<copy todir="${build.test}/META-INF" overwrite="true">

<fileset dir="${test}" includes="persistence.xml"/>

Trang 6

B.2 POJO-Based Web Applications

If you want to use Seam POJOs and forgo the EJB3 session beans, you can choose the

jpaproject as the template (see Chapter 4) This project builds the application into a

WAR file deployable in the J2EE 1.4-compliant profile of the JBoss AS 4.0.5+ With

a little tuning, you can build WAR files deployable on any J2EE 1.4 application server

(e.g., WebLogic or Sun Application Server).

The following listing shows the structure of the jpa project:

433

B.2 POJO-BASED WEB APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 7

To customize the project for your application, follow these steps:

• Add Seam components and other classes in the src directory.

• Add web pages, images, and other web resources in the view directory.

• Edit the build.xml file to include any third-party library files required for your

application through either the war or eartask For instance, you can include the

Ajax4jsf JARs, as we did in Chapter 20.

• Change the resources/WEB-INF/navigation.xml file to define the navigation

rules (i.e., the pageflow) in the new application.

• Edit the resources/WEB-INF/pages.xml file to include page parameters for

RESTful pages (see Chapter 15), page actions, and stateful navigation rules (see

Section 24.5).

• Change the resources/META-INF/persistence.xmlfile to specify custom

persis-tence options for the new application, if any (see Chapter 28 for some examples).

For Hibernate applications, modify the resources/hibernate.cfg.xml file as

needed.

• Change the application name as follows:

• Change the project name "jpa" in the build.xml file to your own project name

(e.g.,"mywebapp").

• Change the resources/WEB-INF/jboss-web.xml file to reflect your

application’s context root URL as needed.

APPENDIX B USING EXAMPLE APPLICATIONS AS TEMPLATES

434

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 8

Runant in the project directory to build the build/jars/mywebapp.war application

archive Required application library JARs are included in the WEB-INF/lib directory.

This is the content of the WAR file:

Running tests in a POJO project is the same as in an EJB3 project The unit tests and

integration tests are located in the test directory Since we have no EJBs here, there

is no need to have a test-specific version of components.xml for the POJO application.

This is the build.xml script to build the WAR application from the Seam POJO project:

<project name="HelloWorld" default="main" basedir=".">

<property name="lib" location="${seam.home}/lib" />

<property name="applib" location="lib" />

<path id="lib.classpath">

<fileset dir="${lib}" includes="*.jar"/>

<fileset dir="${applib}" includes="*.jar"/>

</path>

<property name="testlib" location="${seam.home}/lib/test" />

<property name="eejb.conf.dir" value="${seam.home}/bootstrap" />

<property name="resources" location="resources" />

<property name="src" location="src" />

<property name="test" location="test" />

<property name="view" location="view" />

<property name="build.classes" location="build/classes" />

<property name="build.jars" location="build/jars" />

435

B.2 POJO-BASED WEB APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 9

<property name="build.test" location="build/test" />

<property name="build.testout" location="build/testout" />

<target name="test" depends="compile">

<taskdef resource="testngtasks" classpathref="lib.classpath"/>

<fileset dir="${build.classes}" includes="**/*.*"/>

<fileset dir="${resources}" includes="**/*.*"/>

</copy>

<! Overwrite the META-INF/persistence.xml >

<copy todir="${build.test}/META-INF" overwrite="true">

<fileset dir="${test}" includes="persistence.xml"/>

Trang 10

B.2 POJO-BASED WEB APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 11

For plain Tomcat deployment of Seam POJO applications, refer to the tomcatjpa

example for more details.

B.3 More Complex Applications

The two applications we have discussed in this appendix are simple web applications.

If your application uses advanced Seam features, you must package additional JAR

files and configuration files in the EAR or WAR archives.

• The Drools JARs and configuration files are needed to support the rule-based web

security framework in Seam Refer to Chapter 22 for more details.

APPENDIX B USING EXAMPLE APPLICATIONS AS TEMPLATES

438

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 12

• The jBPM JAR and configuration files are needed to support business processes

and stateful pageflows in Seam applications Refer to Chapter 24 for more details.

• PDF support requires the jboss-seam-pdf.jar file and the itext-*.jar file

in the WEB-INF/lib directory of the WAR archive Refer to Section 3.4.1 for

more details.

• Facelets-based email template support requires the jboss-seam-mail.jar file in

the WEB-INF/libdirectory of the WAR archive Refer to Section 3.4.2 for more

details.

• Wiki text support requires the antlr-*.jar file in the WEB-INF/libdirectory of

the WAR archive Refer to Section 3.4.3 for more details.

439

B.3 MORE COMPLEX APPLICATIONS

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 13

This page intentionally left blank

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 14

M ost example applications in this book use Ant as the build system Ant is simple

and straightforward However, since the first edition of this book, we have seen an

in-creasing adaption of Maven to manage builds Maven provides declarative dependency

management, which fits well with Seam applications since Seam integrates many

third-party libraries and frameworks In this appendix, we “mavenize” the Integration

example application to show you how to use Maven to build Seam applications The

result application is in the maven-ear example.

First, let’s look at the structure of the source code In order to build an EAR application

in Maven, you need to put each component of the EAR in a separate Maven module.

Theejb module builds the EJB JAR; the war builds the WAR; and the ear module

assembles them together into an EAR.

Trang 15

The directory structure here follows Maven’s standard convention For instance, Maven

knows to look for Java sources in src/main/java, classpath resources (e.g., configuration

files) in src/main/resources, and web content in src/main/webapp That saves us

a lot of time trying to specify and figure out the source structure.

As you can see, there are no JARs in this structure All the JARs needed for building

and packaging are downloaded from central Maven repositories off the Internet.

The four pom.xml files are the core of the build system Let’s look at them one by one.

The pom.xml file in the project root directory defines the defaults for this project It

specifies things like the group ID of the entire project, which Maven repositories to use,

and the default version number and scope for each dependency It gives us a central

place to view and update the version numbers for each dependency—much better than

the old way of examining each dependency JAR individually If a dependency has the

provided scope, it means that the dependency is provided by the runtime environment

(i.e., the application server) on the classpath Hence, Maven will only include the

provided dependencies in the compiling classpath, but will not include those JARs in

the final EAR, WAR, or JAR builds.

Trang 16

APPENDIX C USING MAVEN

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 18

The ejb/pom.xml declares the compile-time dependency of the classes in the EJB

module It should declare all its dependencies as provided since those dependencies

will be provided by the application server or included in the EAR Notice that the

de-pendencies here have no version number since they are inherited from the parent pom.xml

in the project root directory.

APPENDIX C USING MAVEN

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 19

Thewar/pom.xml declares the dependencies for the WAR module Every dependency

that does not have a provided scope will be included in the WAR’s WEB-INF/lib

directory All the compiled Java classes will be included in WEB-INF/classes.

Trang 20

<! The "provided" dependencies are

only need for compilation >

APPENDIX C USING MAVEN

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 21

Theear/pom.xml is executed last in the build process It assembles the JARs together

into an EAR file All modules and library JARs in the EAR need to be declared as

de-pendencies Then, in the maven-ear-plugin part of the script, we specify the type of

each module and where it should be placed in the EAR archive Maven will build the

application.xml accordingly on the fly Similarly, we can also specify the JBoss

loader in the build so that it will generate a jboss-app.xml for us.

Trang 22

<! The stuff that needs to go in the lib directory.

They will not be included in application.xml >

Maven’spom.xml files can be much longer than Ant scripts, but their declarative nature

makes them a lot easier to follow and understand.

Now you have the EAR structure of a Maven project It is easy to convert it into a WAR

project for Seam POJO applications or add testing support Check out Maven

documentation for more details.

449

APPENDIX C USING MAVEN

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 23

This page intentionally left blank

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

Trang 24

I n most examples throughout the book, we use JPA (Java Persistence API) for the

persistence logic We use Hibernate as our JPA implementation However, as it is an

open source framework at the forefront of ORM innovation, some of Hibernate’s features

are not yet standardized In particular, JPA does not yet support these features:

• The JPA query language is not as rich as that in Hibernate For instance, JPA does

not support Hibernate’s query-by-criteria or query-by-example.

• Hibernate offers more methods to manage objects with detached state; JPA supports

only one merge() operation in the EntityManager.

• The object type system in Hibernate is much richer than that in JPA.

• Hibernate gives you more control over the size of the extended persistence context.

If you need to use those features, you must use the Hibernate API directly You also

need to use the Hibernate API directly if you are working with legacy Hibernate code

(a large number of XML mapping files and queries in existing applications belong to

this category) That would mean to use Hibernate Session instead of the JPA

EntityManager in your Seam components The example used in this appendix can be

found in the hibernate example It is a port of the integration example.

D.1 Using the Hibernate API

To use the Hibernate API to manage database objects, we inject a Hibernate Session

instead of an EntityManager into the ManagerPojo class The API methods in the

Trang 25

Hibernate Session are roughly equivalent to those in the EntityManager; they have

only slightly different method names This is the Hibernate version of the ManagerPojo

class:

@Name("manager")

public class ManagerPojo {

@In (required=false) @Out (required=false)

private Person person;

private Person selectedFan;

public String sayHello () {

helloSession.save (person);

return "fans";

}

@Factory("fans")

public void findFans () {

fans = helloSession.createQuery("select p from Person p").list();

public String delete () {

Person toDelete = (Person) helloSession.merge (selectedFan);

Ngày đăng: 13/08/2014, 08:21

w