Overblog
Suivre ce blog Administration + Créer mon blog
28 décembre 2008 7 28 /12 /décembre /2008 21:19

Ce billet est la suite de Utiliser un formulaire Web- Tutorial Seam partie 4

 

Créer une entity Animal
Pour persister les données et représenter les objets métiers, Seam utilise des Entity EJB3. Ces entity font partie de la partie « model » du projet.
Notre entity va s’appeler animal et représente un animal.
Dans eclipse, dans le projet, dans l’onglet Project Explorer, sélectionner src/model, faire un clique droit. Choisir New Class. Dans le champs package écrire com.getj2ee.tuto.seam.helloworld.model et dans le champs Name écrire Animal. Cliquez sur next.
 
Modifier la classe pour arriver à ceci :
package com.getj2ee.tuto.seam.helloworld.model;

import java.io.Serializable;

import static org.jboss.seam.ScopeType.SESSION;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Entity
@Name("animal")
@Table(name = "animals")
public class Animal implements Serializable {
 private static final long serialVersionUID = 1881413500711441952L;
 
 @Id @GeneratedValue
 private Long id;
 
 @NotNull
 private String name;
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
}


La déclaration d’un bean entity se fait grâce à l’annotation @Entity.
@Name est le nom du bean entity dans le contexte du serveur.
@Table (name = « animals ») permet de préciser que cette entity sera stockée dans la table animals.

Chaque entity doit avoir un id unique. La notation
@Id @GeneratedValue
private Long id
Signifie que l’id de cette entity est le champs id de la classe et qu’il est généré automatiquement par la base de données.

Créer une interface Action
Cette interface décrit les actions possibles sur une entity Animal. Créer une classe AnimalsAction qui contiendra ce code :
package com.getj2ee.tuto.seam.helloworld.action;

import java.util.List;
import javax.ejb.Local;
import com.getj2ee.tuto.seam.helloworld.model.Animal;

@Local
public interface AnimalsAction
{
   public String saveUpdate();
   public List<Animal> getAllAnimals();
}

Créer une classe Action
Cette classe action va permettre de faire toutes les actions de persistance concernant l’entité Animal. Cette classe est une implémentation d’AnimalAction.
Dans eclipse, dans le projet, dans l’onglet Project Explorer, sélectionner src/model et faire un clique droit. Choisir New Class. Dans le champs package écrire com.getj2ee.tuto.seam.helloworld.action et dans le champs Name écrire AnimalsAction. Cliquez sur next.
Modifier la classe pour arriver à ceci :
package com.getj2ee.tuto.seam.helloworld.action;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import com.getj2ee.tuto.seam.helloworld.model.Animal;

@Stateless
@Name("animalsAction")
public class AnimalsActionBean implements AnimalsAction {
 @In
 private Animal animal;
 @PersistenceContext
 private EntityManager em;
 @Logger
 private Log log;

 public String saveUpdate() {
  List<Animal> existing = em.createQuery("select a from Animal a where name=#{animal.name}").getResultList();
  if (existing.size() == 0) {
   em.persist(animal);
   log.info("Registered new name #{animal.name}");
   return "/registered.xhtml";
  } else {
   FacesMessages.instance().add("Animal #{animal.name} already exists");
   return null;
  }
 }

 public Animal getAnimal() {
  return animal;
 }

 public void setAnimal(Animal animal) {
  this.animal = animal;
 }

 public List<Animal> getAllAnimals() {
  List<Animal> animals = em.createQuery("select a from Animal a").getResultList();
  return animals;
 }
}

AnimalActionBean est un bean stateless, c a d sans état. Il est recréé à chaque utilisation.
La notation @In private Animal animal ; précise qu’un entity animal est injecter dans animalActions
@PersistanceContext private EntityManager em ;  désigne l’entityManager qui sera utilisé pour la persistance des entity .

La méthode saveUpdate permet de sauvegarder l’animal. Ceci se fait via l’instruction : em.persist(animal).

La méthode getAllAnimals() retourne tous les animal de la base de données. L’entity manager permet de faire des requêtes : em.createQuery("select a from Animal a").getResultList()


Injecter actionAnimals dans crudAnimals
Modifier le code de crudsAnimals pour obtenir ceci :
package com.getj2ee.tuto.seam.helloworld;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import com.getj2ee.tuto.seam.helloworld.action.AnimalsAction;
import com.getj2ee.tuto.seam.helloworld.model.Animal;

@Stateless
@Name("crudAnimals")
public class CrudAnimalsBean implements CrudAnimals {

 @Logger
 private Log log;

 @In()
 FacesMessages facesMessages;

 @In(create = true)
 AnimalsAction animalsAction;

 @In(create = true)
 private Animal animal;
 private String name = "No name";

 public void crudAnimals() {
  // implement your business logic here
  log.info("crudAnimals.crudAnimals() action called");
  animal.setName(this.name);
  animalsAction.saveUpdate();
  facesMessages.add("crudAnimals");
 }

 @Override
 public String getName() {
  return name;
 }

 @Override
 public void setName(String name) {
  log.info("change name in my bean :" + name);
  this.name = name;
 }

 public AnimalsAction getAnimalsAction() {
  return animalsAction;
 }

 public void setAnimalsAction(AnimalsAction animalsAction) {
  this.animalsAction = animalsAction;
 }

 // add additional action methods
 public Animal getAnimal() {
  return animal;
 }

 public void setAnimal(Animal animal) {
  this.animal = animal;
 }

 @Override
 public List<Animal> getAllAnimals() {
  return animalsAction.getAllAnimals();
 }

}


L’injection de animal action se fait via cette annotation: @In(create = true) private AnimalsAction animalAction . La notation create = true précise que le bean animalAction sera créé si aucun bean n’est disponible pour l’injection

L’entity animal est injecter via @In(create = true) .

La ligne animalsAction.saveUpdate(); sauvegarde l’entity animal.


Afficher tous les « animals » dans un datatable
Modifions la page crudAnimals.xhtml pour faire apparaitre la liste de tous les animaux qui sont dans la base de données.

Juste après la dernière balise </h :form>  ajouter ceci :

<h:dataTable value="#{crudAnimals.allAnimals}" var="oneAnimal"

                        rendered="true">

                        <h:column>

                             <f:facet name="header">

                                   <h:outputText value="Id" />

                             </f:facet>

                             <h:outputText value="#{oneAnimal.id}"></h:outputText>

                        </h:column>

                        <h:column>

                             <f:facet name="header">

                                   <h:outputText value="Name" />

                             </f:facet>

                             <h:outputText value="#{oneAnimal.name}"></h:outputText>

                        </h:column>

 </h:dataTable>

Redéployer et tester
Dans le script Ant, déclencher la target deploy. Aller à l’url
http://localhost:8080/helloworld/crudAnimals.seam , il est maintenant possible d’ajouter autant d’animaux que l’on veut.

 

 

Références :
Tuto seam Partie 1 : Installer SEAM et JBoss
Tuto seam Partie 2 : Creer un projet SEAM
Tuto seam Partie 3 : Créer une première page web - Tutorial Seam partie 3
Tuto seam Partie 4 : Utiliser un formulaire Web- Tutorial Seam partie 4
EL JSF et JSP : jsf and jsp expression language
La doc de seam : 
http://www.seamframework.org/Documentation
Partager cet article
Repost0
28 septembre 2008 7 28 /09 /septembre /2008 22:51
Partager cet article
Repost0
14 septembre 2008 7 14 /09 /septembre /2008 15:57

Ce billet est la suite de Créer une première page web - Tutorial Seam partie 3

Ajoutons dans notre formulaire un champs name pour notre animal.

Le formulaire se trouve dans le projet Eclipse dans view/crudAnimals.xhtml .
Ouvrons ce fichier, o
n remarque en haut de la page cette déclaration :

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"

                xmlns:s="http://jboss.com/products/seam/taglib"

                xmlns:ui="http://java.sun.com/jsf/facelets"

                xmlns:f="http://java.sun.com/jsf/core"

                xmlns:h="http://java.sun.com/jsf/html"

                xmlns:rich="http://richfaces.org/rich"

                xmlns:a="http://richfaces.org/a4j"

                template="layout/template.xhtml">

 

Cette page est donc en XML et est une page JSF.

 

Voici le formulaire :

<h:form id="crudAnimalsForm">

<h:commandButton id="crudAnimals" value="crudAnimals!"

action="#{crudAnimals.crudAnimals}" style=" width : 80px;" />

</h:form>

  

Ajoutons le champs name de notre animal :

<h:form id="crudAnimalsForm">

<h:inputText value="#{crudAnimals.name}">Name :</h:inputText>

     <br />

<h:commandButton id="crudAnimals" value="crudAnimals!"

action="#{crudAnimals.crudAnimals}" style=" width : 80px;" />

</h:form>

 

La notation  value="#{crudAnimals.name}" est de l’Expression Language JSF.

Cela signifie que la value du paramètre du formulaire est liée à la valeur de l’attribut name du bean crudAnimals.

 

