IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Web Java Discussion :

Integration de struts2 et spring2 [Struts]


Sujet :

Spring Web Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mars 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 93
    Par défaut Integration de struts2 et spring2
    Bonjour tout le monde. J'essaye en ce moment d'intégrer spring et struts dans le même projet mais j'y arrive pas,même si j'ai identifié d'où venait le problème je n'arrive pas à le résoudre. L'erreur est que je ne sais pas comment indiquer a struts que le bean action qu'il doit utiliser est celui créer par spring et non un bean qu'il doit lui créer. Voici mes fichiers:
    web.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
      <display-name>struts2spring2</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    	<filter>
    	<filter-name>struts2</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    	</filter>
     
    	<filter-mapping>
    	<filter-name>struts2</filter-name>
    	<url-pattern>/*</url-pattern>
    	</filter-mapping>
     
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
     
    </web-app>
    struts.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
     
    <struts>
     
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
     
     
     
    <package name="produit" extends="struts-default">
    	<action name="showFormProduit">
    		<result>/WEB-INF/pages/creer.jsp</result>
    	</action>
     
    	<action name="creation" class="actions.CreateProduit">
    		<result name="input">/WEB-INF/pages/creer.jsp</result>
    		<result name="error">/WEB-INF/pages/creer.jsp</result>
    		<result>/WEB-INF/pages/ajoutProduitTermine.jsp</result>		
    	</action>
     
    	<action name="lstProduit" class="actions.LstProduit">
    		<result>/WEB-INF/pages/listproduit.jsp</result>
    	</action>
    </package>
     
    <!-- Add packages here -->
     
    </struts>
    applicationcontext.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        <bean id="action" class="actions.LstProduit" >
        	<property name="produitManager" ref="serv"/>
        </bean>
        <bean id="serv" class="service.ImplProduit">
        	<property name="produitDAO" ref="Dao"/>
        </bean>
        <bean id="Dao" class="repositry.ImplProduitDAO">
        </bean>
    </beans>
    et l'action:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    package actions;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.validator.annotations.Validations;
     
    import model.Produit;
     
    import service.ImplProduit;
    import service.ProduitManager;
     
     
     
    /**
     * <p> Validate a user login. </p>
     */
    @Validations
    public  class LstProduit  extends ActionSupport {
    	ProduitManager produitManager;
     
    	private List<Produit> lst=new ArrayList<Produit>();
     
        public String execute() throws Exception {
        	lst=produitManager.getLstProduit();
        	return SUCCESS;
        }
     
    	public List<Produit> getLst() {
    		return lst;
    	}
     
    	public ProduitManager getProduitManager() {
    		return produitManager;
    	}
     
    	public void setProduitManager(ProduitManager produitManager) {
    		this.produitManager = produitManager;
    	}
     
     
     
     
     
     
    }
    le reste c'est du classique. Je dois d'une certaine manière indiquer a struts d'utiloiser le bean nommé action de l'applicationcontext au lieu de créer un nouveau bean.
    Merci d'avance pour vos réponses.

  2. #2
    Membre confirmé
    Inscrit en
    Mars 2009
    Messages
    93
    Détails du profil
    Informations forums :
    Inscription : Mars 2009
    Messages : 93
    Par défaut
    J'ai trouvé la solution,pour ceux que sa intéresse voici ce que j'ai fait tout d'abord il faut ajouter dans le fichier struts.xml la constante suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    et d'ajouter le jar suivant struts2-spring-plugin-2.1.8.jar. L'idée est que struts utilise les bean crée par spring dans l'applicationContext.xml au lieu des actions classique voici un exemple:
    le fichier applicationContext.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        <bean id="actionPers" class="actions.LstProduit" scope="prototype" >
        	<property name="produitManager" ref="serv"/>
        </bean>
        <bean id="serv" class="service.ImplProduit">
        	<property name="produitDAO" ref="Dao"/>
        </bean>
        <bean id="Dao" class="repositry.ImplProduitDAO">
        </bean>
    </beans>
    le fichier struts.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <package name="produit" extends="struts-default">
    	<action name="showFormProduit">
    		<result>/WEB-INF/pages/creer.jsp</result>
    	</action>
     
    	<action name="lstProduit" class="actionPers">
    		<result>/WEB-INF/pages/listproduit.jsp</result>
    	</action>
    </package>
    vous remarquez qu'au lieu d'utiliser la classe de l'action directement dans l'action lstProduit j'utilise le bean actionPers définie par spring.

  3. #3
    Membre chevronné
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2003
    Messages : 476
    Par défaut Plus simple si tu aimes l'autowire
    Salut,

    Je connaissais pas cette façon de faire pour référencer dans Struts un bean Action déclaré dans Spring.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    <action name="lstProduit" class="actionPers">
    </code>
    L'idée est pas bête, par contre pas très intuitive :
    L'attribut class n'est pas une classe mais une instance...

    Perso, je déclare uniquement mes actions côté Struts, puisque le framework fait qu'on n'est pas obligé de déclarer dans un fichiers de conf Spring, les actions nécessitant une initialisation de leurs propriétés.
    En effet le plugin Spring de Struts 2 est configuré de sorte que Spring déclare par défaut en autowire toutes les actions (byName par défaut) .

    Ca fait moins redondant en évitant la multiplication des déclarations qui ne facilitent pas la lecture de la configuration des actions et les fichier xml en général.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Framework] Integration de Struts 2 avec Spring (Struts2-spring-plugin )
    Par medtouffahi dans le forum Spring
    Réponses: 0
    Dernier message: 01/03/2015, 03h50
  2. Integration Birt dans Struts2
    Par IngInf dans le forum Struts 1
    Réponses: 0
    Dernier message: 21/05/2012, 16h19
  3. integration de repertoire dans ma webapp
    Par thomy dans le forum JBuilder
    Réponses: 2
    Dernier message: 04/06/2003, 10h34
  4. calcul integral en c
    Par Anonymous dans le forum C
    Réponses: 3
    Dernier message: 11/01/2003, 11h32
  5. Réponses: 10
    Dernier message: 27/08/2002, 23h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo