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