Ainsi quand on clique sur le bouton du formulaire la valeur du nom va être passée directement au bean crudAnimals.

 

Ajoutons l’attribut name au bean crudAnimals.  

 

Dans la classe CrudsAnimalsBean.java ajoutons :

private String name="No name";

   

public String getName() {

            return name;

}

 

public void setName(String name) {

             log.info("change name in my bean :"+name);

            this.name=name;

      }

Il faut aussi ajouter les méthodes setName et getName dans l‘interface CrudAnimals.java pour que ces méthodes soit accessible dans la page JSF.

Dans l’interface CrudAnimals ajoutons :

public String getName();

public void setName(String name);

 

Les modifications sont terminées. Une fois le serveur web lancé, lorsque l’on change le nom de l’Animal on voit apparaitre dans la console le message suivant :

09:52:44,533 INFO  [CrudAnimalsBean] change name in my bean :Jumbo

La valeur du nom est donc bien passée du formulaire au bean du serveur.

Références :
Tuto seam Partie 1 : Installer SEAM et JBoss
Tuto seam Partie 2 : Creer un projet SEAM
Tuto seam Partie 3 : Créer une première page web - Tutorial Seam partie 3
EL JSF et JSP : jsf and jsp expression language
La doc de seam : 
http://www.seamframework.org/Documentation

Partager cet article
Repost0
29 juin 2008 7 29 /06 /juin /2008 14:41


Les nouvelles spécifications pour EJB 3.1 et JAVA EE 6 sont en cours de finalisation.

Cet article de Celinio Fernandes nous les présentes en français : EJB 3.1 - les nouveautés avec Java EE 6 

A première vue, les EJB Lite devraient beaucoup simplifier la vie des développeurs. L' EJB Timer aussi.

Partager cet article
Repost0
4 juin 2008 3 04 /06 /juin /2008 13:48

Ce billet est la suite de l'article : Créer un projet Seam - Tutorial Seam partie 2

Nous allons créer une première page web que nous appellerons crudAnimals.

Pourquoi ce nom bizarre ? L’objectif ce tutoriel est de montrer comment faire uncrud sur une entité Animal. CRUD signifie Create Read Update Delete, c a d que nous allons faire une seul page qui permet de Créer Lire Modifier Mettre à jour Supprimer un animal dans notre base de donnée.

Pour créer une première page, le plus simple est de demander à SEAM de le faire.

Aller en ligne de commande dans le projet helloworld SEAM et tapez :

C:/devjava/workspace/helloworld> C:/devjava/workspace/jboss-seam-2.0.1.GA/seam new-action

Vous pouvez tapez directement seam new-action si vous avez mis le répertoire de jboss-seam-2.0.1.GA dans le path de windows.

Ceci va créer la page SEAM et son action web :
C:/devjava/workspace/helloworld>..jboss-seam-2.0.1.GA/seam new-action
Unable to locate tools.jar. Expected to find it in C:/Program Files/Java/jre1.6.0_04/lib/tools.jar
Buildfile C:/devjava/workspace jboss-seam-2.0.1.GA/seam-gen/build.xml
init:
init-properties:
[echo] C:/Program Files/jboss-4.2.2.GA
validate-workspace:
validate-project:
action-input:
[input] Enter the Seam component name
crudAnimals

[input] Enter the local interface name [CrudAnimals] [CrudAnimals]
[input] Enter the bean class name [CrudAnimalsBean] [CrudAnimalsBean]
[input] Enter the action method name [crudAnimals] [crudAnimals]
[input] Enter the page name [crudAnimals] [crudAnimals]
setup-filters:
new-action-ear:
[echo] Creating a new stateless session bean component with an action method
[copy] Copying 1 file to C:devjavaworkspace /helloworld/src
/action/com/getj2ee/tuto/seam/helloworld
[copy] Copying 1 file to C:devjavaworkspace /helloworld/src
action/com/getj2ee/tuto/seam/helloworld
[copy] Copying 1 file to C:devjavaworkspace /helloworld/src
test/com/getj2ee/tuto/seam/helloworld/test
[copy] Copying 1 file to C:devjavaworkspace /helloworld/src
test/com/getj2ee/tuto/seam/helloworld/test
[copy] Copying 1 file to C:/devjava/workspace /helloworld/view
[echo] Type 'seam restart' and go to http://localhost:8080/helloworld/crudAnimals.seam

new-action-war:
new-action:

BUILD SUCCESSFUL
Total time: 45 seconds
C:devjavaworksaqcehelloworld>

Retourner dans Eclipse. Rafraichir le projet. Et relancer JBoss.

