Facelets,
(JBoss) Seam, Netbeans and Glassfish (SJSAS)
IDE configuration and basic development
howto
Jens Frey <jens.frey@coffeecrew.org>
Contents
This document describes how
to configure the Netbeans IDE
version 5.51
with
facelets2,
seam3
and 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
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:
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.
- 1
- Download from http://www.netbeans.org
- 2
- Download from https://facelets.dev.java.net/
- 3
- Download from http://labs.jboss.com/portal/jbossseam
- 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
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.
And to end the seam library creation desaster, add a new library
>>seam-debug<<
and include the following jars.
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 >>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
After the plugin successfully imported the necessary libraries into
the Netbeans IDE, the frameworks screen looks as shown in
[Abb.: 2.2]:
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?
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 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 ;))
- 1
- See the download link in [Kapitel 1.1], then unzip and install
manually (via Tools->Update Center->Install manually ...)
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 >>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
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 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.
Listing 1:
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/application_5.xsd">
<display-name>sampleProject</display-name>
<module>
<web>
<web-uri>sampleProject-war.war</web-uri>
<context-root>/sampleProject-war</context-root>
</web>
</module>
<module>
<ejb>sampleProject-ejb.jar</ejb>
</module>
<module>
<java>jboss-seam.jar</java>
</module>
</application>
3.2 EJB
project configuration
The next step is configuring the
EJB project properly. Right click your EJB project
(sampleProject-ejb) select New->File/Folder,
choose File type
Enterprise->Standard
Deployment Descriptor. The creates
an ejb-jar.xml
file. The full configuration file is printed
in [Listing 2] (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”.
<?xml version="1.0"
encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<interceptors>
<interceptor>
<interceptor-class>
org.jboss.seam.ejb.SeamInterceptor
</interceptor-class>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>
org.jboss.seam.ejb.SeamInterceptor
</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
You can find further information according to this topic on the
documents page, for version 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).
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 >>(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" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.)
-->
<project name="sampleProject-ejb" default="default" basedir="."
xmlns:ejbjarproject="http://www.netbeans.org/ns/j2ee-ejbjarproject/3">
<description>Builds, tests, and runs the project
sampleProject-ejb.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-pre-compile">
<copy file="${meta.inf}/seam.properties"
todir="${build.dir}/ear-module"/>
</target>
</project>
Switch Back to the projects view >>(Ctrl+1)<<
Because seam encourages
you to use [s]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 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.println("Hello 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).
Listing 4:
ActionBean.java
package dummy;
import javax.ejb.Stateless;
import org.jboss.seam.annotations.Name;
@Stateless
@Name("action")
public class ActionBean implements ActionLocal {
/** Creates a new instance of ActionBean */
public ActionBean() {
}
public void sayHello() {
System.out.println("Hello World");
}
}
And, of course we need a interface:
Listing 5:
ActionLocal.java
package dummy;
import javax.ejb.Local;
/**
* This is the business interface for Action enterprise bean.
*/
@Local
public interface ActionLocal {
void sayHello();
}
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 ability 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 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 CSS
style. If you are not using the plugin, you have to create the folder
and CSS yourself.
Figure 3.4: Add facelets framework
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 “Layout
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).
Listing 6:
sampleTemplate.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<link href="css/default.css" rel="stylesheet"
type="text/css" />
<link href="css/cssLayout.css" rel="stylesheet"
type="text/css" />
<title>Facelets Template</title>
</head>
<body>
<div id="top" class="top">
<ui:insert name="top">Top</ui:insert>
</div>
<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>
</body>
</html>
The two CSS files (cssLayout.css
and default.css)
are as follows.
#top {
position: relative;
background-color: #036fab;
color: white;
padding: 5px;
margin: 0px 0px 10px 0px;
}
#bottom {
position: relative;
background-color: #c2dfef;
padding: 5px;
margin: 10px 0px 0px 0px;
}
#left {
float: left;
background-color: #ece3a5;
padding: 5px;
width: 150px;
}
#right {
float: right;
background-color: #ece3a5;
padding: 5px;
width: 150px;
}
.center_content {
position: relative;
background-color: #dddddd;
padding: 5px;
}
.left_content {
background-color: #dddddd;
padding: 5px;
margin-left: 170px;
}
.right_content {
background-color: #dddddd;
padding: 5px;
margin: 0px 170px 0px 170px;
}
#top a:link, #top a:visited {
color: white;
font-weight : bold;
text-decoration: none;
}
#top a:link:hover, #top a:visited:hover {
color: black;
font-weight : bold;
text-decoration : underline;
}
body {
background-color: #ffffff;
font-size: 12px;
font-family: Verdana, "Verdana CE", Arial, "Arial CE",
"Lucida Grande CE", lucida, "Helvetica CE", sans-serif;
color: #000000;
margin: 10px;
}
h1 {
font-family: Arial, "Arial CE", "Lucida Grande CE", lucida,
"Helvetica CE", sans-serif;
border-bottom: 1px solid #AFAFAF;
font-size: 16px;
font-weight: bold;
margin: 0px;
padding: 0px;
color: #D20005;
}
a:link, a:visited {
color: #045491;
font-weight : bold;
text-decoration: none;
}
a:link:hover, a:visited:hover {
color: #045491;
font-weight : bold;
text-decoration : underline;
}
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 sampleTemplate.xhtml
in this case. Click finish. Insert the following code into the
generated file:
Listing 9:
sample code to insert
<h:form>
<h:commandButton value="Click me" action="#{action.sayHello}"/>
</h:form>
The full sample.xhtml
file now looks like this.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<ui:composition template="sampleTemplate.xhtml">
<ui:define name="top">
top
</ui:define>
<ui:define name="content">
<h:form>
<h:commandButton value="Click me"
action="#{action.sayHello}"/>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
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="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.xhtml
</welcome-file>
</welcome-file-list>
<!-- additional stuff for seam -->
<!-- Seam -->
<!--
Remember that "sampleProject-ejb" specifies the EJB projects
name. Meaning that "-ejb" is part of the EJB name
-->
<context-param>
<description>
</description>
<param-name>org.jboss.seam.core.init.jndiPattern</param-name>
<param-value>java:comp/env/sampleProject-ejb/#{ejbName}/local</param-value>
</context-param>
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<ejb-local-ref>
<ejb-ref-name>sampleProject-ejb/ActionBean/local</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>dummy.ActionLocal</local>
<ejb-link>ActionBean</ejb-link>
</ejb-local-ref>
</web-app>
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.
Listing 12:
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<application>
<view-handler>
com.sun.facelets.FaceletViewHandler
</view-handler>
</application>
<lifecycle>
<phase-listener>
org.jboss.seam.jsf.SeamPhaseListener
</phase-listener>
</lifecycle>
<!-- seam on JSF 1.2 -->
<application>
<el-resolver>
org.jboss.seam.jsf.SeamELResolver
</el-resolver>
</application>
</faces-config>
According to the Reference Documentation [Kin06,
p. 121] the
following line work around a bug in the JSF reference implementation.
Listing 13:
Workaround Bug in RI
<application>
<el-resolver>
org.jboss.seam.jsf.SeamELResolver
</el-resolver>
</application>
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/sampleProject-war/sample.xhtml,
and you should see a page that displays “top” and a
“click me”
button as shown in [Abb.: 3.5].
If you now click
the button, you should see “Hello World” in the
Output log from the
Sun Java System Application Server 9 ([Abb.: 3.5]).
Figure 3.5: Sample project's output
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.
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
sampleProjectDb, as username enter “test” and as
password enter
“test123” as shown in [Abb.: 4.1].
Figure 4.1: Create new Java database
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
[Abb.: 4.2].
Figure 4.2: Create new Table
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 datasource. Select new Datasource.
Enter a JNDI Name, name it “sampleProjectDs” as
shown in
[Abb.: 4.3].
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 [Abb.: 4.4]).
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.
Figure 4.5: Create persistence unit
Do not forget to add the @Name("user") annotation to the generated
entity class, seam otherwise 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 [Abb.: 4.6]
Figure 4.6: Create components.xml
file
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
[Abb.: 4.7].
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.
Listing 14:
components.xml
<!DOCTYPE components PUBLIC
"-//JBoss/Seam Component Configuration DTD 1.1//EN"
"http://jboss.com/products/seam/components-1.1.dtd">
<components>
<component name="org.jboss.seam.core.init">
<property name="debug">true</property>
</component>
<component name="org.jboss.seam.core.init">
<property
name="jndiPattern">java:comp/env/sampleProject-ejb/#{ejbName}/local</property>
</component>
<component name="entityManager"
class="org.jboss.seam.core.ManagedPersistenceContext">
<property
name="persistenceUnitJndiName">java:/sampleProject-ejbPU</property>
</component>
</components>
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'' encoding=''UTF-8''?>
<web-app version=''2.5'' xmlns=''http://java.sun.com/xml/ns/javaee''
xmlns:xsi=''http://www.w3.org/2001/XMLSchema-instance''
xsi:schemaLocation=''http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd''>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.xhtml
</welcome-file>
</welcome-file-list>
<!-- additional stuff for seam -->
<!-- Seam -->
<!--
Remember that ``seamTest-ejb'' specifies the EJB projects name
and that the JNDI pattern is now specified in components.xml
-->
<!--
<context-param>
<description>
</description>
<param-name>org.jboss.seam.core.init.jndiPattern</param-name>
<param-value>java:comp/env/sampleProject-ejb/#{ejbName}/local</param-value>
</context-param>
-->
<!-- Seam -->
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<filter>
<filter-name>Seam Redirect Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamRedirectFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Redirect Filter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter>
<filter-name>Seam Exception Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamExceptionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Exception Filter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<!-- EJB definitions -->
<ejb-local-ref>
<ejb-ref-name>sampleProject-ejb/ActionBean/local</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>dummy.ActionLocal</local>
<ejb-link>ActionBean</ejb-link>
</ejb-local-ref>
</web-app>
Now we have to extend the view, to make it possible to add
username/password combinations 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 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<ui:composition template="sampleTemplate.xhtml">
<ui:define name="top">
top
</ui:define>
<ui:define name="content">
<h:form>
<h:inputText id="username" value="#{user.username}"/>
<h:inputText id="password" value="#{user.password}"/>
<h:commandButton value="Click me" action="#{action.sayHello}"/>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
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:
Listing 17:
ActionBean.java
package dummy;
import dummy.entity.User;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
@Stateless
@Name("action")
public class ActionBean implements ActionLocal {
@In(create=true, required=true)
User user;
@PersistenceContext()
private EntityManager em;
/** Creates a new instance of ActionBean */
public ActionBean() {
}
public void sayHello() {
System.out.println("Hello World");
System.out.println("Username: " + user.getUsername());
System.out.println("Password: " + user.getPassword());
em.persist(user);
}
}
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.
References
- [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
- [Leo06]
- Leonard,
Brian.
Trying out JBoss' Seam.
http://weblogs.java.net.
2006
Index