Building Seam 2.0 Application with NetBeans 6.1

<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE"><meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"><style type="text/css"> </style>

Building Seam 2.0 Application with NetBeans 6.1


转载请保留作者信息:

Author: 88250

Blog: http:/blog.youkuaiyun.com/DL88250

MSN & Gmail & QQ: DL88250@gmail.com




Introduction

This article depicts how to build a simple registration application base on JBoss Seam 2.0(JSF with Facelets, EJB3, JPA) using NetBeans 6.1, and deploys it on Glassfish v2, MySQL 5.1.

To the demonstration building, I divide the its content into two ways:

  • Using NetBeans built-in project wizard to create a enterprise application, which includes a ejb project and a web project. This entry will use this way to build the sample application.

  • Using Maven for NetBeans plugin to create a enterprise application, also it includes a ejb project and a web project. This way I will use to the subsequent entry, please pay more attention to my blog: http://blog.youkuaiyun.com/DL88250 :-)


<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE">

<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"><style type="text/css"> </style><meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE"><meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"><style type="text/css"> </style>

<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE">

<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"><style type="text/css"> </style>

All of these, will deploy on Glassfish V2 and use MySQL 5.1 community edition. As I mentioned formerly, this demo using Facelets framework for JSF view definition, the most important thing is it setup with NetBeans IDE project wizard and deploys on Glassfish v2. Although you maybe refer to jee-booking example in JBoss Seam tutorial, there are some practical issues you will occur. So, Just follow me! :-)

Prerequisites

In this sample application, I use Facelets as JSF view definition framework, it is a very elegant presentation for JSF.

Seam glimpse

As we known, Seam is a powerful open source development platform for building rich Internet applications in Java. Seam integrates technologies such as Asynchronous JavaScript and XML (AJAX), JavaServer Faces (JSF), Java Persistence (JPA), Enterprise Java Beans (EJB 3.0) and Business Process Management (BPM) into a unified full-stack solution, complete with sophisticated tooling. The simple chart of architectural design will show you about this:




The following demonstration will show you a part of features Seam brought.

Set up HelloSeam application

In this section, I will mention some important notices of creating seam application using NetBeans IDE.


Create Project

Open NetBeans IDE, and create a enterprise application project, named HelloSeam. It should include a ejb application project(HelloSeam-ejb) and a web application project(HelloSeam-war).

Create a enterprise application deployment descriptor

The descriptor named application.xml, is placed in HelloSeam/src/conf/, when we build project, it will copy to HelloSeam/dist/HelloSeam.ear/META-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<application version="5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">

<display-name>HelloSeam</display-name>

<module>

<web>

<web-uri>HelloSeam-war.war</web-uri>

<context-root>/HelloSeam-war</context-root>

</web>

</module>

<module>

<ejb>HelloSeam-ejb.jar</ejb>

</module>

</application>

Notice: add the jboss-seam.jar as a ejb module is NOT necessary.


Add dependencies for HelloSeam-ejb project

Open you HelloSeam-ejb project, add the following jar libraries:


All of them you can find under SeamHome/lib.

Add dependencies for HelloSeam-war project



All of them you can find under SeamHome/lib or under FaceletsHome.

Create a ejb deployment descriptor

The descriptor named ejb-jar.xml, is placed in HelloSeam-ejb/src/conf/, when we build project, it will copy to HelloSeam-ejb/dist/HelloSeam-ejb.jar/META-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"

version="3.0">

<interceptors>

<interceptor>

<interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>

</interceptor>

</interceptors>

<assembly-descriptor>

<interceptor-binding>

<ejb-name>*</ejb-name>

<interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>

</interceptor-binding>

</assembly-descriptor>

</ejb-jar>

Create a persistence unit of ejb project

The descriptor named persistence.xml, is placed in HelloSeam-ejb/src/conf/, when we build project, it will copy to HelloSeam-ejb/dist/HelloSeam-ejb.jar/META-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="userDatabase">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<jta-data-source>jdbc/seamHelloDS</jta-data-source>

<class>org.jboss.seam.example.registration.User</class>

<exclude-unlisted-classes>false</exclude-unlisted-classes>

<properties>

<!-- The following two properties are for Glassfish -->

<property name="hibernate.dialect"

value="org.hibernate.dialect.DerbyDialect"/>

<property name="hibernate.transaction.manager_lookup_class"

value="org.hibernate.transaction.SunONETransactionManagerLookup"/>

<!-- common configurations -->

<property name="hibernate.hbm2ddl.auto" value="create-drop"/>

<property name="hibernate.show_sql" value="true"/>

<property name="hibernate.transaction.flush_before_completion" value="true"/>

<property name="hibernate.cache.provider_class"

value="org.hibernate.cache.HashtableCacheProvider"/>

</properties>

</persistence-unit>

</persistence>

Notice: This demonstration use Hibernate as the JPA provider.

Create the seam.properties

Create a file named seam.properties, and places it in HelloSeam-ejb/src/conf/. This file is very important for loading seam components. If you ignores it, maybe you will occurs some particular exceptions, such as follow:

javax.el.PropertyNotFoundException: /register.xhtml @17,90 value="#{user.username}": Target Unreachable, identifier 'user' resolved to null


Create the compoonents.xml

The descriptor named components.xml, is placed in HelloSeam-war/web/WEB-INF/, when we build project, it will copy to HelloSeam-war/dist/HelloSeam-war.war/WEB-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<components xmlns="http://jboss.com/products/seam/components"

xmlns:core="http://jboss.com/products/seam/core"

xmlns:security="http://jboss.com/products/seam/security"

xmlns:transaction="http://jboss.com/products/seam/transaction"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=

"http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd

http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd

http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.0.xsd

http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd">

<core:init jndi-pattern="java:comp/env/HelloSeam/#{ejbName}/local" />

<!-- some issue with ejb transcation using glassfish v2, also comments

web.xml

-->

<!--

<transaction:ejb-transaction/>

-->

<core:manager conversation-timeout="120000"

concurrent-request-timeout="500"

conversation-id-parameter="cid"/>

</components>

Notice: formerly, I want to use ejb transaction for JPA, but there is some issues

working with Glassfish v2....


Create the faces-config.xml

The descriptor named faces-config.xml, is placed in HelloSeam-war/web/WEB-INF/, when we build project, it will copy to HelloSeam-war/dist/HelloSeam-war.war/WEB-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config version="1.2"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">


<!-- Facelets support -->

<application>

<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>

</application>

</faces-config>

Notice: I defines the navigation rules in file pages.xml, as JBoss Seam recommend, refers to the next instruction.


Create the pages.xml

The descriptor named pages.xml, is placed in HelloSeam-war/web/WEB-INF/, when we build project, it will copy to HelloSeam-war/dist/HelloSeam-war.war/WEB-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<pages xmlns="http://jboss.com/products/seam/pages"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd"

>

<page view-id="/register.xhtml">

<navigation>

<rule if="#{register.registered}">

<redirect view-id="/registered.xhtml"/>

</rule>

</navigation>

</page>

<exception class="org.jboss.seam.security.NotLoggedInException">

<redirect view-id="/.xhtml">

<message severity="warn">You must be logged in to use this feature

</message>

</redirect>

</exception>

</pages>


Create the web.xml

The descriptor named web.xml, is placed in HelloSeam-war/web/WEB-INF/, when we build project, it will copy to HelloSeam-war/dist/HelloSeam-war.war/WEB-INF. Its content like this:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- Seam -->

<listener>

<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>

</listener>

<filter>

<filter-name>Seam Filter</filter-name>

<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>Seam Filter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<servlet>

<servlet-name>Seam Resource Servlet</servlet-name>

<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Seam Resource Servlet</servlet-name>

<url-pattern>/seam/resource/*</url-pattern>

</servlet-mapping>

<!-- JSF and Facelets -->

<context-param>

<param-name>javax.faces.DEFAULT_SUFFIX</param-name>

<param-value>.xhtml</param-value>

</context-param>

<context-param>

<param-name>facelets.DEVELOPMENT</param-name>

<param-value>true</param-value>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值