Aller à l’adresse http://localhost:8080/helloworld/crudAnimals.seam pour voir apparaitre la nouvelle page


Références :
Tuto seam Partie 1 : Installer SEAM et JBoss
Tuto seam Partie 2 : Creer un projet SEAM
La doc de seam : 
http://www.seamframework.org/Documentation
Installer Ant :  http://java.developpez.com/articles/ant/partie2/

Partager cet article
Repost0
21 mai 2008 3 21 /05 /mai /2008 13:49

Voici la suite du Tutorial sur Seam. Après avoir vue l'installation de seam dans Installer Seam et JBoss, voyons comment créer un projet seam dans eclipse.

Créer un projet Seam

 

 

1 - Setup de Seam

Aller dans REP_WORKSPACE/-seam-2.0.1.GA  avec l’invite de commande Windows

Tapez seam setup puis enter

 

Voici ce que j’obtiens sur mon pc :

C:devjavaworkspacejboss-seam-2.0.1.GA>seam setup

Buildfile: C:devjavaworkspacejboss-seam-2.0.1.GAseam-genbuil

d.xml

init:

setup:

[echo] Welcome to seam-gen :-)

[input] Enter your Java project workspace (the directory that contains your

Seam projects) [C:/Projects] [C:/Projects]

E:/DavidDate/DevNuts/ProjetsEclipse

[input] Enter your JBoss home directory [C:/Program Files/jboss-4.2.2.GA] [C

:/Program Files/jboss-4.2.2.GA]

[input] Enter the project name [myproject] [myproject]

helloworld

[echo] Accepted project name as: helloworld

[input] Do you want to use ICEFaces instead of RichFaces [n] (y, [n], )

n

[input] skipping input as property icefaces.home.new has already been set.

[input] Select a RichFaces skin [blueSky] ([blueSky], classic, ruby, wine, d

eepMarine, emeraldTown, sakura, DEFAULT)

[input] Is this project deployed as an EAR (with EJB components) or a WAR (w

ith no EJB support) [ear] ([ear], war, )

[input] Enter the Java package name for your session beans [com.mydomain.hel

loworld] [com.mydomain.helloworld]

com.getj2ee.tuto.seam.helloworld

[input] Enter the Java package name for your entity beans [com.getj2ee.tuto.

seam.helloworld] [com.getj2ee.tuto.seam.helloworld]

 

[input] Enter the Java package name for your test cases [com.getj2ee.tuto.se

am.helloworld.test] [com.getj2ee.tuto.seam.helloworld.test]

[input] What kind of database are you using? [hsql] ([hsql], mysql, oracle,

postgres, mssql, db2, sybase, enterprisedb, h2)

[input] Enter the Hibernate dialect for your database [org.hibernate.dialect

.HSQLDialect] [org.hibernate.dialect.HSQLDialect]

[input] Enter the filesystem path to the JDBC driver jar [lib/hsqldb.jar] [l

ib/hsqldb.jar]

[input] Enter JDBC driver class for your database [org.hsqldb.jdbcDriver] [o

rg.hsqldb.jdbcDriver]

[input] Enter the JDBC URL for your database [jdbc:hsqldb:.] [jdbc:hsqldb:.]

[input] Enter database username [sa] [sa]

[input] Enter database password [] []

[input] Enter the database schema name (it is OK to leave this blank) [] []

[input] Enter the database catalog name (it is OK to leave this blank) [] []

[input] Are you working with tables that already exist in the database? [n]

(y, [n], )

[input] Do you want to drop and recreate the database tables and data in imp

ort.sql each time you deploy? [n] (y, [n], )

[propertyfile] Creating new property file: C:devjavaworkspacej

boss-seam-2.0.1.GAseam-genbuild.properties

[echo] Installing JDBC driver jar to JBoss server

[copy] Copying 1 file to C:Program Filesjboss-4.2.2.GAserverdefaultlib

[echo] Type 'seam create-project' to create the new project

BUILD SUCCESSFUL

Total time: 6 minutes 27 seconds

C:devjavaworkspacejboss-seam-2.0.1.GA>

 

 

Seam est correctement installé.

 

2 - Création du projet dans Eclipse.

Pour il faut maintenant taper seam new-project

Vous allez obtenir ceci :

 

C:devjavaworkspacejboss-seam-2.0.1.GA>seam new-project

Unable to locate tools.jar. Expected to find it in C:Program FilesJavajre1.6.

0_04libtools.jar

Buildfile: C:devjavaworkspacejboss-seam-2.0.1.GAseam-genbuil

d.xml

 

init:

 

init-properties:

[echo] C:/Program Files/jboss-4.2.2.GA

 

validate-workspace:

 

validate-project:

 

icefaces-staging-copy:

 

initcopy:

 

initpoms:

[echo] Setting up dependencies

[mkdir] Created dir: C:devjavaworkspacejboss-seam-2.0.1.GA

classespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[artifact:install] [INFO] Installing C:devjavaworkspacejboss-s

eam-2.0.1.GAclassespomsroot.pom to C:Usersdavidgimelle.m2repositoryorgj

bossseamroot2.0.1.GAroot-2.0.1.GA.pom

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[artifact:install] [INFO] Installing C:devjavaworkspacejboss-s

eam-2.0.1.GAclassespomsparent.pom to C:Usersdavidgimelle.m2repositoryorg

jbossseamparent2.0.1.GAparent-2.0.1.GA.pom

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

[copy] Copying 1 file to C:devjavaworkspacejboss-seam-2.0

.1.GAclassespoms

 

copyseam:

 

copyseamdependencies:

 

copyjbossembedded:

 

copy-icefaces-home:

 

copy-icefaces-maven:

 

copy-lib:

[echo] Copying Seam and depdencies to the E:/DavidDate/DevNuts/ProjetsEclip

se/helloworld/lib directory...

[copy] Copying 97 files to C:devjavaworkspacehelloworldl

ib

[copy] Copied 4 empty directories to 1 empty directory under E:DavidDateD

evNutsProjetsEclipsehelloworldlib

[echo] Copying JBoss Embedded configuration to the E:/DavidDate/DevNuts/Pro

jetsEclipse/helloworld/bootstrap directory...

[copy] Copying 35 files to C:devjavaworkspacehelloworldb

ootstrap

[copy] Copied 15 empty directories to 2 empty directories under E:DavidDat

eDevNutsProjetsEclipsehelloworldbootstrap

 

file-copy-war:

 

file-copy-ear:

[echo] Copying resources needed for EAR deployment to the E:/DavidDate/DevN

uts/ProjetsEclipse/helloworld/resources directory...

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ourcesWEB-INF

[copy] Copying 1 file to C:devjavaworkspacehelloworld

[copy] Copying 6 files to C:devjavaworkspacehelloworldre

sources

 

setup-filters:

 

file-copy:

[copy] Copying 11 files to C:devjavaworkspacehelloworldr

esources

[copy] Copying 1 file to C:devjavaworkspacehelloworld

[copy] Copying 1 file to C:devjavaworkspacehelloworld

[copy] Copying 1 file to C:devjavaworkspacehelloworld

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 1 file to C:devjavaworkspacehelloworld.se

ttings

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 1 file to C:devjavaworkspacehelloworldres

ources

[copy] Copying 10 files to C:devjavaworkspacehelloworldv

iew

[copy] Copying 2 files to C:devjavaworkspacehelloworldvi

ew

[copy] Copying 1 file to C:devjavaworkspacehelloworldsrc

actioncomgetj2eetutoseamhelloworld

[copy] Copying 6 files to C:devjavaworkspacehelloworld

[mkdir] Created dir: C:devjavaworkspacehelloworldsrcmode

l

[mkdir] Created dir: C:devjavaworkspacehelloworldsrctest

 

[copy] Copying 1 file to C:devjavaworkspacehelloworldsrc

test

[mkdir] Created dir: C:devjavaworkspacehelloworldnbprojec

t

[copy] Copying 3 files to C:devjavaworkspacehelloworldnb

project

 

create-project:

[echo] A new Seam project named 'helloworld' was created in the E:/DavidDat

e/DevNuts/ProjetsEclipse directory

[echo] Type 'seam explode' and go to http://localhost:8080/helloworld

[echo] Eclipse Users: Add the project into Eclipse using File > New > Proje

ct and select General > Project (not Java Project)

[echo] NetBeans Users: Open the project in NetBeans

 

new-project:

 

BUILD SUCCESSFUL

Total time: 40 seconds

 

C:devjavaworkspacejboss-seam-2.0.1.GA>

 

 

3 - Importer le projet dans Eclipse

Ouvrir Eclipse

Dans le file choisir

            New/project/general/project

Next

Taper helloworld en nom de projet

Laisser la compilation automatique d’Eclipse se faire. Si vous n’avez de compilation automatique aller dans Project/buildAll

 

4 - Déployer dans JBoss

Ouvrer la vue Ant de Eclipse

Dans le projet sélectionner le fichier build.xml et le glisser dans la vue ant

            Dans la vue ant, ouvrir le nœud helloworld et double cliquer sur la target Deploy

