; Facelets_ Seam and Netbeans -- IDE configuration howto
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Facelets_ Seam and Netbeans -- IDE configuration howto

VIEWS: 72 PAGES: 41

  • pg 1
									                                                                Draft Version – December 14, 2006
FACELETS , (JB OSS ) S EAM , N ETBEANS
            AND G LASSFISH (SJSAS)
             IDE CONFIGURATION AND BASIC DEVELOPMENT HOWTO




                         Jens Frey <jens.frey@coffeecrew.org>
Draft Version – December 14, 2006
                                                                                                      Draft Version – December 14, 2006
                                                                                        Contents




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 Seam and Derby                                                                                 21

List of Figures                                                                                  31

Listings                                                                                         35

Bibliography                                                                                     35

Index                                                                                            37




                                                                                                  i
Draft Version – December 14, 2006
                                                                                                  Draft Version – December 14, 2006
                                                                              CHAPTER 1




                                                                               Introduction




This document describes how to configure the Netbeans Integrated Development Environ-
ment (IDE) version 5.51 with facelets2 , seam3 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 Hello
World 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 from it, 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
1. I NTRODUCTION

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




                                                                                                                 Draft Version – December 14, 2006
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/FaceletsSupport4

    • 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
                                                                           CHAPTER 2




                                                                         Basic IDE setup




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
2. B ASIC IDE SETUP

And to end the seam library creation desaster, add a new library »SEAM - DEBUG« and include
the following jars.




                                                                                                               Draft Version – December 14, 2006
    • JBOSS - SEAM - DEBUG . JAR


That should do it to have seam libraries setup correctly.



2.2     Facelets

If you are using the facelets plugin1 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 »C TRL +S HIFT-N«
and name it “flSetup” (you may want to set the project location to a different place, like a
temporary folder), click »N EXT« . 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
                                                                                2.3. B ROWSER


After the plugin successfully imported the necessary libraries into the Netbeans IDE, the
frameworks screen looks as shown in [Fig. : 2.2, p. 5]:




                                                                                                  Draft Version – December 14, 2006
                     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 open Tools->Options
from the menubar. In “General” you do of course select firefox as browser, do you?



                                                                                             5
2. B ASIC IDE SETUP

2.4    Glassfish/SJSAS setup




                                                                                                  Draft Version – December 14, 2006
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
                                                                              CHAPTER 3




                                                                                 Basic Seam




This chapter explains the necessary steps to develop a simple seam project from scratch 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 »C TRL +S HIFT-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
    3. B ASIC S EAM

    3.1      EAR project configuration




                                                                                                             Draft Version – December 14, 2006
                                  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 seam library 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 " encoding= "UTF−8" ?>
   < a p p l i c a t i o n version= " 5 " xmlns= " h t t p : / / j a v a . sun . com / xml / ns / javaee "
   x m l n s : x s i = " h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema−i n s t a n c e "
   x s i : s c h e m a L o c a t i o n = " h t t p : / / j a v a . sun . com / xml / ns / javaee
   h t t p : / / j a v a . sun . com / xml / ns / javaee / a p p l i c a t i o n _ 5 . xsd " >
 6    < d i s p l a y −name> s a m p l e P r o j e c t < / d i s p l a y −name>
      <module>
            <web>
                <web−u r i > sampleProject −war . war< / web−u r i >
                < c o n t e x t −r o o t > / sampleProject −war< / c o n t e x t −r o o t >
11          < / web>
      < / module>



    8
                                                                                     3.2. EJB PROJECT CONFIGURATION


      <module>
          < e j b > sampleProject −e j b . j a r < / e j b >
      < / module>




                                                                                                                           Draft Version – December 14, 2006
16    <module>
             < j a v a > jboss −seam . j a r < / j a v a >
      < / module>
   </ application>
                                                 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 " encoding= "UTF−8" ?>

     <ejb−j a r xmlns = " h t t p : / / j a v a . sun . com / xml / ns / javaee "
                       version = " 3 . 0 "
                       x m l n s : x s i = " h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema−i n s t a n c e "
 6                     x s i : s c h e m a L o c a t i o n = " h t t p : / / j a v a . sun . com / xml / ns / javaee
                       h t t p : / / j a v a . sun . com / xml / ns / javaee / ejb−j a r _ 3 _ 0 . xsd " >
      <interceptors>
          <interceptor>
              < i n t e r c e p t o r −c l a s s >
11                  org . j b o s s . seam . e j b . S e a m I n t e r c e p t o r
              < / i n t e r c e p t o r −c l a s s >
          </ interceptor>
      </ interceptors>
      <assembly−d e s c r i p t o r >
16        < i n t e r c e p t o r −b i n d i n g >
              <ejb−name>∗< / ejb−name>
                  < i n t e r c e p t o r −c l a s s >
                         org . j b o s s . seam . e j b . S e a m I n t e r c e p t o r
                  < / i n t e r c e p t o r −c l a s s >
21        < / i n t e r c e p t o r −b i n d i n g >
      < / assembly−d e s c r i p t o r >
     < / ejb−j a r >
                                                     Listing 3.2: ejb-jar.xml



                                                                                                                       9
     3. B ASIC S EAM

     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 from the PDF).




                                                                                                                               Draft Version – December 14, 2006
     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 seam to work correctly.

     Now select the files tab »(C TRL +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 " encoding= "UTF−8" ?>
2 < !−− You may f r e e l y                e d i t t h i s f i l e . See commented b l o c k s below f o r −−>
   < !−− some examples o f how t o customize t h e b u i l d . −−>
   < !−− ( I f you d e l e t e i t and reopen t h e p r o j e c t i t w i l l be r e c r e a t e d . )
   −−>
   < p r o j e c t name= " sampleProject −e j b " d e f a u l t = " d e f a u l t " b a s e d i r = " . "
 7 x m l n s : e j b j a r p r o j e c t = " h t t p : / / www. netbeans . org / ns / j2ee−e j b j a r p r o j e c t / 3 " >
          < d e s c r i p t i o n > B u i l d s , t e s t s , and runs t h e p r o j e c t
          sampleProject −e j b . < / d e s c r i p t i o n >
          < i m p o r t f i l e = " n b p r o j e c t / b u i l d −i m p l . xml " / >

12        < t a r g e t name= "−pre−compile " >
                  <copy f i l e = " $ { meta . i n f } / seam . p r o p e r t i e s "
                  t o d i r = " $ { b u i l d . d i r } / ear−module " / >
          </ target>
     </ project>
                                                    Listing 3.3: build.xml

     Switch Back to the projects view »(C TRL +1)« Because seam encourages 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 system console.

     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
                                                                           3.2. EJB PROJECT CONFIGURATION


     sure you “Session Type –> Stateless” selected and that “create interface –> local” is selected
     too. Click Finish.




                                                                                                            Draft Version – December 14, 2006
     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 . o u t . p r i n t l n ( " H e l 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 »(A LT +S HIFT +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 a v a x . e j b . S t a t e l e s s ;
 4 import org . j b o s s . seam . a n n o t a t i o n s . Name ;

     @Stateless
     @Name( " a c t i o n " )
     public class ActionBean implements A c t i o n L o c a l {
 9
            / ∗ ∗ Creates a new i n s t a n c e o f ActionBean ∗ /
            public ActionBean ( ) {
            }

14          public void s a y H e l l o ( ) {
                System . o u t . p r i n t l n ( " H e l l o World " ) ;
            }
     }
                                                Listing 3.4: ActionBean.java


     And, of course we need a interface:
     package dummy ;

 3 import j a v a x . e j b . L o c a l ;

   /∗ ∗
    ∗ T h i s i s t h e business i n t e r f a c e f o r A c t i o n e n t e r p r i s e bean .
    ∗/
 8 @Local
   public i n t e r f a c e A c t i o n L o c a l {
        void s a y H e l l o ( ) ;



                                                                                                      11
3. B ASIC S EAM


}
                                Listing 3.5: ActionLocal.java




                                                                                                   Draft Version – December 14, 2006
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 trim web.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
                                                                              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 »(C TRL +1)« ). The




                                                                                                               Draft Version – December 14, 2006
     sampleTemplate.xhtml file is like follows (for those who don’t use facelets plugin).
   < !DOCTYPE html PUBLIC " −//W3C / / DTD XHTML 1 . 0 T r a n s i t i o n a l / / EN"
   " h t t p : / / www. w3 . org / TR / xhtml1 /DTD/ xhtml1−t r a n s i t i o n a l . d t d " >
 3 <html xmlns= " h t t p : / / www. w3 . org / 1 9 9 9 / x h t m l "
              xmlns : u i = " h t t p : / / j a v a . sun . com / j s f / f a c e l e t s " >

          <head>
                <meta h t t p −e q u i v = " Content−Type " content= " t e x t / h t m l ;
 8              c h a r s e t =UTF−8" / >
                < l i n k h r e f = " css / d e f a u l t . css " r e l = " s t y l e s h e e t "
                type= " t e x t / css " / >
                < l i n k h r e f = " css / cssLayout . css " r e l = " s t y l e s h e e t "
                type= " t e x t / css " / >
13              < t i t l e > F a c e l e t s Template< / t i t l e >
          < / head>

          <body>

18              <div i d = " t o p " class= " t o p " >
                      < u i : i n s e r t name= " t o p " >Top< / u i : i n s e r t >
                < / div>

                <div i d = " c o n t e n t " class= " c e n t e r _ c o n t e n t " >
23                    < u i : i n s e r t name= " c o n t e n t " >Content< / u i : i n s e r t >
                < / div>

          < / body>

28 < / html>
                                         Listing 3.6: sampleTemplate.xhtml


     The two CSS files (cssLayout.css and default.css) are as follows.
   #top {
 2     position : relative ;
       background−c o l o r : #036 f a b ;
       color : white ;
       padding : 5px ;
       margin : 0px 0px 10px 0px ;
 7 }

   # bottom {
         position : relative ;
        background−c o l o r : # c 2 d f e f ;
