Results 1 to 7 of 7

Thread: [Struts] Use bean in session

  1. #1
    Join Date
    Nov 2009
    Posts
    6

    Question How actived scope session when Struts use Spring with ContextLoaderPlugIn

    Hello,

    I try to use Bean in session with Spring and Struts, but I found this error :

    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.
    Spring create the bean as a Singleton, not as a bean in web session.

    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
    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>
    I tried with RequestContextListener, RequestContextFilter or the both… The problem is already the same.

    My struts-config.xml
    Code:
    <?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>
    My action-servlet.xml, for mapping action mapping’s path and Bean
    <?xml version="1.0" encoding="UTF-8"?>
    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>
    And to finish, the application-context.xml, to declare service, DAO and bean in session.
    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
    Last edited by Barbidure; Nov 9th, 2009 at 11:29 AM. Reason: Change the title

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Your filter doesn't do anything. If you add a filter you need to map it to a url, only adding a filter element is basicly useless.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2009
    Posts
    6

    Default

    Yes, I saw this error after my post, and added in the web.xml this configuration :
    Code:
    <filter-mapping>
    	<filter-name>requestContextFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    After, the weblogic deployments were updated. I have already the same problem.

    Error An error occurred during activation of changes, please see the log for details.

    Error weblogic.application.ModuleException:

    Error 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.
    Any idea?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    I wasn't completly finished . You are also duplicating the amount of beans loaded you are loading the 'application-context.xml' twice, which you really shouldn't be doing. In your case simply declaring a ContextLoaderPlugin should be enough.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Nov 2009
    Posts
    6

    Default

    For summary, I changed the web.xml with the Filter and the filter-mapping

    Code:
    <filter>
    	<filter-name>requestContextFilter</filter-name>
    	<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    </filter>
    
    <filter-mapping>
    	<filter-name>requestContextFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    And in the struts-action.xml, I declared the ContextLoaderPlugin with only one file

    Code:
    	
    <plug-in
    	className="org.springframework.web.struts.ContextLoaderPlugIn">
    	<set-property property="contextConfigLocation"
    		value="/WEB-INF/action-servlet.xml" />
    </plug-in>

    But, weblogic return always the same error :

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean wit
    h name '/resultatRecherche' defined in ServletContext resource [/WEB-INF/action-
    servlet.xml]: Cannot resolve reference to bean 'habilitationAccess' while settin
    g bean property 'habilitationAccess'; nested exception is org.springframework.be
    ans.factory.BeanCreationException: Error creating bean with name 'habilitationAc
    cess': 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; neste
    d exception is java.lang.IllegalStateException: No thread-bound request found: A
    re 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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    There is also an error in your configuration, next to scope="session" you also need to use an aop:scoped-proxy else it it not going to work. Which is also explained in the reference guide

    Code:
    <bean id="habilitationAccess" class="com.it.HabilitationAccessImpl" scope="session">
      <aop:scoped-proxy />
    </bean>
    which after closer inspection is also mentioned in the stacktrace...

    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.
    Last edited by Marten Deinum; Nov 10th, 2009 at 05:33 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Nov 2009
    Posts
    6

    Default

    That's working!

    Thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •