DraftVersion–December14,2006 DraftVersion–December14,2006 ...

thrillukrainianSoftware and s/w Development

Jun 7, 2012 (5 years and 3 months ago)

422 views

Draft Version – December 14,2006
Draft Version – December 14,2006
FACELETS,(JBOSS) SEAM,NETBEANS
AND GLASSFISH (SJSAS)
IDE CONFIGURATION AND BASIC DEVELOPMENT HOWTO
Jens Frey <jens.frey@coffeecrew.org>
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006

1 Introduction
1
1.1 Preconditions
....................................
1
2 Basic IDE setup
3
2.1 Setup seam
.....................................
3
2.2 Facelets
.......................................
4
2.3 Browser
.......................................
5
2.4 Glassfish/SJSAS setup
...............................
6
3 Basic Seam
7
3.1 EAR project configuration
.............................
8
3.2 EJB project configuration
.............................
9
3.3 WAR project configuration
.............................
12
3.3.1 Got Errors?
.................................
18
4 Seamand Derby
21
List of Figures
31
Listings
35
Bibliography
35
Index
37
i
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
 
 
This document describes how to configure the Netbeans Integrated Development Environ-
ment (IDE) version 5.5
1
with facelets
2
,seam
3
and Enterprise Java Bean version 3 (EJB3)
technology.It basically covers the configuration of the IDE,not how to seriously develop
applications using the named technologies.The technologies itself are explained (more or
less) in depth on the according project pages.Nevertheless a little project will be developed,
so we can proove we have a working installation.The mini application simply prints 
 to the application servers system console.This is done to keep the application as
simple as possible.We could have redirected to another page,but that would mean we’d
have to build another site;)
There will be a chapter where we will write some date into a database,and – who knows –
probably we’ll read it again fromit,redirecting to another page.
1.1 Preconditions
Before you begin,you have to download the Netbeans IDE,version 5.5 and the corresponding
enterprise pack.Further may want to download the facelets plugin for netbeans.The facelets
plugin for netbeans is recommended to install.As we do deploy on Glassfish,which is aka Sun
Java System Application Server 9,it would be nice having Netbeans and glassfish packaged
together.Luckily this is already done,and you can download “NetBeans IDE 5.5 + Sun Java
1
Download from
http://www.netbeans.org
2
Download from
https://facelets.dev.java.net/
3
Download from
http://labs.jboss.com/portal/jbossseam
1
Draft Version – December 14,2006
Draft Version – December 14,2006
1.INTRODUCTION
System Application Server 9.0 U1 Bundle Download” from
http://www.netbeans.org
.if you
are using Linux,the appropriate filename is./sjsas_pe-9_0_01-nb-5_5-linux.bin
When installing the IDE i remind you to read the last page,sometimes there are actually
errors,that the intaller software tries to tell us.
To validate your Netbeans bundle installation,open Netbeans,select Tools->Server Manager
from the menu bar.If you see “Sun Java System Application Server 9” within J2EE Servers,
everything is ok.Then go on,close Netbeans and install the “Visual Web Pack 5.5”.
You can get the software from:

Netbeans Visual Web Pack 5.5 (Technology Preview) [optional,but useful for editing
CSS and such things]:
http://www.netbeans.org/products/visualweb/

Netbeans Facelets support:
http://wiki.netbeans.info/wiki/view/FaceletsSupport
4

Facelets:
https://facelets.dev.java.net/

JBoss-Seam:
http://labs.jboss.com/portal/jbossseam
After you have installed Netbeans and the various add-ons,you may extract your libraries
into a common lib folder,for this tutorial the library folder is/data/coding2/webapps/libs.
4
If you don’t mind reading the facelets support page,you can get the lates snapshot of the netbeans support
module here
https://nbfaceletssupport.dev.java.net/files/documents/5684/43225/nbfaceletssupport-0-3.zip
2
Draft Version – December 14,2006
Draft Version – December 14,2006
 
  
The following lines explain how to firstly configure your IDE,so you’re able to work with
seam and facelets.First thin to do,after you unpacked the jar files and moved them to
your lib folder,you have to setup the libraries within the library manager (Tools->Library
Manager).
2.1 Setup seam
At first create a (class) library named »SEAM« and add the following jar files to it (that are
now in your lib folder i assume;)).

JBOSS-SEAM.JAR

JBPM-3.1.2.JAR

THIRDPARTY-ALL.JAR

HIBERNATE-ALL.JAR
Now create a library named »SEAM-UI« and include the following jars.

JBOSS-SEAM-UI.JAR
3
Draft Version – December 14,2006
Draft Version – December 14,2006
2.BASIC IDE SETUP
And to end the seam library creation desaster,add a new library »SEAM-DEBUG« and include
the following jars.

JBOSS-SEAM-DEBUG.JAR
That should do it to have seamlibraries setup correctly.
2.2 Facelets
If you are using the facelets plugin
1
for the first time,you may want to create a dummy
project,to have the “setup-procedure” of the plugin already completed if you’re going to
setup the seam application.For this to work correctly,be sure that you do not already have
a FACELETS library in your library manager.Create a new Web-Application »CTRL+SHIFT-N«
and name it “flSetup” (you may want to set the project location to a different place,like a
temporary folder),click »NEXT«.You are now asked to select a framework,you would like
to use.Select “Facelets” there.
Where it asks for a directory,give it the path to the directly unzipped directory,do not give
that dialog the “only needed” jars,otherwise the dialog will not work correctly.
Figure 2.1:New facelets web application,uninitialized
1
See the download link in [chapter
1.1
,p.
1
],then unzip and install manually (via Tools->Update Center-
>Install manually...)
4
Draft Version – December 14,2006
Draft Version – December 14,2006
2.3.BROWSER
After the plugin successfully imported the necessary libraries into the Netbeans IDE,the
frameworks screen looks as shown in [Fig.:
2.2
,p.
5
]:
Figure 2.2:New facelets web application,initialized
You can proove the correct installation of the Facelets plugin if the “Libraries” node from the
web application project contains a library FACELETS (or check with the “Library Manager”).
You can now of course delete the created web-project,so your workspace is clean and virgin
again.
If you are not using the Netbeans facelet plug-in you probably want to configure a facelets
library.So again,create a new library,this time named »FACELETS« and include the following
files:

JSF-FACELETS.JAR

EL-API.JAR (the el-* jars are maybe not necessary with JSF 1.2)

EL-RI.JAR
2.3 Browser
That Netbeans is able to start your browser with your selected web project,you have to
configure it,to select the correct browser for you.This can be done if you openTools->Options
fromthe menubar.In “General” you do of course select firefox as browser,do you?
5
Draft Version – December 14,2006
Draft Version – December 14,2006
2.BASIC IDE SETUP
2.4 Glassfish/SJSAS setup
May be you want to “tune” the application server somehow.This is possible from within the
Netbeans IDE.To do this,select the “Runtime” tab,open “Servers” and select the Sun Java
System Application Server (SJSAS).Next click right and select “properties”.Select the JVM
Options line and hit “...”.Select the first line (“-client”) and change it into “-server”,then
press “Edit”,NOT enter (for those of you who actually hit enter,go to the bottom of the list
and remove the “-server” flag;)
Next add “-XX:MaxPermSize=128m” this prevents running out of memory when redeploying
(at least on JBoss,but may help glassfish too;))
6
Draft Version – December 14,2006
Draft Version – December 14,2006
 
 
This chapter explains the necessary steps to develop a simple seamproject fromscratch using
the Netbeans IDE 5.5 and the Glassfish or SJSAS.
First of all we do want to create an Enterprise Application,so i’d say we’ll do this.To do
so,press »CTRL+SHIFT-N« and select Enterprise -> Enterprise Application.In the following
dialog enter sampleProject as project name and let all settings checked as they are per default.
Figure 3.1:Create enterprise application
7
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
3.1 EAR project configuration
Within the Netbeans IDE select your EAR project (sampleProject)
and right click it,to select properties.Now add the created SEAM
library to the project (you can do this with build->packaging).If you
do have any changes in application.xml,you maybe have to reassure,
that your changes are not overwritten by Netbeans trying to generate
a valid application.xml file.You should see the following dialog set,
if you are adding the SEAM library to the EAR project.
Figure 3.2:Add seamlibrary to EAR file
Adopt application.xml to include the jboss-seam.jar file as java module.Be careful here,if
you do sth.that may regenerate application.xml.
1 <?xml version="1.0"encodi ng="UTF−8"?>
<appl i cat i on version="5"xmlns="ht t p://j ava.sun.com/xml/ns/j avaee"
xml ns:xsi ="ht t p://www.w3.org/2001/XMLSchema−i nst ance"
xsi:schemaLocat i on="ht t p://j ava.sun.com/xml/ns/j avaee
ht t p://j ava.sun.com/xml/ns/j avaee/appl i cat i on_5.xsd">
6 <di spl ay−name>sampl eProj ect </di spl ay−name>
<module>
<web>
<web−ur i >sampl eProj ect −war.war</web−ur i >
<cont ext −r oot >/sampl eProj ect −war</cont ext −r oot >
11 </web>
</module>
8
Draft Version – December 14,2006
Draft Version – December 14,2006
3.2.EJB PROJECT CONFIGURATION
<module>
<ej b>sampl eProj ect −ej b.j ar </ej b>
</module>
16 <module>
<j ava>j boss−seam.j ar </j ava>
</module>
</appl i cat i on>
Listing 3.1:application.xml
3.2 EJB project configuration
The next step is configuring the Enterprise Java Bean (EJB) project
properly.Right click your EJB project (sampleProject-ejb) select
New->File/Folder,choose File type Enterprise->Standard Deploy-
ment Descriptor.The creates an ejb-jar.xml file.The full config-
uration file is printed in [Listing
3.2
,S.
9
] (The ejb-jar tag will be
auto-generated from the IDE,which is maybe not that bad,if e.g.
the EJB version changes,you’ll have that automatically set).Because
we want to use SEAM in our EJB project,we have to add the seam
library to the EJB project.You can do this by right-clicking the “Library” node inside the EJB
project and then select “Add Library”.
1 <?xml version="1.0"encodi ng="UTF−8"?>
<ej b−j ar xmlns ="ht t p://j ava.sun.com/xml/ns/j avaee"
version ="3.0"
xml ns:xsi ="ht t p://www.w3.org/2001/XMLSchema−i nst ance"
6 xsi:schemaLocat i on ="ht t p://j ava.sun.com/xml/ns/j avaee
ht t p://j ava.sun.com/xml/ns/j avaee/ej b−j ar_3_0.xsd">
<i nt er cept or s>
<i nt er cept or >
<i nt er cept or −cl ass>
11 org.j boss.seam.ej b.SeamI nt ercept or
</i nt er cept or −cl ass>
</i nt er cept or >
</i nt er cept or s>
<assembly−descr i pt or >
16 <i nt er cept or −bi ndi ng>
<ej b−name>∗</ej b−name>
<i nt er cept or −cl ass>
org.j boss.seam.ej b.SeamI nt ercept or
</i nt er cept or −cl ass>
21 </i nt er cept or −bi ndi ng>
</assembly−descr i pt or >
</ej b−j ar >
Listing 3.2:ejb-jar.xml
9
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
You can find further information according to this topic on the documents page,for ver-
sion 1.1.0 BETA,look at [
Kin06
,p.55] (There is an online and offline version available to
download,the page number is,of course fromthe PDF).
Right click your EJB project (sampleProject-ejb) again and select New->File/Folder,choose
File type Other->Properties File name it seam (.properties will be automatically appended)
as folder select src/conf this file may be empty,but is required for seamto work correctly.
Now select the files tab »(CTRL+2)« and edit the build.xml file add the following target
inside the <project></project> delimiters
Figure 3.3:Edit build properties
<?xml version="1.0"encodi ng="UTF−8"?>
2 <!−− You may f r eel y edi t t hi s f i l e.See commented bl ocks below f or −−>
<!−− some examples of how t o customi ze t he bui l d.−−>
<!−− ( I f you del et e i t and reopen t he pr oj ect i t wi l l be r ecr eat ed.)
−−>
<pr oj ect name="sampl eProj ect −ej b"def aul t ="def aul t"basedi r ="."
7 xml ns:ej bj ar pr oj ect ="ht t p://www.netbeans.org/ns/j 2ee−ej bj ar pr oj ec t/3">
<descr i pt i on>Bui l ds,t est s,and runs t he pr oj ect
sampl eProj ect −ej b.</descr i pt i on>
<i mpor t f i l e ="nbpr oj ect/bui l d−i mpl.xml"/>
12 <t ar get name="−pre−compi l e">
<copy f i l e ="${ meta.i n f }/seam.pr oper t i es"
t odi r ="${ bui l d.di r }/ear−module"/>
</t ar get >
</pr oj ect >
Listing 3.3:build.xml
Switch Back to the projects view »(CTRL+1)« Because seamencourages you to use Stateless
Session Beans (SLSB) as JSF Action Listeners,we create an ActionBean which we can use in
our “HelloWorld” Application.It simply sends “Hello World” to the systemconsole.
To do so,right click “Source Packages” in your EJB project.Select New->File/Folder choose
Enterprise->Session Bean.As EJB Name type “Action”,for Package enter “dummy”.Make
10
Draft Version – December 14,2006
Draft Version – December 14,2006
3.2.EJB PROJECT CONFIGURATION
sure you “Session Type –> Stateless” selected and that “create interface –> local” is selected
too.Click Finish.
The ActionBean.java class opens in the editor.Right click above the source code and select
EJB Methods->Add Business Method...
As Method name enter “sayHello” and have “Use in interface->Local” selected,click ok.The
interface definition will be updated automatically.
Now enter the EJB code,which,in this case,is especially simple
System.out.pr i nt l n ("Hel l o World");
To make it possible for seam to locate your ActionBean,you have to give it a name,which
in this case is simply “action”.This is done by annotating the class ActionBean with the
@Name(“action”) annotation.If you typed the @Name(“action”) line,the IDE will mark it
as an error.Now it is necessary to fix the imports.Therefor type »(ALT+SHIFT+F)« and be
shure,you have the class org.jboss.seam.annotations.Name selected in the dropdown menu.
The full blown;) class listing is shown below (stripped some generated comments).
package dummy;
import j avax.ej b.St at el ess;
4 import org.j boss.seam.annot at i ons.Name;
@Statel ess
@Name("act i on")
publ i c class Acti onBean implements Act i onLocal {
9
/∗ ∗ Creates a new i nst ance of Acti onBean ∗/
publ i c Acti onBean ( ) {
}
14 publ i c void sayHel l o ( ) {
System.out.pr i nt l n ("Hel l o World");
}
}
Listing 3.4:ActionBean.java
And,of course we need a interface:
package dummy;
3 import j avax.ej b.Local;
/∗ ∗
∗ Thi s i s t he busi ness i nt er f ace f or Act i on ent er pr i se bean.
∗/
8 @Local
publ i c i nt erf ace Act i onLocal {
void sayHel l o ( );
11
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
}
Listing 3.5:ActionLocal.java
Now we are finished with the EJB part,let’s swing over to the web tier.
3.3 WAR project configuration
Now that we have implemented our very complicated business tier,
we do want to have some web control,where we can control our
business from.You may at this point really want to use the netbeans
facelets plugin.If you do this,you can right click on your web project
(sampleProject-war) and select “Properties”.You then have the abil-
ity to select a “Framework” to add to your web project.If the dialog
opens,select the Facelets “Framework”.You do not have to select
the Java Server Faces (JSF) framework,the facelets plugin does that
correctly for you.If you do not use the facelets plugin,you have to manually trimweb.xml
and faces-config.xml.Additionally the plugin add a css folder and a default Cascading Style
Sheets (CSS) style.If you are not using the plugin,you have to create the folder and CSS
yourself.
Figure 3.4:Add facelets framework
12
Draft Version – December 14,2006
Draft Version – December 14,2006
3.3.WAR PROJECT CONFIGURATION
If you are using the netbeans-facelets extension,in your web project right click “Web Pages”
and select New->File/Folder->”Facelets Template”,name it sampleTemplate,and select “Lay-
out Style”->CSS,press finish (be shure you do this in project view »(CTRL+1)« ).The
sampleTemplate.xhtml file is like follows (for those who don’t use facelets plugin).
<!DOCTYPE html PUBLIC"−//W3C//DTD XHTML 1.0 Tr ansi t i onal//EN"
"ht t p://www.w3.org/TR/xht ml 1/DTD/xhtml 1−t r ans i t i onal.dt d">
3 <html xmlns="ht t p://www.w3.org/1999/xht ml"
xmlns:ui ="ht t p://j ava.sun.com/j s f/f acel et s">
<head>
<meta ht t p−equi v="Content−Type"content="t ext/ht ml;
8 char set =UTF−8"/>
<l i nk href="css/def aul t.css"r el ="st yl esheet"
type="t ext/css"/>
<l i nk href="css/cssLayout.css"r el ="st yl esheet"
type="t ext/css"/>
13 < t i t l e >Facel et s Template</t i t l e >
</head>
<body>
18 <div i d="t op"class="t op">
<ui:i nser t name="t op">Top</ui:i nser t >
</div>
<div i d="cont ent"class="cent er _cont ent">
23 <ui:i nser t name="cont ent">Content </ui:i nser t >
</div>
</body>
28 </html>
Listing 3.6:sampleTemplate.xhtml
The two CSS files (cssLayout.css and default.css) are as follows.
#t op {
2 posi t i on:r el at i v e;
background−col or:#036f ab;
col or:whi t e;
paddi ng:5px;
margi n:0px 0px 10px 0px;
7 }
#bottom {
posi t i on:r el at i v e;
background−col or:#c2df ef;
12 paddi ng:5px;
margi n:10px 0px 0px 0px;
}
#l e f t {
17 f l oat:l e f t;
13
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
background−col or:#ece3a5;
paddi ng:5px;
wi dt h:150px;
}
22
#r i ght {
f l oat:r i ght;
background−col or:#ece3a5;
paddi ng:5px;
27 wi dt h:150px;
}
.cent er _cont ent {
posi t i on:r el at i v e;
32 background−col or:#dddddd;
paddi ng:5px;
}
.l ef t _cont ent {
37 background−col or:#dddddd;
paddi ng:5px;
margin−l e f t:170px;
}
42.r i ght _cont ent {
background−col or:#dddddd;
paddi ng:5px;
margi n:0px 170px 0px 170px;
}
47
#t op a:l i nk,#t op a:v i s i t ed {
col or:whi t e;
f ont −wei ght:bol d;
t ext −decor at i on:none;
52 }
#t op a:l i nk:hover,#t op a:v i s i t ed:hover {
col or:bl ack;
f ont −wei ght:bol d;
57 t ext −decor at i on:under l i ne;
}
Listing 3.7:cssLayout.css
body {
2 background−col or:#f f f f f f;
f ont −si ze:12px;
f ont −f ami l y:Verdana,"Verdana CE",Ar i al,"Ar i al CE",
"Luci da Grande CE",l uci da,"Hel vet i ca CE",sans−s e r i f;
col or:#000000;
7 margi n:10px;
}
h1 {
f ont −f ami l y:Ar i al,"Ar i al CE","Luci da Grande CE",l uci da,
12"Hel vet i ca CE",sans−s e r i f;
14
Draft Version – December 14,2006
Draft Version – December 14,2006
3.3.WAR PROJECT CONFIGURATION
border−bottom:1px sol i d#AFAFAF;
f ont −si ze:16px;
f ont −wei ght:bol d;
margi n:0px;
17 paddi ng:0px;
col or:#D20005;
}
a:l i nk,a:v i s i t ed {
22 col or:#045491;
f ont −wei ght:bol d;
t ext −decor at i on:none;
}
27 a:l i nk:hover,a:v i s i t ed:hover {
col or:#045491;
f ont −wei ght:bol d;
t ext −decor at i on:under l i ne;
}
Listing 3.8:default.css
As next step we have to add the so called template client (that’s the page that is using the
template).To do so,right click your web-project and select New->File/Folder Web->Facelets
Template Client.Enter sample as Filename and select the template to use.This is sam-
pleTemplate.xhtml in this case.Click finish.Insert the following code into the generated
file:
<h:form>
<h:commandButton value="Cl i ck me"acti on="#{ act i on.sayHel l o }"/>
</h:form>
Listing 3.9:sample code to insert
The full sample.xhtml file now looks like this.
<!DOCTYPE html PUBLIC"−//W3C//DTD XHTML 1.0 Tr ansi t i onal//EN"
2"ht t p://www.w3.org/TR/xht ml 1/DTD/xhtml 1−t r ans i t i onal.dt d">
<html xmlns="ht t p://www.w3.org/1999/xht ml"
xmlns:ui ="ht t p://j ava.sun.com/j s f/f acel et s"
xmlns:h="ht t p://j ava.sun.com/j s f/ht ml">
7 <body>
<ui:composi t i on t empl at e="sampleTemplate.xht ml">
<ui:def i ne name="t op">
12 t op
</ui:def i ne>
<ui:def i ne name="cont ent">
<h:form>
17 <h:commandButton value="Cl i ck me"
acti on="#{ act i on.sayHel l o }"/>
15
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
</h:form>
</ui:def i ne>
22 </ui:composi t i on>
</body>
</html>
Listing 3.10:sample.xhtml
The action that was configured within the EJB project as a SLSB,is nowused fromwithin this
page.This is the “sayHello()” method from the EJB that will be executed once the button is
clicked.As you can see “action” equals exactly that name,we annotated our ActionBean.java
class with.
The next step involves changing web.xml to our convenience.You can select web.xml from
Configuration Files inside your web project.Change the “servlet-mapping” and “index file”
directives to “xhtml” and add the seamspecific stuff.The full file contains:
<?xml version="1.0"encodi ng="UTF−8"?>
<web−app version="2.5"xmlns="ht t p://j ava.sun.com/xml/ns/j avaee"
xml ns:xsi ="ht t p://www.w3.org/2001/XMLSchema−i nst ance"
xsi:schemaLocat i on="ht t p://j ava.sun.com/xml/ns/j avaee
5 ht t p://j ava.sun.com/xml/ns/j avaee/web−app_2_5.xsd">
<cont ext −param>
<param−name>com.sun.f aces.ver i f yObj ect s</param−name>
<param−val ue>t r ue</param−val ue>
</cont ext −param>
10 <cont ext −param>
<param−name>com.sun.f aces.val i dat eXml </param−name>
<param−val ue>t r ue</param−val ue>
</cont ext −param>
<cont ext −param>
15 <param−name>j avax.f aces.DEFAULT_SUFFIX</param−name>
<param−val ue>.xht ml </param−val ue>
</cont ext −param>
<cont ext −param>
<param−name>f acel et s.DEVELOPMENT</param−name>
20 <param−val ue>t r ue</param−val ue>
</cont ext −param>
<ser vl et >
<ser vl et −name>Faces Ser vl et </ser vl et −name>
<ser vl et −cl ass>j avax.f aces.webapp.FacesServl et </ser vl et −cl ass>
25 <l oad−on−st ar t up>1</l oad−on−st ar t up>
</ser vl et >
<ser vl et −mapping>
<ser vl et −name>Faces Ser vl et </ser vl et −name>
<ur l −pat t er n>∗.xht ml </ur l −pat t er n>
30 </ser vl et −mapping>
<sessi on−conf i g>
<sessi on−t i meout >
30
16
Draft Version – December 14,2006
Draft Version – December 14,2006
3.3.WAR PROJECT CONFIGURATION
</sessi on−t i meout >
35 </sessi on−conf i g>
<welcome−f i l e −l i s t >
<welcome−f i l e >
i ndex.xht ml
</welcome−f i l e >
40 </welcome−f i l e −l i s t >
<!−− addi t i onal s t u f f f or seam −−>
<!−− Seam −−>
<!−−
45 Remember t hat"sampl eProj ect −ej b"speci f i es t he EJB pr oj ect s
name.Meaning t hat"−ej b"i s par t of t he EJB name
−−>
<cont ext −param>
<descr i pt i on>
50 </descr i pt i on>
<param−name>org.j boss.seam.core.i n i t.j ndi Pat t er n</param−name>
<param−val ue>j ava:comp/env/sampl eProj ect −ej b/#{ ejbName }/l ocal </param−val ue>
</cont ext −param>
55 <l i s t ener >
<l i st ener −cl ass>org.j boss.seam.ser vl et.SeamLi stener</l i st ener −cl ass>
</l i s t ener >
<ej b−l ocal −r ef >
60 <ej b−r ef −name>sampl eProj ect −ej b/Acti onBean/l ocal </ej b−r ef −name>
<ej b−r ef −t ype>Sessi on</ej b−r ef −t ype>
<l ocal >dummy.Act i onLocal </l ocal >
<ej b−l i nk >Acti onBean</ej b−l i nk >
</ej b−l ocal −r ef >
65 </web−app>
Listing 3.11:web.xml
This configures the JSF Servlet,facelets and seam.As you can see,if you are using glassfish
as JEE5 server you have to specify the EJB local reference too,on JBoss-AS these entries are
not needed.So every EJB you are using with Seamhas to be specified within web.xml.
In the next step you have to edit faces-config.xml,which is located under the “Configuration
Files” Node in your web project.
The following shows the full configuration file,as you need it with the JSF 1.2 RI.
<?xml version= ’ 1.0 ’ encodi ng= ’ UTF−8 ’?>
<faces−conf i g version="1.2"
xmlns="ht t p://j ava.sun.com/xml/ns/j avaee"
xml ns:xsi ="ht t p://www.w3.org/2001/XMLSchema−i nst ance"
5 xsi:schemaLocat i on="ht t p://j ava.sun.com/xml/ns/j avaee
ht t p://j ava.sun.com/xml/ns/j avaee/web−f acesconf i g_1_2.xsd">
<appl i cat i on>
<view−handl er >
17
Draft Version – December 14,2006
Draft Version – December 14,2006
3.BASIC SEAM
10 com.sun.f acel et s.Facel et Vi ewHandl er
</view−handl er >
</appl i cat i on>
<l i f ec y c l e >
15 <phase−l i s t ener >
org.j boss.seam.j s f.SeamPhaseListener
</phase−l i s t ener >
</l i f ec y c l e >
<!−− seam on JSF 1.2 −−>
20 <appl i cat i on>
<el −r esol ver >
org.j boss.seam.j s f.SeamELResolver
</el −r esol ver >
</appl i cat i on>
25 </faces−conf i g>
Listing 3.12:faces-config.xml
According to the Reference Documentation [
Kin06
,p.121] the following line work around a
bug in the JSF reference implementation.
<appl i cat i on>
<el −r esol ver >
org.j boss.seam.j s f.SeamELResolver
</el −r esol ver >
5 </appl i cat i on>
Listing 3.13:Workaround Bug in RI
Noweverything should be correctly configured and set up.Nowright-click your main project
and select “Run Project”.The project should now be built and be deployed to the Sun Java
SystemApplication Server.
Your configured browser should nowpop up.Point your browser to
http://localhost:8080/sa-
mpleProject-war/sample.xhtml
,and you should see a page that displays “top” and a “click
me” button as shown in [Fig.:
3.5
,p.
19
].If you now click the button,you should see “Hello
World” in the Output log fromthe Sun Java SystemApplication Server 9 ([Fig.:
3.5
,p.
19
]).
3.3.1 Got Errors?
Well,maybe you have received an error page,instead of the really cool output shown above.
If this is the case,you probably have to reread your sample.xhtml file again.You probably
forgot to include  in your HTML tag.
I hope you enjoyed getting this trivial example to work.
18
Draft Version – December 14,2006
Draft Version – December 14,2006
3.3.WAR PROJECT CONFIGURATION
Figure 3.5:Sample project’s output
19
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
 
  
As it would be incredibly boring to develop web applications without a database,lets attach
the built in Derby database.We may want to extend the sample page by a “User” Table,which
holds a username/password combinaton inside the database.For the sake of simplicity,the
password will be stored in plaintext inside the database.
As a first step create a Java Database within Netbeans that contains a table “Users” with two
columns;username and password.
To do so,click Tools→Java DB Database→Create Java DB Database.Name the DB sam-
pleProjectDb,as username enter “test” and as password enter “test123” as shown in
[Fig.:
4.1
,p.
22
].
Next switch to the runtime tab »(CTRL+5)«,select the created sampleProjectDb and right-
click on it,and say “connect”.Now create a table,by right clicking on “Table” and select
“Create”.Create the username and password field,with a VARCHAR length of 40 characters,
and set the username as index field,as shown in [Fig.:
4.2
,p.
22
].
Switch back to the “Projects” view »(CTRL+1)«.Now right-click your EJB project and
select “Entity classes from database”.A wizard opens,where you have to select a data-
source.Select new Datasource.Enter a JNDI Name,name it “sampleProjectDs” as shown in
[Fig.:
4.3
,p.
23
].
21
Draft Version – December 14,2006
Draft Version – December 14,2006
4.SEAM AND DERBY
Figure 4.1:Create new Java database
Figure 4.2:Create new Table
22
Draft Version – December 14,2006
Draft Version – December 14,2006
Figure 4.3:Create new datasource
Now select at least one table,you want to generate entity classes for.click next.In the
following dialod,rename the class from “Users” to “User” because it will only represent a
single instance of a user,additionally change the package name,the classes will be generated,
to “dummy.entity” (see [Fig.:
4.5
,p.
24
]).
Figure 4.4:Change class name
In the next step generate a persistence unit,as shown in the following dialog.Netbeans
creates a correct persistence.xml for you.
23
Draft Version – December 14,2006
Draft Version – December 14,2006
4.SEAM AND DERBY
Figure 4.5:Create persistence unit
Do not forget to add the @Name("user") annotation to the generated entity class,seam oth-
erwise cannot find the component (or specify this in components.xml )
NOTE:specify the JNDI pattern in the components.xml file,that will be deployed with the
web project.To do so,create a new “XML document” within your web project.Name the file
components (.xml) will be attached automatically and select src/conf as folder,as shown in
[Fig.:
4.6
,p.
24
]
Figure 4.6:Create components.xml file
24
Draft Version – December 14,2006
Draft Version – December 14,2006
If you are ready with that,the components.xml file has to be deployed to the/WEB-INF/
folder inside the WAR.To do this,right clickt the web project,select properties build-
>packaging and add that file to the WEB-INF directory as shown in [Fig.:
4.7
,p.
25
].
Figure 4.7:Package components.xml file
If only components.xml would have some content now,but i hope i can help with this little
problem.
<!DOCTYPE components PUBLIC
"−//JBoss/Seam Component Conf i gur at i on DTD 1.1//EN"
"ht t p://j boss.com/product s/seam/components−1.1.dt d">
5 <components>
<component name="org.j boss.seam.core.i n i t">
<pr oper t y name="debug">t r ue</pr oper t y>
</component>
10 <component name="org.j boss.seam.core.i n i t">
<pr oper t y
name="j ndi Pat t er n">j ava:comp/env/sampl eProj ect−ej b/#{ ejbName }/l ocal </pr oper t y>
</component>
15 <component name="ent i t yManager"
cl ass="org.j boss.seam.core.ManagedPersi stenceContext">
<pr oper t y
name="persi st enceUni t Jndi Name">j ava:/sampl eProj ect−ejbPU</pr oper t y>
</component>
20 </components>
Listing 4.1:components.xml
25
Draft Version – December 14,2006
Draft Version – December 14,2006
4.SEAM AND DERBY
Because we moved some of the initialization thins into components.xml we have to remove
the entries from web.xml so that we don’t have conflictiong entries.Additionally some
entries have been added.The full blown web.xml contains:
<?xml version= ’ ’ 1.0 ’ ’ encodi ng= ’ ’ UTF−8 ’ ’?>
<web−app version= ’ ’ 2.5 ’ ’ xmlns= ’ ’ ht t p://j ava.sun.com/xml/ns/j avaee ’ ’
xml ns:xsi = ’ ’ ht t p://www.w3.org/2001/XMLSchema−i nst ance ’ ’
xsi:schemaLocat i on= ’ ’ ht t p://j ava.sun.com/xml/ns/j avaee
5 ht t p://j ava.sun.com/xml/ns/j avaee/web−app_2_5.xsd ’ ’ >
<cont ext −param>
<param−name>com.sun.f aces.ver i f yObj ect s</param−name>
<param−val ue>t r ue</param−val ue>
</cont ext −param>
10 <cont ext −param>
<param−name>com.sun.f aces.val i dat eXml </param−name>
<param−val ue>t r ue</param−val ue>
</cont ext −param>
<cont ext −param>
15 <param−name>j avax.f aces.DEFAULT_SUFFIX</param−name>
<param−val ue>.xht ml </param−val ue>
</cont ext −param>
<cont ext −param>
<param−name>f acel et s.DEVELOPMENT</param−name>
20 <param−val ue>t r ue</param−val ue>
</cont ext −param>
<ser vl et >
<ser vl et −name>Faces Ser vl et </ser vl et −name>
<ser vl et −cl ass>j avax.f aces.webapp.FacesServl et </ser vl et −cl ass>
25 <l oad−on−st ar t up>1</l oad−on−st ar t up>
</ser vl et >
<ser vl et −mapping>
<ser vl et −name>Faces Ser vl et </ser vl et −name>
<ur l −pat t er n>∗.xht ml </ur l −pat t er n>
30 </ser vl et −mapping>
<sessi on−conf i g>
<sessi on−t i meout >
30
</sessi on−t i meout >
35 </sessi on−conf i g>
<welcome−f i l e −l i s t >
<welcome−f i l e >
i ndex.xht ml
</welcome−f i l e >
40 </welcome−f i l e −l i s t >
<!−− addi t i onal s t u f f f or seam −−>
<!−− Seam −−>
<!−−
45 Remember t hat ‘ ‘ seamTest−ej b ’ ’ speci f i es t he EJB pr oj ect s name
and t hat t he JNDI pat t er n i s now speci f i ed i n components.xml
−−>
<!−−
<cont ext −param>
50 <descr i pt i on>
26
Draft Version – December 14,2006
Draft Version – December 14,2006
</descr i pt i on>
<param−name>org.j boss.seam.core.i n i t.j ndi Pat t er n</param−name>
<param−val ue>j ava:comp/env/sampl eProj ect −ej b/#{ ejbName }/l ocal </param−val ue>
</cont ext −param>
55 −−>
<!−− Seam −−>
<l i s t ener >
60 <l i st ener −cl ass>org.j boss.seam.ser vl et.SeamLi stener</l i st ener −cl ass>
</l i s t ener >
< f i l t e r >
< f i l t e r −name>Seam Redi r ect Fi l t e r </f i l t e r −name>
65 < f i l t e r −cl ass>org.j boss.seam.ser vl et.SeamRedi r ect Fi l t er </f i l t e r −cl ass>
</f i l t e r >
< f i l t e r −mapping>
< f i l t e r −name>Seam Redi r ect Fi l t e r </f i l t e r −name>
70 <ur l −pat t er n>∗.xht ml </ur l −pat t er n>
</f i l t e r −mapping>
< f i l t e r >
< f i l t e r −name>Seam Except i on Fi l t e r </f i l t e r −name>
75 < f i l t e r −cl ass>org.j boss.seam.ser vl et.SeamExcept i onFi l t er </f i l t e r −cl ass>
</f i l t e r >
< f i l t e r −mapping>
< f i l t e r −name>Seam Except i on Fi l t e r </f i l t e r −name>
80 <ur l −pat t er n>∗.xht ml </ur l −pat t er n>
</f i l t e r −mapping>
<!−− EJB def i ni t i ons −−>
<ej b−l ocal −r ef >
85 <ej b−r ef −name>sampl eProj ect −ej b/Acti onBean/l ocal </ej b−r ef −name>
<ej b−r ef −t ype>Sessi on</ej b−r ef −t ype>
<l ocal >dummy.Act i onLocal </l ocal >
<ej b−l i nk >Acti onBean</ej b−l i nk >
</ej b−l ocal −r ef >
90
</web−app>
Listing 4.2:web.xml
Now we have to extend the view,to make it possible to add username/password combina-
tions to the database.There will be no exception handling,so don’t use this as production
code!All we need is some form fields in the view,where we can enter our data.We simply
extend our sample.xhtml file fromthe beginning with a few lines.
<!DOCTYPE html PUBLIC"−//W3C//DTD XHTML 1.0 Tr ansi t i onal//EN"
"ht t p://www.w3.org/TR/xht ml 1/DTD/xhtml 1−t r ans i t i onal.dt d">
<html xmlns="ht t p://www.w3.org/1999/xht ml"
4 xmlns:ui ="ht t p://j ava.sun.com/j s f/f acel et s"
27
Draft Version – December 14,2006
Draft Version – December 14,2006
4.SEAM AND DERBY
xmlns:h="ht t p://j ava.sun.com/j s f/ht ml">
<body>
9 <ui:composi t i on t empl at e="sampleTemplate.xht ml">
<ui:def i ne name="t op">
t op
</ui:def i ne>
14
<ui:def i ne name="cont ent">
<h:form>
<h:i nput Text i d="username"
value="#{ user.username}"/>
19 <h:i nput Text i d="password"
value="#{ user.password }"/>
<h:commandButton value="Cl i ck me"
acti on="#{ act i on.sayHel l o }"/>
</h:form>
24 </ui:def i ne>
</ui:composi t i on>
</body>
29 </html>
Listing 4.3:sample.xhtml
This site,does not like very nice,but who cares at the moment,this should be the simplest
thing to change,or is it?
To actually persist the entered user information we still need to write some code.As we
already re-used the view,we’ll reuse the existing action bean too.We simply have to add a
few lines of code.The full code listing contains:
1 package dummy;
import dummy.ent i t y.User;
import j avax.ej b.St at el ess;
import j avax.per si st ence.Ent i t yManager;
6 import j avax.per si st ence.Per si st enceCont ext;
import org.j boss.seam.annot at i ons.I n;
import org.j boss.seam.annot at i ons.Name;
@Statel ess
11 @Name("act i on")
publ i c class Acti onBean implements Act i onLocal {
@In( cr eat e=true,r equi r ed=true )
User user;
16 @Persi stenceContext ( )
pri vat e Ent i t yManager em;
/∗ ∗ Creates a new i nst ance of Acti onBean ∗/
28
Draft Version – December 14,2006
Draft Version – December 14,2006
publ i c Acti onBean ( ) {
21 }
publ i c void sayHel l o ( ) {
System.out.pr i nt l n ("Hel l o World");
System.out.pr i nt l n ("Username:"+ user.getUsername ( ) );
26 System.out.pr i nt l n ("Password:"+ user.getPassword ( ) );
em.per si st ( user );
}
}
Listing 4.4:ActionBean.java
We should now be ready to deploy the application,which will insert username/password
combinations into the database.You can view the content of the database with the built in
“database tool” in Netbeans.Simply switch to the runtime tab,select your DB connection,if
necessary say “connect”.Then select a table and select “View data”.A SQL window will pop
up,that show a select statement,which can be executed if you press the green triangle next
to the connection drop down box.
29
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
  