(Vous allez peut-être avoir un message de votre firewall, il faut autoriser l’accès à JBoss)

 

5 - Lancer jboss

Aller dans la vue JBoss et lancer JBoss

(Vous allez peut-être avoir un message de votre firewall, il faut autoriser l’accès à JBoss et java)

 

6 - Voir la page par défaut

Aller votre navigateur préféré a l’adresse : http://localhost:8080/helloworld/ Vous devez y voir la page par défaut de l’application

La suite de ce tutorial est ici : Créer une première page web - Tutorial Seam partie 3

Références :
Tutorial seam Partie 1 : Installer SEAM et JBoss
Tutorial seam Partie 3 : Créer une première page web - Tutorial Seam partie 3
La doc de seam : 
http://www.seamframework.org/Documentation
Installer Ant :  http://java.developpez.com/articles/ant/partie2/

Partager cet article
Repost0
24 avril 2008 4 24 /04 /avril /2008 01:51

Il arrive que le serveur de test de www.openldap.com ne soit pas disponible. C'est bien dommage car c'est celui ci que j'ai utilisé dans le tutorial que j'ai publié sur Srping Ldap sur ce blog :Tutorial Spring Ldap et sur developpez.com Tuto Spring Ldap

Dans ce cas quand on execute TestLdap.java on ce genre de message dans la console :

