Hello,
I try to use Bean in session with Spring and Struts, but I found this error :
Spring create the bean as a Singleton, not as a bean in web session.Code: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.
For information, I linked Struts and Spring with org.springframework.web.struts.ContextLoaderPlugIn (see the Struts-config.xml below)
In the web.xml, I declared the RequestContextListener
I tried with RequestContextListener, RequestContextFilter or the both… The problem is already the same.Code:<?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> <filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </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>
My struts-config.xml
My action-servlet.xml, for mapping action mapping’s path and BeanCode:<?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>
<?xml version="1.0" encoding="UTF-8"?>
And to finish, the application-context.xml, to declare service, DAO and bean in session.Code:<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>
Code:<?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"> <!-- HABILITATION --> <bean id="habilitationAccess" class="com.it.HabilitationAccessImpl" scope="session"> </bean> <!-- END HABILITATION --> <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 --> </beans>
I just want to create one bean in session and inject this into Action Struts and DAO.
Someone have an idea?
Thank you
Barbidure
P.S. : Servlet-api : version 2.5





Reply With Quote