12       padding : 5px ;
        margin : 10px 0px 0px 0px ;
   }

     #left {
17       float : l e f t ;



                                                                                                         13
     3. B ASIC S EAM

            background−c o l o r : #ece3a5 ;
            padding : 5px ;
            w i d t h : 150px ;




                                                                                                       Draft Version – December 14, 2006
     }
22
   #right {
       float : right ;
       background−c o l o r : #ece3a5 ;
       padding : 5px ;
27     w i d t h : 150px ;
   }

   . center_content {
        position : relative ;
32      background−c o l o r : #dddddd ;
        padding : 5px ;
   }

     . left_content {
37        background−c o l o r : #dddddd ;
          padding : 5px ;
          margin− l e f t : 170px ;
     }

42 . r i g h t _ c o n t e n t {
            background−c o l o r : #dddddd ;
            padding : 5px ;
            margin : 0px 170px 0px 170px ;
     }
47
   #top a : l i n k , #top a : v i s i t e d {
     color : white ;
     f o n t −w e i g h t : b o l d ;
     t e x t −d e c o r a t i o n : none ;
52 }

   # t o p a : l i n k : hover , # t o p a : v i s i t e d : hover   {
       color : black ;
       f o n t −w e i g h t : b o l d ;
57     t e x t −d e c o r a t i o n : u n d e r l i n e ;
   }
                                              Listing 3.7: cssLayout.css

   body {
 2     background−c o l o r : # f f f f f f ;
       f o n t −s i z e : 12px ;
       f o n t −f a m i l y : Verdana , " Verdana CE" , A r i a l , " A r i a l CE" ,
       " Lucida Grande CE" , l u c i d a , " H e l v e t i c a CE" , sans−s e r i f ;
       c o l o r : #000000;
 7     margin : 10px ;
   }

     h1 {
            f o n t −f a m i l y : A r i a l , " A r i a l CE" , " Lucida Grande CE" , l u c i d a ,
12          " H e l v e t i c a CE" , sans−s e r i f ;



     14
                                                                                  3.3. WAR PROJECT CONFIGURATION


            border−bottom : 1px s o l i d #AFAFAF ;
            f o n t −s i z e : 16px ;
            f o n t −w e i g h t : b o l d ;




                                                                                                                   Draft Version – December 14, 2006
            margin : 0px ;
17          padding : 0px ;
            c o l o r : #D20005 ;
     }

     a: link , a: visited {
22      c o l o r : #045491;
        f o n t −w e i g h t : b o l d ;
        t e x t −d e c o r a t i o n : none ;
     }