23 avr. 2008 20:00:18 org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet
INFO: Property 'userDn' not set - anonymous context will be used for read-write operations
Exception in thread "main"
org.springframework.ldap.CommunicationException: www.openldap.com:389; nested exception is javax.naming.CommunicationException: www.openldap.com:389 [Root exception is java.net.ConnectException: Connection refused: connect]Caused by: javax.naming.CommunicationException: www.openldap.com:389 [Root exception is java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.ldap.Connection.<init>(
Connection.java:207
)
at com.sun.jndi.ldap.LdapClient.<init>(
LdapClient.java:118
)
at com.sun.jndi.ldap.LdapClientFactory.createPooledConnection(
LdapClientFactory.java:46
)
at com.sun.jndi.ldap.pool.Connections.<init>(
Connections.java:97
)
at com.sun.jndi.ldap.pool.Pool.getPooledConnection(
Pool.java:114
)
at com.sun.jndi.ldap.LdapPoolManager.getLdapClient(
LdapPoolManager.java:310
)
at com.sun.jndi.ldap.LdapClient.getInstance(
LdapClient.java:1572
)
at com.sun.jndi.ldap.LdapCtx.connect(
LdapCtx.java:2616
)
at com.sun.jndi.ldap.LdapCtx.<init>(
LdapCtx.java:287
)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(
LdapCtxFactory.java:175
)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(
LdapCtxFactory.java:193
)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(
LdapCtxFactory.java:136
)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(
LdapCtxFactory.java:66
)
at javax.naming.spi.NamingManager.getInitialContext(
NamingManager.java:667
)
at javax.naming.InitialContext.getDefaultInitCtx(
InitialContext.java:288
)
at javax.naming.InitialContext.init(
InitialContext.java:223
)
at javax.naming.ldap.InitialLdapContext.<init>(
InitialLdapContext.java:134
)
at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(
LdapContextSource.java:43
)
at org.springframework.ldap.core.support.AbstractContextSource.createContext(
AbstractContextSource.java:223
)
at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(
AbstractContextSource.java:107
)
at org.springframework.ldap.core.LdapTemplate.executeReadOnly(
LdapTemplate.java:770
)
at org.springframework.ldap.core.LdapTemplate.lookup(
LdapTemplate.java:821
)
at com.neoneto.demo.springLdap1_2.PersonDao.findByPrimaryKey(
PersonDao.java:31
)
at com.neoneto.demo.springLdap1_2.TestLdap.main(
TestLdap.java:28
)Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(
Native Method
)
at java.net.PlainSocketImpl.doConnect(
PlainSocketImpl.java:333
)
at java.net.PlainSocketImpl.connectToAddress(
PlainSocketImpl.java:195
)
at java.net.PlainSocketImpl.connect(
PlainSocketImpl.java:182
)
at java.net.SocksSocketImpl.connect(
SocksSocketImpl.java:366
)
at java.net.Socket.connect(
Socket.java:519
)
at java.net.Socket.connect(
Socket.java:469
)
at java.net.Socket.<init>(
Socket.java:366
)
at java.net.Socket.<init>(
Socket.java:180
)
at com.sun.jndi.ldap.Connection.createSocket(
Connection.java:349
)
at com.sun.jndi.ldap.Connection.<init>(
Connection.java:184
)

- La solution numero 1 à ce type de probleme conciste à trouver un serveur ldap public disponible et à modifier les sources pour ca fonctionne. Pour trouver un serveur public qui fonctionne il suffit d'utiliser Ldap Browser et la liste des serveurs publiques fournit dans les liens du tutorial.

Faire tous ca, ca prend du temps et c'est pas toujours facile quand on debute avec Ldap. 
C'est pourquoi il y a la solution numero 2.

- Solution numero 2 : Utiliser ces sources java qui permettent de se connecter sur un autre serveur ldap public, celui de Baylor University.

 

package com.neoneto.demo.springLdap1_2.rescue;
public class Person2 {

 private String uid;
 private String firstName;
 private String lastName;
 private String email;
 
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getUid() {
  return uid;
 }
 public void setUid(String uid) {
  this.uid = uid;
 }
 public String toString(){
  return uid+" - "+firstName+" "+lastName;
 }
}

 


package com.neoneto.demo.springLdap1_2.rescue;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;

public class PersonDao2 {

 private LdapTemplate ldapTemplate;

 private static class PersonAttributMapper implements AttributesMapper {

  public Person2 mapFromAttributes(Attributes attrs)
    throws javax.naming.NamingException {
   Person2 p = new Person2();
   p.setFirstName(attrs.get("givenName").get().toString());
   p.setLastName(attrs.get("sn").get().toString());
   p.setUid(attrs.get("cn").get().toString());
   p.setEmail(attrs.get("mail").get().toString());
   return p;
  }
 }

 public Person2 findByPrimaryKey(String cn) {
  Name dn = buildDn(cn);
  return (Person2) ldapTemplate.lookup(dn, new PersonAttributMapper());
 }

 private Name buildDn(String cn) {
  DistinguishedName dn = new DistinguishedName();
  dn.add("ou", "Faculty and Staff");
  dn.add("ou", "Computer Science - Eng/Comp Sci");
  dn.add("cn", cn);
  return dn;
 }

 public void setLdapTemplate(LdapTemplate ldapTemplate) {
  this.ldapTemplate = ldapTemplate;
 }

}



package com.neoneto.demo.springLdap1_2.rescue;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource;

public class LdapContextSourceFactory2 {

 public static ContextSource getLdapContextSource() throws Exception {
  LdapContextSource ldapContextSource = new LdapContextSource();
  ldapContextSource.setUrl("ldap://ldap.baylor.edu:389");
  ldapContextSource.setBase("ou=People,o=Baylor University,"+
"c=US");
  ldapContextSource.afterPropertiesSet();
  return ldapContextSource;
 }
}



package com.neoneto.demo.springLdap1_2.rescue;
import java.util.List;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;

public class TestLdap2 {

 /** Retrieve a Person from ldap server and display it in Standard Out - Use Baylor University server*/
 public static void main(String[] args) {

  // 1 Retrieve a LdapContextSource
  ContextSource ldapContextSource = null;
  try {
   ldapContextSource = LdapContextSourceFactory2.getLdapContextSource();
  } catch (Exception e) {
   System.out.println("Impossible to get a"
+" LdapContextSource.");
   e.printStackTrace();
  }

  // 2 Instanciate a LdapTemplate
  LdapTemplate ldapTemplate = new LdapTemplate();
  ldapTemplate.setContextSource(ldapContextSource);

  // 3 instanciate a PersonDao
  PersonDao2 dao = new PersonDao2();
  dao.setLdapTemplate(ldapTemplate);

  // 4 retrieve a Person and display it
  Person2 person = dao.findByPrimaryKey("Patrick Hynan");
  System.out.println("Uid: " + person.getUid());
  System.out.println("FirstName: " + person.getFirstName());
  System.out.println("LastName: " + person.getLastName());
  System.out.println("Email: " + person.getEmail() + "n");

 }
}

 

 

 

Partager cet article
Repost0
22 avril 2008 2 22 /04 /avril /2008 14:03

Ce billet est le premiere d'une serie sur SEAM. Vous trouverez à la fin de l'article un lien vers la partie suivante.

Voici comment comment installer l'environement de developpement de SEAM. C'est à dire Seam, Jboss et Seamgen. Seamgen est l'outil de Seam pour generer des projets.


Prérequis :
Vous devez avoir eclipse 3.2 (ou superieur) et le JDK 1.5 (ou superieur). Il est tres important d'avoir un JDK et non un JRE.

Vous vous avez des difficultés sur cette partie pouvez consultez ces liens :
JDK,JRE :

http://java.developpez.com/faq/java/?page=langage#LANGAGE_jre_jdk

Eclipse :
http://java.developpez.com/faq/eclipse/?page=plateform#installerEclipse

Je n'en dirai pas plus sur cette partie mille fois documentées sur le web.


Convention :
REP_ECLIPSE represente le repertoire est installé eclispe. Ex: C:devjavaeclipse
REP_WORKSPACE represente le workspace de eclise : Ex: C:devjavaworkspace
Ce tutorial concerne une installation sous windows vista.


Installation et téléchargement

1 - Installer ant 1.6
Il faut installer ant 1.6 car avec la version courante (1.7) Seam ne fonctionne pas.
Telecharger ant 1.6,5 ici :
 
Decompresser l'archive dans un repertoire temporaire.
Copier le repertoire apache-ant-1.6.5 dans C:Program Files.
Créer la variable d'environement ANT_HOME et modifier PATH:
Aller dans Paneau de configuration / system .
Cliquer sur parametres avancés.
Cliquer sur variable d'environement.
Modifier PATH pour lui ajouter : C:Program Filesapache-ant-1.6.5bin.
Créer une variable systeme ANT_HOME avec la valeur C:Program Filesapache-ant-1.6.5


2 - Installer jboss 4.2.2.GA
Télécharger jboss ici : http://www.jboss.org/jbossas/
Decompresser l'archive dans un repertoire temporaire.
Copier le repertoire jboss-4.2.2.GA dans c:Program File 


3 - Installer le plugin TestNG
TestNG sert a faire des test unitaires.
Aller sur http://testng.org/doc/download.html
et télécharger le plugin testng pour eclipse.
Decompresser l'archive dans un repertoire temporaire.
Copier le jar org.testng.eclipse_5.8.0.1.jar dans REP_ECLIPSE/plugins.
Redemarrer eclipse.
Verifiez que vous avez bien TestNG dans le menu d'eclipse : Windows/show view/other/java 


4 - Installer le plugin jboss tools.
Télécharger eclipse jboss tools 2.0.1 ALL :http://www.jboss.org/tools/download/index.html 
Decompresser dans un repertoire temporaire.
Copier le contenue du repertoire plugins dans REP_ECLIPSE/plugins
Redemarrer eclipse  


5 - Configurer eclipse pour jboss :
Ouvrir le paneau jboss view : aller dans Windows/show view/other/server/jboss view
Faire un clique droit dans le paneau
Choisir new server
Choisir jboss AS 4.2, cliquer sur NEXT
Choisir le repertoire C:Program Filesjboss-4.2.2.GA .
Verifier que le JRE est bien un jdk 1.5 ou 1.6.
Si ce n'est pas un jdk, il faut ajouter un jdk en cliquant sur le bouton JRE
Choisir la configuration par defaut.
Cliquer sur Next,cliquer sur Finish 


6 - Installer seam
Télécharger seam 2,0,1 GA ici :
Deziper dans un repertoire temporaire
Copier le repertoire jboss-seam-2.0.1.GA dans REP_WORKSPACE

Vous trouverez la suite de ce tutorial ici :Créer un projet Seam - Tutorial Seam partie 2


Références :

Tutorial seam partie 2 : Créer un projet Seam - Tuto Seam partie 2
Tutoriql seam partie 3 : Créer une première page web - Tutorial Seam partie 3
La doc de seam : 
http://www.seamframework.org/Documentation
Installer Ant :  http://java.developpez.com/articles/ant/partie2/

Partager cet article
Repost0
8 avril 2008 2 08 /04 /avril /2008 13:33
Partager cet article
Repost0
7 avril 2008 1 07 /04 /avril /2008 13:17

Aujourd'hui Lundi 7 Avril 2007, c'est le lancement de mon espace perso sur www.developpez.com :
http://david-gimelle.developpez.com.  Cette espace permettra de diffuser plus largement mes tutoriels Java.

Cette premiere publication est un tutorial sur J2EE et Spring Ldap :
http://david-gimelle.developpez.com/tutoriels/java/j2ee/springldap .
Cette article une reprise au format pdf du billet de Fevrier 2007 sur ce blog.

Partager cet article
Repost0

Summary

  • : GetJ2ee -Java development
  • : Articles about Java and EE
  • Contact

Profil

  • David Gimelle
  • Java Developer for more 10 years. I worked in France, Canada and Switzerland. I am contractor in London since 2010.
  • Java Developer for more 10 years. I worked in France, Canada and Switzerland. I am contractor in London since 2010.

Contact

You can contact me here

or by Twitter

Search

Archives