First JBoss Seam tutorial

Introduction

This tutorials shows how to setup a application using JBoss Seam, JSF and Facelets. I will show a configuration for Tomcat and the JBoss Application Server. Furthermore, you can learn how to use Maven to define the dependencies.
Version

Tutorial version 1.0 by Sebastian Hennebrueder

Library versions

*

Seam 2.0.1.GA
*

Tomcat 6
*

JBoss Application Server 4.2 (not 4.0)

Marketing

Advanced Training and Education

Range of advanced trainings on Hibernate, EJB3, JBoss Seam, Design Patterns, and Enterprise Best Practices

The training can be customized to your needs.

Support, consulting and development

Get the amount of help you need by experienced trainers and developers. An hour of support can save you a lot of time, Code and Design Reviews insures that the best practices are being followed! Reduce solving and testing time. We can implement a part of your application, create prototypes or proof of concepts.
Setup the project

Set up a web project with your preferred IDE. We will use a WAR archive for deployment.
Seam configuration files

We need the following configuration files.

components.xml

This file configures Seam and our own components. I prefer to configure my components with annotations and not in this file but still we can do it here as well.

Place: WEB-INF folder in a WAR archive, META-INF folder in a JAR. We have a WAR archive.

Just tell Seam that we don’t want it to manage transactions (at the moment).


xmlns:core="http://jboss.com/products/seam/core"
xmlns:persistence="http://jboss.com/products/seam/persistence"
xmlns:security="http://jboss.com/products/seam/security"
xmlns:drools="http://jboss.com/products/seam/drools"
xmlns:web="http://jboss.com/products/seam/web"
xmlns:mail="http://jboss.com/products/seam/mail"
xmlns:transaction="http://jboss.com/products/seam/transaction"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd
http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd
http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd
http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.0.xsd
http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.0.xsd
http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.0.xsd
http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd">

seam.properties

Includes settings which can be used in components.xml. Apart from this, it is a marker for Seam that a archive (WAR or JAR) includes Seam components. So just create an empty file.

