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:
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:
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:
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:
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.
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:
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.