Building Seam 2.0 Application with NetBeans 6.1
转载请保留作者信息:
Author: 88250
Blog: http:/blog.csdn.net/DL88250
MSN & Gmail & QQ: [email protected]
Table of Contents
Set up HelloSeam application 2
Create a enterprise application deployment descriptor 2
Add dependencies for HelloSeam-ejb project 3
Add dependencies for HelloSeam-war project 3
Create a ejb deployment descriptor 3
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.csdn.net/DL88250 :-)
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } --></style> <meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8"> <meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } --> </style><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } --> </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! :-)
In this sample application, I use Facelets as JSF view definition framework, it is a very elegant presentation for JSF.
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.
In this section, I will mention some important notices of creating seam application using NetBeans IDE.
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).
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.
Open you HelloSeam-ejb project, add the following jar libraries:
All of them you can find under SeamHome/lib.
All of them you can find under SeamHome/lib or under FaceletsHome.
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>
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 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
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....
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.
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>
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>
</context-param>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.seam</url-pattern>
</servlet-mapping>
<!-- JEE5 EJB3 names -->
<ejb-local-ref>
<ejb-ref-name>HelloSeam/RegisterAction/local</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>org.jboss.seam.example.registration.Register</local>
<ejb-link>RegisterAction</ejb-link>
</ejb-local-ref>
<!-- some issue with ejb transcation using glassfish v2, also comments
components.xml
-->
<!--
<ejb-local-ref>
<ejb-ref-name>HelloSeam/EjbSynchronizations/local</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home/>
<local>org.jboss.seam.transaction.LocalEjbSynchronizations</local>
<ejb-link>EjbSynchronizations</ejb-link>
</ejb-local-ref>
-->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
Notice: formerly, I want to use ejb transaction for JPA, but there is some issues
working with Glassfish v2.... Anyone can help me out?
From instruction 2-11 are the basic configuration files for JBoss Seam application. And then, let's coding!
After we finished HelloSeam application setting up, let me have a short introduce about this application.
register.xhtml design:
When we register successfully, the page will redirect to registerd.xhtml, and display:
Now, we can code these pages as followings:
register.xhtml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Register New User</title>
</head>
<body>
<f:view>
<h:form>
<s:validateAll>
<h:panelGrid columns="2">
Username: <h:inputText value="#{user.username}" required="true"/>
Real Name: <h:inputText value="#{user.name}" required="true"/>
Password: <h:inputSecret value="#{user.password}" required="true"/>
</h:panelGrid>
</s:validateAll>
<h:messages/>
<h:commandButton value="Register" action="#{register.register}"/>
</h:form>
</f:view>
</body>
</html>
registered.xhtml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Successfully Registered New User</title>
</head>
<body>
<f:view>
Welcome, #{user.name}, you are successfully registered as #{user.username}.
</f:view>
</body>
</html>
Index.html(just for redirecting the default page in web.xml):
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=register.seam">
</head>
</html>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } --> </style>Here is the whole scenario description:
In this section, I use the JBoss Seam example code(Modify a little):
Register:
package org.jboss.seam.example.registration;
import javax.ejb.Local;
@Local
public interface Register {
public String register();
}
RegisterAction:
package org.jboss.seam.example.registration;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
import static org.jboss.seam.ScopeType.EVENT;
@Stateless
@Scope(EVENT)
@Name("register")
public class RegisterAction implements Register {
@In
private User user;
@PersistenceContext
private EntityManager em;
@Logger
private static Log log;
private boolean registered;
public boolean isRegistered() {
return registered;
}
public void setRegistered(boolean registered) {
this.registered = registered;
}
public String register() {
List existing =
em.createQuery("select u.username from User u where u.username=#{user.username}").getResultList();
if (existing.size() == 0) {
em.persist(user);
em.flush();
log.info("Registered new user #{user.username}");
registered = true;
return "/registered.seam";
} else {
FacesMessages.instance().add("User #{user.username} already exists");
registered = false;
return null;
}
}
}
Notice: class RegisterAction, I use stateless session bean.
User Bean:
package org.jboss.seam.example.registration;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Entity
@Name("user")
@Scope(SESSION)
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = 1881413500711441951L;
private String username;
private String password;
private String name;
public User(String name, String password, String username) {
this.name = name;
this.password = password;
this.username = username;
}
public User() {
}
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotNull
@Length(min = 5, max = 15)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
@NotNull
@Length(min = 5, max = 15)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User(" + username + ")";
}
}
In this article, I introduce the basic configurations of an JBoss Seam 2 application, how to build it with NetBeans IDE, and deploy it on Glassfish V2. If you will create your own seam application, the important is the various of configuration files and library dependencies. I think, we should pay more attention on them, and configure them carefully.
And then, I will compose another article about how to build a JBoss Seam 2 application by Maven2 with NetBeans IDE as I mentioned. So, do not miss! : )
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<meta name="GENERATOR" content="OpenOffice.org 2.4 (Win32)"> <style type="text/css"> <!-- @page { size: 21cm 29.7cm; margin: 2cm } P { margin-bottom: 0.21cm } --> </style>Here, you can download the whole sample application and the PDF document of this article.
Java BluePrints Solutions Catalog for the Java Persistence APIs contains a collection of topics and example applications.