2.1 New facelets web application,uninitialized
...................
4
2.2 New facelets web application,initialized
....................
5
3.1 Create enterprise application
...........................
7
3.2 Add seamlibrary to EAR file
...........................
8
3.3 Edit build properties
...............................
10
3.4 Add facelets framework
..............................
12
3.5 Sample project’s output
..............................
19
4.1 Create new Java database
............................
22
4.2 Create new Table
.................................
22
4.3 Create new datasource
..............................
23
4.4 Change class name
................................
23
4.5 Create persitence unit
...............................
24
4.6 Create components.xml file
............................
24
4.7 Package components.xml file
...........................
25
31
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006

3.1 application.xml
...................................
8
3.2 ejb-jar.xml
......................................
9
3.3 build.xml
......................................
10
3.4 ActionBean.java
...................................
11
3.5 ActionLocal.java
..................................
11
3.6 sampleTemplate.xhtml
...............................
13
3.7 cssLayout.css
....................................
13
3.8 default.css
......................................
14
3.9 sample code to insert
................................
15
3.10 sample.xhtml
....................................
15
3.11 web.xml
.......................................
16
3.12 faces-config.xml
...................................
17
3.13 Workaround Bug in RI
...............................
18
4.1 components.xml
..................................
25
4.2 web.xml
.......................................
26
4.3 sample.xhtml
....................................
27
4.4 ActionBean.java
...................................
28
33
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006

[Kal06]
KALALI,Masoud.Develop web application with netbeans,seam and Glassfish.
http://weblogs.java.net
.2006
[Kin06]
KING,Gavin.Seam - Contextual Components – version 1.1.0 BETA.
http://www.jboss.org/
.2006
10
,
18
[Leo06]
LEONARD,Brian.Trying out JBoss’ Seam.
http://weblogs.java.net
.2006
35
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006
Draft Version – December 14,2006

introduction,
1
Preconditions,
1
37