Place: root source folder (same folder as your Java classes.

pages.xml

Includes navigation rules and is comparable to the JSF faces-config.xml. In addition we could add security rules as well.

Place: WEB-INF of a war project

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd"
no-conversation-view-id="/home.xhtml"
login-view-id="/login.xhtml">




Unexpected error, please try again

faces-config.xml

Normal JavaServer Faces configuration file. The default rendering of Seam is JavaServer Faces, this is why we need this config file.

We define default and supported languages and the prefix of the resource bundle.

This example will use facelets for rendering. This is why we have to define the facelets view handler in this configuration file.

Do not place JSF navigation rules here as with Seam we will use the pages.xml for navigation.


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">

messages

en
en

com.sun.facelets.FaceletViewHandler

web.xml

Place: well it is where it always should be in a webapp

Defines Seam and JSF specific settings, filters and servlets.


xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

org.jboss.seam.servlet.SeamListener
Seam Filter
org.jboss.seam.web.SeamFilter


Seam Filter
/*


facelets.DEVELOPMENT true


javax.faces.STATE_SAVING_METHOD client

javax.faces.DEFAULT_SUFFIX .xhtml


Faces Servlet
javax.faces.webapp.FacesServlet
1


Faces Servlet
*.seam

Add libraries

We start with a minimal configured JBoss Seam. As a consequence with this library set you cannot use Seam Email, Seam PDF, …

JBoss 4.2 provides already the jsf-api and jsf-impl libraries. Don’t deploy them. Tomcat or Jetty will need them of course.
Manually

Copy the following libraries to the WEB-INF lib folder. You can find them in the lib folder of your JBoss Seam download.

*

dom4j-1.6.1-jboss.jar
*

commons-beanutils-1.7.0.jar
*

hibernate-validator-3.0.0.ga.jar
*

javassist-3.3.ga.jar
*

jboss-el-2.0.1.GA.jar
*

jboss-seam-2.0.1.GA.jar
*

jboss-seam-ui-2.0.1.GA.jar
*

jsf-api-1.2_04-p02.jar
*

jsf-facelets-1.1.11.jar
*

jsf-impl-1.2_04-p02.jar
*

jta-1.1.jar
*

persistence-api-1.0.jar

Maven

Alternatively, you may use Maven. I like it just for managing my libraries. We need a JBoss repository. My IntelliJ project deployed unintentionally the el-api.jar which was loaded by Maven. This provokes a problem on Tomcat 6 and JBoss 4.2. Just make sure that it is not deployed or exclude it.

Here is the pom.xml file we need:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
de.laliluna
seamWeb

war 1.0-SNAPSHOT
seamWeb
http://maven.apache.org


seamWeb

org.apache.maven.plugins
maven-compiler-plugin

1.5
1.5
org.apache.maven.plugins
maven-idea-plugin

true
true




repository.jboss.org
http://repository.jboss.org/maven2

true




org.jboss.seam
jboss-seam
2.0.1.GA
compile


org.jboss.seam
jboss-seam-ui
2.0.1.GA
compile



javax.transaction
jta
1.1


javax.persistence
persistence-api
1.0


org.hibernate
hibernate-validator
3.0.0.ga



javax.faces
jsf-api
1.2_04-p02


javax.faces
jsf-impl
1.2_04-p02


com.sun.facelets
jsf-facelets
1.1.11



log4j
log4j
1.2.14
compile

Hello world

Create a index.jsp in the root folder. It will redirect us to the hello page.

<% response.sendRedirect("hello.seam"); %>

Create the facelets page hello.xhtml.


PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">


Welcome to your first Seam application


Deploy your application to Tomcat or JBoss and test it. With my project name, I used the following URL

http://localhost:8080/seamWeb/

Writing a Seam component

The next step is to improve the hello page. Our application will deal with hedgehogs and the first thing to be done is to print a list of hedgehogs.

Create a class Hedgehog.

package de.laliluna.seam;

public class Hedgehog {

private String name;

public Hedgehog() {
}

public String getName() {
return name;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(„Hedgehog“);
sb.append(„{name=’“).append(name).append(“’);
sb.append(‘}’);
return sb.toString();
}

public void setName(String name) {
this.name = name;
}

public Hedgehog(String name) {
this.name = name;
}
}

Create a class HedgehogService. It is a Seam component. The @Name annotation marks it as component and defines the name. In addition we can define the context where this component is placed in with the @Scope.

We will see later that we can reference the component using the name. Our service class provides a data model for a JSF data table – hedgehogs.

package de.laliluna.seam;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.ScopeType;
import java.util.List;
import java.util.ArrayList;

@Name(„hedgehogService“)
@Scope(ScopeType.SESSION)
public class HedgehogService {

@DataModel
private List hedgehogs;

public HedgehogService() {
hedgehogs = new ArrayList();
hedgehogs.add(new Hedgehog(„Holger“));
hedgehogs.add(new Hedgehog(„Pete“));
hedgehogs.add(new Hedgehog(„Sebastian“));
}

public String getHello() {
return „Your hedgehogService says hello“;
}
}

Finally we will add some messages to the English resource bundle.

Create a file messages_en.properties in the src folder.

hello=Welcome to your Seam application
hedgehog.name=Name

Improve the hello.xhtml to print the hedgehogs, messages from our resource bundle and the getHello method.


PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">


#{messages.hello}

#{hedgehogService.hello}



#{messages['hedgehog.name']}
#{h.name}



That’s it. Test your application again.

Tips for debugging

When your application starts up it will print all components being created (Seam and your own) and the context they are placed in. Validate that your new component is displayed as well.

Seam has a debug page showing useful information.

Add the library jboss-seam-debug.jar in the WEB-INF folder. Alternatively add the following dependency to your pom.xml:


org.jboss.seam
jboss-seam-debug
2.0.1.GA

In the components.xml activate the debug mode.

Open your browser and point to the following URL to see the debug page.

http://localhost:8080/seamWeb/debug.seam

Add Hibernate

In this step we will leverage our application to load hedgehogs from the database.

In this example, I will use a PostgreSQL database. To change the database, just change the hibernate.cfg.xml settings to your database and replace the library.
Add libraries

In addition we need the following libraries.

*

antlr-2.7.6.jar
*

asm-1.5.3.jar
*

commons-beanutils-1.7.0.jar
*

asm-attrs-1.5.3.jar
*

hibernate-3.2.5.ga.jar
*

commons-collections-2.1.1.jar
*

c3p0-0.9.1.2.jar
*

postgresql-8.2-507.jdbc3.jar
*

backport-util-concurrent-3.0.jar
*

commons-logging-1.0.4.jar
*

hibernate-annotations-3.3.0.ga.jar
*

hibernate-commons-annotations-3.3.0.ga.jar
*

ehcache-1.3.0.jar
*

cglib-2.1_3.jar
*

jsr107cache-1.0.jar

If you prefer Maven, here are the additional dependencies.


org.hibernate
hibernate
3.2.5.ga
compile


c3p0
c3p0
0.9.1.2
compile


net.sf.ehcache
ehcache
1.3.0


org.hibernate
hibernate-annotations
3.3.0.ga
compile


org.hibernate
hibernate-commons-annotations
3.3.0.ga



postgresql
postgresql
8.2-507.jdbc3
compile

Configuration

We need a Hibernate configuration file hibernate.cfg.xml in the root source folder.


"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



jdbc:postgresql://localhost:5432/learninghibernate postgres p org.postgresql.Driver org.hibernate.dialect.PostgreSQLDialect 4 1 org.hibernate.cache.EhCacheProvider org.hibernate.transaction.JDBCTransactionFactory true after_statement update 4

In the components.xml we have to change the transaction handling. The factory is just a work around for a naming problem. We cannot inject a Hibernate session with the name session into the transaction, but

I wanted to use this component name in my application.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

You can leave a response, or trackback from your own site.

2 Responses to “First JBoss Seam tutorial”

  1. chips zynga казва:

    omg quite a few of the observations people submit are a bit spacey, frequently i think about whether they even read the pieces and items before writing a comment or whether or not they mearly skim the title of the post and pen only the first thought that drifts into their minds. anyway, it is really pleasant to browse through clever commentary occasionally rather than the same exact, outdated post vomit that i almost always discover on the web

  2. Private Servers казва:

    Habbo Retros…

    Habbo Retros are slowly attracting more users with each passing day, most people prefer to play Habbo Retros with pets according to a recent google study, further evidence also supports that Habbo Retros have lead to an annual decrease in revenue for s…

Leave a Reply

Задвижван с помощта на WordPress | Compare Cell Phone Plans at iCellPhonePlans.com | Thanks to Cheap Palm Pixi, Bromoney and Wordpress Themes