27 a : l i n k : hover , a : v i s i t e d : hover            {
         c o l o r : #045491;
         f o n t −w e i g h t : b o l d ;
         t e x t −d e c o r a t i o n : u n d e r l i n e ;
     }
                                                       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= " C l i c k me" action = " # { a c t i o n . s a y H e l 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 T r a n s i t i o n a l / / EN"
 2 " h t t p : / / www. w3 . org / TR / xhtml1 /DTD/ xhtml1−t r a n s i t i o n a l . d t d " >
     <html xmlns= " h t t p : / / www. w3 . org / 1 9 9 9 / x h t m l "
           xmlns : u i = " h t t p : / / j a v a . sun . com / j s f / f a c e l e t s "
           xmlns : h= " h t t p : / / j a v a . sun . com / j s f / h t m l " >

 7          <body>

                   < u i : c o m p o s i t i o n t e m p l a t e = " sampleTemplate . x h t m l " >

                          < u i : d e f i n e name= " t o p " >
12                                top
                          < / ui : define>

                          < u i : d e f i n e name= " c o n t e n t " >
                                  <h : form>
17                                <h : commandButton value= " C l i c k me"
                                  action = " # { a c t i o n . s a y H e l l o } " / >



                                                                                                             15
     3. B ASIC S EAM

                                  < / h : form>
                           < / ui : define>




                                                                                                                                            Draft Version – December 14, 2006
22                  < / u i : composition>

           < / body>
     < / html>
                                                      Listing 3.10: sample.xhtml


     The action that was configured within the EJB project as a SLSB, is now used from within 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 seam specific stuff. The full file contains:


     <?xml version= " 1 . 0 " encoding= "UTF−8" ?>
     <web−app version= " 2 . 5 " xmlns= " h t t p : / / j a v a . sun . com / xml / ns / javaee "
     x m l n s : x s i = " h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema−i n s t a n c e "
     x s i : s c h e m a L o c a t i o n = " h t t p : / / j a v a . sun . com / xml / ns / javaee
 5   h t t p : / / j a v a . sun . com / xml / ns / javaee / web−app_2_5 . xsd " >
              < c o n t e x t −param>
                     <param−name>com . sun . f a c e s . v e r i f y O b j e c t s < / param−name>
                     <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
10            < c o n t e x t −param>
                     <param−name>com . sun . f a c e s . v a l i d a t e X m l < / param−name>
                     <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
              < c o n t e x t −param>
15                   <param−name> j a v a x . f a c e s . DEFAULT_SUFFIX< / param−name>
                     <param−v a l u e > . x h t m l < / param−v a l u e >
              < / c o n t e x t −param>
              < c o n t e x t −param>
                     <param−name> f a c e l e t s .DEVELOPMENT< / param−name>
20                   <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
              <servlet>
                     < s e r v l e t −name>Faces S e r v l e t < / s e r v l e t −name>
                     < s e r v l e t −c l a s s > j a v a x . f a c e s . webapp . F a c e s S e r v l e t < / s e r v l e t −c l a s s >
25                   <load−on−s t a r t u p >1< / load−on−s t a r t u p >
              </ servlet>
              < s e r v l e t −mapping>
                     < s e r v l e t −name>Faces S e r v l e t < / s e r v l e t −name>
                     < u r l −p a t t e r n > ∗ . x h t m l < / u r l −p a t t e r n >
30            < / s e r v l e t −mapping>
              <session−c o n f i g >
                     <session−t i m e o u t >
                              30



     16
                                                                               3.3. WAR PROJECT CONFIGURATION


                < / session−t i m e o u t >
35        < / session−c o n f i g >
          <welcome−f i l e − l i s t >




                                                                                                                                     Draft Version – December 14, 2006
          <welcome− f i l e >
                      index . xhtml
                < / welcome− f i l e >
40        < / welcome−f i l e − l i s t >

          < !−− a d d i t i o n a l s t u f f f o r seam −−>
          < !−− Seam −−>
          < !−−
45               Remember t h a t " sampleProject −e j b " s p e c i f i e s t h e EJB p r o j e c t s
                 name . Meaning t h a t "−e j b " i s p a r t o f t h e EJB name
          −−>
          < c o n t e x t −param>
                 <description>
50               </ description>
                 <param−name>org . j b o s s . seam . core . i n i t . j n d i P a t t e r n < / param−name>
                 <param−v a l u e >java:comp / env / sampleProject −e j b / # { ejbName } / l o c a l < / param−v a l u e >
          < / c o n t e x t −param>

