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 Java Discussion :

[Struts] Injecter un bean session avec Struts et Spring [Integration]


Sujet :

Spring Java

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 22
    Points : 19
    Points
    19
    Par défaut [Struts] Injecter un bean session avec Struts et Spring
    Bonjour,


    Mon application combine Struts 1.0 et Spring (le lien est réalisé avec un org.springframework.web.struts.ContextLoaderPlugIn). Je souhaiterais utiliser un bean contenant les droits de l’utilisateur en session, afin que le bean ne soit pas partagé entre les différents utilisateurs de l’application. Ainsi, je pourrais injecter ce bean dans les DAO, qui renverraient uniquement les informations auquel l’utilisateur courant à accès en toute transparence.

    Le problème, c’est que lorsque j’utilise un élément dans le scope session j’ai l’erreur suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name '/resultatRecherche' defined in ServletContext resource [/WEB-INF/action-servlet.xml]: 
    Cannot resolve reference to bean 'habilitationAccess' while setting bean property 'habilitationAccess'; 
    nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'habilitationAccess': Scope 'session' is not active for the current thread; 
    consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; 
    nested exception is java.lang.IllegalStateException: No thread-bound request found: 
    Are you referring to request attributes outside of an actual web request? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet/DispatcherPortlet: 
    In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    Dans le web.xml, j’ai declaré le RequestContextListener
    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
    49
    50
    51
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    	<display-name>myStrutsSpringApplication</display-name>
     
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/spring/application-context.xml
    		</param-value>
    	</context-param>
     
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
     
    	<listener>
    		<listener-class>
    		org.springframework.web.context.request.RequestContextListener
    		</listener-class>
    	</listener>
     
     
    	<servlet>
    		<servlet-name>action</servlet-name>
    		<servlet-class>
    			org.apache.struts.action.ActionServlet
    		</servlet-class>
    		<init-param>
    			<param-name>config</param-name>
    			<param-value>/WEB-INF/struts-config.xml</param-value>
    		</init-param>
    		<init-param>
    			<param-name>debug</param-name>
    			<param-value>2</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
     
    	<servlet-mapping>
    		<servlet-name>action</servlet-name>
    		<url-pattern>*.do</url-pattern>
    	</servlet-mapping>
     
    	<welcome-file-list>
    		<welcome-file>/accueil.do</welcome-file>
    		<welcome-file>/index.html</welcome-file>
    	</welcome-file-list>
    </web-app>
    En fait, j’ai essayé avec le RequestContextListener, RequestContextFilter, ou les deux. Mais, le problème est resté le même.


    Dans le struts-config.xml, je declare les différents éléments Struts et le ContextLoaderPlugIn.
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
    <struts-config>
     
    	<form-beans>
    		<form-bean name="SearchResultForm"
    			type="com.it.SearchResultForm" />
    	</form-beans>
     
    	<action-mappings>
    		<action name="SearchResultForm" path="/resultatRecherche"
    			type="org.springframework.web.struts.DelegatingActionProxy"
    			parameter="actionName">
    			<forward name="success" path="resultatRecherche.tiles"
    				redirect="false" />
    		</action>
     
    	</action-mappings>
     
    	<plug-in
    		className="org.springframework.web.struts.ContextLoaderPlugIn">
    		<set-property property="contextConfigLocation"
    			value="/WEB-INF/action-servlet.xml,/WEB-INF/spring/application-context.xml" />
    	</plug-in>
     
    	<plug-in className="org.apache.struts.tiles.TilesPlugin">
    		<set-property property="definitions-config"
    			value="/WEB-INF/tiles-def.xml" />
    		<set-property property="moduleAware" value="true" />
    		<set-property property="definitions-parser-validate"
    			value="true" />
    	</plug-in>
     
    </struts-config>
    L’action-servlet.xml, ou je mappe avec les bean Spring
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     
    	<bean id="abstractAction" abstract="true">
    		<property name="habilitationAccess" ref="habilitationAccess" />
    	</bean>
     
    	<bean name="/resultatRecherche" class="com.it.SearchResultAction" parent="abstractAction">
    		<property name="articleService" ref="articleService"/>
    	</bean>
     
    </beans>
    Et pour terminer, la déclaration des différents bean Spring, dont la habilitationAccess en seesion.

    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
    49
    50
    51
    52
    <?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:jee="http://www.springframework.org/schema/jee"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
     
    	<bean
    	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="order" value="0" />
    		<property name="ignoreUnresolvablePlaceholders" value="true" />
    		<property name="location">
    			<value>${conf.spring.wls11g}</value>
    		</property>
    	</bean>
     
    	<jee:jndi-lookup id="dataSource" jndi-name="${DATASOURCE }"
    		cache="true" lookup-on-startup="true" />
     
    	<!-- DAO -->
    	<bean id="abstractDao" abstract="true">
    		<property name="dataSource" ref="dataSource" />
    		<property name="habilitationAccess" ref="habilitationAccess" />
    	</bean>
     
    	<bean id="articleInc"
    		class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer">
    		<property name="dataSource" ref="dataSource" />
    		<property name="incrementerName" value="S_T_ARTICLE" />
    	</bean>
    	<bean id="articleDAO" parent="abstractDao"
    		class="com.it.JdbcArticleDAOImpl">
    		<property name="incrementer" ref="articleInc" />
    	</bean>
    	<!-- END DAO -->
     
    	<!-- SERVICE -->
    	<bean id="articleService" class="com.it.ArticleServiceImpl">
    		<property name="articleDAO" ref="articleDAO"/>
    	</bean>
    	<!-- END SERVICE -->
     
    	<!--	HABILITATION	-->
    	<bean id="habilitationAccess"
    		class="com.it.HabilitationAccessImpl" scope="session">
    	</bean>
    	<!--	END HABILITATION	-->
     
    </beans>
    Si vous avez des idées pour faire fonctionner cela m’interresse au plus au point!

    Merci d’avance

  2. #2
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 22
    Points : 19
    Points
    19
    Par défaut
    Ca y est! J'ai fini par trouver la solution

    Il fallait déclarer l'élément du scope session de la façon suivante

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <beans xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
    <a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a> http://www.springframework.org/schema/aop/spring-aop.xsd">
    	<bean id="habilitationAccess"
    		class="com.it.bae.habilitation.HabilitationAccessImpl" scope="session">
    		<aop:scoped-proxy/>
    	</bean>
    </beans>
    Sans oublier d'ajouter les lib spring-oap et cglib

  3. #3
    Membre régulier
    Profil pro
    Étudiant
    Inscrit en
    Août 2010
    Messages
    133
    Détails du profil
    Informations personnelles :
    Localisation : France, Territoire de Belfort (Franche Comté)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2010
    Messages : 133
    Points : 93
    Points
    93
    Par défaut
    Merci de nous avoir fourni la réponse.

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

Discussions similaires

  1. Session avec Struts 2
    Par fruwen7 dans le forum Struts 2
    Réponses: 0
    Dernier message: 01/08/2012, 13h59
  2. Gestion de session avec Struts
    Par bourbah dans le forum Struts 1
    Réponses: 1
    Dernier message: 31/12/2011, 19h41
  3. [Spring + Struts]Injecter un bean dans un plugin Struts
    Par Saverok dans le forum Spring Web
    Réponses: 1
    Dernier message: 18/12/2008, 15h18
  4. [DisplayTag] Probleme Request/Session avec Struts
    Par wariom dans le forum Taglibs
    Réponses: 5
    Dernier message: 03/04/2008, 10h44
  5. Réponses: 2
    Dernier message: 20/08/2005, 19h23

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