Building Seam 2.0 Application with NetBeans 6.1

转载请保留作者信息:

Author: 88250

Blog: http:/blog.csdn.net/DL88250

MSN & Gmail & QQ: DL88250@gmail.com

Table of Contents

Introduction 1

Prerequisites 2

Seam glimpse 2

Set up HelloSeam application 2

Create Project 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

Create a persistence unit of ejb project 4

Create the seam.properties 4

Create the compoonents.xml 5

Create the faces-config.xml 5

Create the pages.xml 5

Create the web.xml 6

Web pages coding 7

EJB Benas coding 9

The End.... 11

References: 11

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.csdn.net/DL88250 :-)

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

  • JavaEE Programming(JSF, EJB3, JPA)

  • Usage of NetBeans IDE 6.1

  • JBoss Seam Framework 2.0

  • Facelets

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>

</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!

Web pages 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>

Here is the whole scenario description:

EJB Benas coding

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 + ")";

}

}

The End....

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! : )

Here, you can download the whole sample application and the PDF document of this article.

References:

  • Seam Refresh

  • Carol McDonald's Blog

  • Seam Tutorial

  • Glassfish and Seam Tips

  • JBoss Seam: Simplicity and Power Beyond Java book

  • Java BluePrints Solutions Catalog for the Java Persistence APIs contains a collection of topics and example applications.

  • Java Persistence reference page on GlassFish Project

  • Java EE tutorial, for good tutorial on JSF and JPA

  • Pro EJB 3: Java Persistence API book

转载于:https://www.cnblogs.com/lanzhi/archive/2008/09/13/6470040.html

Building Seam 2.0 Application with NetBeans 6.1相关推荐

  1. JBoss Seam 3.0.0.Beta2 发布

    Seam 3.0.0 Beta2 已经发布,可通过 SourceForge 下载,下载地址:Download JBoss Seam 是"Java EE 5.0 的一个轻量级的框架" ...

  2. JBoss Seam 3.0.0.Beta1 发布

    JBoss Seam是"Java EE 5.0的一个轻量级的框架".它在Java EE 5.0框架的上层,为所有的在企业Web应用中的组件提供了一个统一的.易于理解的编程模型.它同 ...

  3. 70-540 TS: Microsoft Windows Mobile 5.0 - Application Development 考试感言

    Prometric网站自上周六就有问题了,无法注册微软考试(其它考试应该不受影响),害得我今天花了¥400 考的70-540.这门考试考试时间两个半小时,55道题,700分过关. 根据微软官方网站的指 ...

  4. NetBeans IDE 7.0 Beta 发布

    NetBeans 团队宣布 NetBeans IDE 7.0 Beta 可用! NetBeans IDE 7.0 Beta 引入了对 JDK 7 的支持.本次发布也包含了对 Oracle WebLog ...

  5. Jboss Seam

    概述: JBoss Seam是"Java EE 5.0的一个轻量级的框架".这是什么意思?难道Java EE(Enterprise Edition) 5.0本身不是一套" ...

  6. SEAM IN ACTION 第一章

    Part 1 Teeing off with Seam 开球 Many excellent frameworks exist to support the development of webbase ...

  7. 深入浅出JBoss Seam

    最近负责维护一个旧的系统,使用seam框架编写,一开始整个人都不好了,废话不多说,下面是从网上找到的我个人觉得还不错的一篇seam简介教程,更为详细的文档,大家可以去看我从另一个地方找到的参考,比这篇 ...

  8. JBoss Seam框架

    JBoss Seam框架简单介绍 一.Seam适应快速开发.简化框架的趋势 在RoR流行之前,Java社区的主流还是非常讲究分层.架构.复用和模式,而比较忽视快速开发和简化架构的,其结果就是代码量大. ...

  9. ASP.NET Core 6.0对热重载的支持

    .NET 热重载技术支持将代码更改(包括对样式表的更改)实时应用到正在运行的程序中,不需要重启应用,也不会丢失应用状态. 一.整体介绍 目前 ASP.NET Core 6.0 项目都支持热重载.在以下 ...

最新文章

  1. 使用List中的remove方法遇到的坑,不信你没有踩过!
  2. Python-EEG工具库MNE中文教程(8)-参考电极简介
  3. 【深度学习入门到精通系列】图像预处理—图像分块代码(matlab)
  4. 分布式系统和集群的区别
  5. 六十三、SpringBoot中的日志框架SLF4j的使用
  6. 走近华为“天才少年”钟钊:入职两年两度突破业界学界极限
  7. 我的世界基岩版json_(1.8.0.13+)我的世界Minecraft【BE】基岩版 /function 使用教程
  8. 无心剑中译狄金森诗36首
  9. 二进制数表示形式:原码、反码与补码
  10. 尽快更新!Chrome 修复两个已遭在野利用的 0day
  11. flash制作文字笔顺_教你如何给GIF动态图片加上文字
  12. python xpath提取td标签_Python Xpath 提取html整个元素(标签与内容)
  13. 使用telnet登录varnish3.x管理缓存时认证问题
  14. C/C++常见面试题整理
  15. (转)When Milliseconds Make Millions
  16. 1002:方便记忆的电话号码
  17. Ue4首次项目的感想和心得
  18. 带你深入浅出学STM32
  19. 第二章 源力、质量、能量
  20. VC,Http库(get,post,upload(File))

热门文章

  1. angular 的配置文件的应用
  2. HDU1285 确定名次 拓扑排序
  3. [Swift]数组排序:sort和sorted
  4. 【Yaml】Yaml学习笔记
  5. 记linux_centOS安装as86过程
  6. thread.Join(); 让主线程等待自己完成
  7. 黑马程序员-----集合框架类(四) 高级for循环、方法的可变参数及静态导入
  8. HDU5196--DZY Loves Inversions 树状数组 逆序数
  9. ++ 优化 频繁new_Java性能优化的50个细节,我必须分享给你!
  10. nodejs模块导入导出