55        <listener>
               < l i s t e n e r −c l a s s >org . j b o s s . seam . s e r v l e t . SeamListener< / l i s t e n e r −c l a s s >
          </ listener>

         <ejb−l o c a l −r e f >
60       <ejb−r e f −name> sampleProject −e j b / ActionBean / l o c a l < / ejb−r e f −name>
         <ejb−r e f −t y p e >Session< / ejb−r e f −t y p e >
         < l o c a l >dummy . A c t i o n L o c a l < / l o c a l >
                <ejb−l i n k >ActionBean< / ejb−l i n k >
         < / ejb−l o c a l −r e f >
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 Seam has 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 ’ encoding= ’UTF−8 ’ ?>
   <faces−c o n f i g version= " 1 . 2 "
                      xmlns= " h t t p : / / j a v a . sun . com / xml / ns / javaee "
                      x m l n s : x s i = " h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema−i n s t a n c e "
 5                    x s i : s c h e m a L o c a t i o n = " h t t p : / / j a v a . sun . com / xml / ns / javaee
                      h t t p : / / j a v a . sun . com / xml / ns / javaee / web−f a c e s c o n f i g _ 1 _ 2 . xsd " >

          <application>
             <view−h a n d l e r >



                                                                                                                          17
     3. B ASIC S EAM

10                  com . sun . f a c e l e t s . FaceletViewHandler
              < / view−h a n d l e r >
          </ application>




                                                                                                      Draft Version – December 14, 2006
         <lifecycle>
15            <phase−l i s t e n e r >
                      org . j b o s s . seam . j s f . SeamPhaseListener
              < / phase−l i s t e n e r >
         </ lifecycle>
         < !−− seam on JSF 1 . 2 −−>
20       <application>
              < e l −r e s o l v e r >
                      org . j b o s s . seam . j s f . SeamELResolver
              < / e l −r e s o l v e r >
         </ application>
25 < / faces−c o n f 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.
          <application>
              < e l −r e s o l v e r >
                      org . j b o s s . seam . j s f . SeamELResolver
              < / e l −r e s o l v e r >
 5        </ application>
                                    Listing 3.13: Workaround Bug in RI


     Now everything should be correctly configured and set up. Now right-click your main project
     and select “Run Project”. The project should now be built and be deployed to the Sun Java
     System Application Server.

     Your configured browser should now pop 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 from the Sun Java System Application 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 xmlns:h="http://java.sun.com/jsf/html" in your HTML tag.

     I hope you enjoyed getting this trivial example to work.




     18
                            3.3. WAR PROJECT CONFIGURATION




                                                             Draft Version – December 14, 2006
Figure 3.5: Sample project’s output




                                                       19
Draft Version – December 14, 2006
                                                                                               Draft Version – December 14, 2006
                                                                           CHAPTER 4




                                                                        Seam and Derby




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 »(C TRL +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 »(C TRL +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
4. S EAM AND D ERBY




                                                             Draft Version – December 14, 2006
                      Figure 4.1: Create new Java database




                          Figure 4.2: Create new Table




22
                                                                                                  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
4. S EAM AND D ERBY




                                                                                              Draft Version – December 14, 2006
                            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
     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].




                                                                                                                              Draft Version – December 14, 2006
                                      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 C o n f i g u r a t i o n DTD 1 . 1 / / EN"
     " h t t p : / / j b o s s . com / p r o d u c t s / seam / components −1.1. d t d " >

 5 <components>
           <component name= " org . j b o s s . seam . core . i n i t " >
                 < p r o p e r t y name= " debug " > t r u e < / p r o p e r t y >
           < / component>

10         <component name= " org . j b o s s . seam . core . i n i t " >
                 <property
                 name= " j n d i P a t t e r n " >java:comp / env / sampleProject −e j b / # { ejbName } / l o c a l < / p r o p e r t y >
           < / component>

15       <component name= " e n t i t y M a n a g e r "
                         c l a s s = " org . j b o s s . seam . core . ManagedPersistenceContext " >
               <property
               name= " p e r si s te n c e U n i t Jn d i N a m e " > j a v a : / sampleProject −ejbPU< / p r o p e r t y >
         < / component>
20 < / components>
                                              Listing 4.1: components.xml



                                                                                                                     25
     4. S EAM AND D ERBY

     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:




                                                                                                                                            Draft Version – December 14, 2006
     <?xml version= ’ ’ 1 . 0 ’ ’ encoding= ’ ’UTF−8 ’ ’ ?>
     <web−app version= ’ ’ 2 . 5 ’ ’ xmlns= ’ ’ h t t p : / / j a v a . sun . com / xml / ns / javaee ’ ’
     x m l n s : x s i = ’ ’ h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema−i n s t a n c e ’ ’
     x s i : s c h e m a L o c a t i o n = ’ ’ h t t p : / / j a v a . sun . com / xml / ns / javaee
 5   h t t p : / / j a v a . sun . com / xml / ns / javaee / web−app_2_5 . xsd ’ ’ >
              < c o n t e x t −param>
                     <param−name>com . sun . f a c e s . v e r i f y O b j e c t s < / param−name>
                     <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
10            < c o n t e x t −param>
                     <param−name>com . sun . f a c e s . v a l i d a t e X m l < / param−name>
                     <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
              < c o n t e x t −param>
15                   <param−name> j a v a x . f a c e s . DEFAULT_SUFFIX< / param−name>
                     <param−v a l u e > . x h t m l < / param−v a l u e >
              < / c o n t e x t −param>
              < c o n t e x t −param>
                     <param−name> f a c e l e t s .DEVELOPMENT< / param−name>
20                   <param−v a l u e > t r u e < / param−v a l u e >
              < / c o n t e x t −param>
              <servlet>
                     < s e r v l e t −name>Faces S e r v l e t < / s e r v l e t −name>
                     < s e r v l e t −c l a s s > j a v a x . f a c e s . webapp . F a c e s S e r v l e t < / s e r v l e t −c l a s s >
25                   <load−on−s t a r t u p >1< / load−on−s t a r t u p >
              </ servlet>
              < s e r v l e t −mapping>
                     < s e r v l e t −name>Faces S e r v l e t < / s e r v l e t −name>
                     < u r l −p a t t e r n > ∗ . x h t m l < / u r l −p a t t e r n >
30            < / s e r v l e t −mapping>
              <session−c o n f i g >
                     <session−t i m e o u t >
                              30
                     < / session−t i m e o u t >
35            < / session−c o n f i g >
              <welcome−f i l e − l i s t >
                     <welcome− f i l e >
                              index . xhtml
                     < / welcome− f i l e >
40            < / welcome−f i l e − l i s t >

            < !−− a d d i t i o n a l s t u f f f o r seam −−>
            < !−− Seam −−>
            < !−−
45                 Remember t h a t ‘ ‘ seamTest−e j b ’ ’ s p e c i f i e s t h e EJB p r o j e c t s name
                   and t h a t t h e JNDI p a t t e r n i s now s p e c i f i e d i n components . xml
            −−>
            < !−−
            < c o n t e x t −param>
50                 <description>



     26
                </ description>
                <param−name>org . j b o s s . seam . core . i n i t . j n d i P a t t e r n < / param−name>
                <param−v a l u e >java:comp / env / sampleProject −e j b / # { ejbName } / l o c a l < / param−v a l u e >




                                                                                                                                                 Draft Version – December 14, 2006
         < / c o n t e x t −param>
55       −−>

         < !−− Seam −−>

         <listener>
60            < l i s t e n e r −c l a s s >org . j b o s s . seam . s e r v l e t . SeamListener< / l i s t e n e r −c l a s s >
         </ listener>

         <filter>
                < f i l t e r −name>Seam R e d i r e c t F i l t e r < / f i l t e r −name>
65              < f i l t e r −c l a s s >org . j b o s s . seam . s e r v l e t . S e a m R e d i r e c t F i l t e r < / f i l t e r −c l a s s >
         </ f i l t e r >

         < f i l t e r −mapping>
                  < f i l t e r −name>Seam R e d i r e c t F i l t e r < / f i l t e r −name>
70                < u r l −p a t t e r n > ∗ . x h t m l < / u r l −p a t t e r n >
         < / f i l t e r −mapping>

         <filter>
                < f i l t e r −name>Seam E x c e p t i o n F i l t e r < / f i l t e r −name>
75              < f i l t e r −c l a s s >org . j b o s s . seam . s e r v l e t . S e a m E x c e p t i o n F i l t e r < / f i l t e r −c l a s s >
         </ f i l t e r >

         < f i l t e r −mapping>
                  < f i l t e r −name>Seam E x c e p t i o n F i l t e r < / f i l t e r −name>
80                < u r l −p a t t e r n > ∗ . x h t m l < / u r l −p a t t e r n >
         < / f i l t e r −mapping>

         < !−− EJB d e f i n i t i o n s −−>
         <ejb−l o c a l −r e f >
85             <ejb−r e f −name> sampleProject −e j b / ActionBean / l o c a l < / ejb−r e f −name>
               <ejb−r e f −t y p e >Session< / ejb−r e f −t y p e >
               < l o c a l >dummy . A c t i o n L o c a l < / l o c a l >
               <ejb−l i n k >ActionBean< / ejb−l i n k >
         < / ejb−l o c a l −r e f >
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 from the beginning with a few lines.


   < !DOCTYPE html PUBLIC " −//W3C / / DTD XHTML 1 . 0 T r a n s i t i o n a l / / EN"
   " h t t p : / / www. w3 . org / TR / xhtml1 /DTD/ xhtml1−t r a n s i t i o n a l . d t d " >
   <html xmlns= " h t t p : / / www. w3 . org / 1 9 9 9 / x h t m l "
 4            xmlns : u i = " h t t p : / / j a v a . sun . com / j s f / f a c e l e t s "



                                                                                                                                      27
     4. S EAM AND D ERBY

             xmlns : h= " h t t p : / / j a v a . sun . com / j s f / h t m l " >

           <body>




                                                                                                    Draft Version – December 14, 2006
 9              < u i : c o m p o s i t i o n t e m p l a t e = " sampleTemplate . x h t m l " >

                       < u i : d e f i n e name= " t o p " >
                               top
                       < / ui : define>
14
                       < u i : d e f i n e name= " c o n t e n t " >
                               <h : form>
                                       <h : i n p u t T e x t i d = " username "
                                       value= " # { user . username } " / >
19                                     <h : i n p u t T e x t i d = " password "
                                       value= " # { user . password } " / >
                                       <h : commandButton value= " C l i c k me"
                                       action = " # { a c t i o n . s a y H e l l o } " / >
                               < / h : form>
24                     < / ui : define>

                < / u i : composition>

           < / 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 . e n t i t y . User ;
   import     javax . ejb . Stateless ;
   import     j a v a x . p e r s i s t e n c e . EntityManager ;
 6 import     javax . persistence . PersistenceContext ;
   import     org . j b o s s . seam . a n n o t a t i o n s . I n ;
   import     org . j b o s s . seam . a n n o t a t i o n s . Name ;

     @Stateless
11 @Name( " a c t i o n " )
     public class ActionBean implements A c t i o n L o c a l {
        @In ( c r e a t e =true , r e q u i r e d = t r u e )
         User user ;

16        @PersistenceContext ( )
          p r i v a t e EntityManager em;

           / ∗ ∗ Creates a new i n s t a n c e o f ActionBean ∗ /




     28
          public ActionBean ( ) {
21        }




                                                                                                    Draft Version – December 14, 2006
          public void s a y H e l l o ( )        {
              System . o u t . p r i n t l n   ( " H e l l o World " ) ;
              System . o u t . p r i n t l n   ( " Username : " + user . getUsername ( ) ) ;
26            System . o u t . p r i n t l n   ( " Password : " + user . getPassword ( ) ) ;
             em. p e r s i s t ( 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
                                                                             List of Figures




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 seam library 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
                                                                                       Listings




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
                                                                           Bibliography




[Kal06] KALALI, Masoud. Develop web application with netbeans ,seam and Glassfish.
        http://weblogs.java.net. 2006

[Kin06] K ING, Gavin. Seam - Contextual Components – version 1.1.0 BETA.
        http://www.jboss.org/. 2006 10, 18

[Leo06] L EONARD, Brian. Trying out JBoss’ Seam.
        http://weblogs.java.net. 2006




                                                                                    35
Draft Version – December 14, 2006
                           Draft Version – December 14, 2006
                   Index




introduction, 1

Preconditions, 1




                     37

								
To top