Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Project is based on Struts2 + Spring + Hibernate.
In my project, I'm using 'singleton' for bean scopes.
But I want to apply request or session scope for the some beans.
I'll describe configuration files of my projects.

Web.xml
XML
<listener> <!-- Required for the struts2 spring plugin to work -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           /WEB-INF/applicationContext*.xml
        </param-value>
    </context-param>
<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>




applicationContext.xml

HTML
<bean id="userMainModel" class="kp.rns.jog.user.model.UserMainModel" scope="request">
		<property name="userMainDao" ref="userMainDao"></property>
	</bean>
	
	<bean id="userMainAction" class="kp.rns.jog.user.action.FreeUser" scope="request">
		<property name="userMainModel" ref="userMainModel"></property>
	</bean>


So I changed their scope to &#39;request&#39; but it doesn&#39;t work.

Errors are reported as following.
Unable to instantiate Action, kp.rns.jog.user.action.FreeUser, defined for 'freeUser' in namespace '/register'Error creating bean with name 'userMainModel': Scope 'request' 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, or processing a request outside of the originally receiving thread? 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.


****
Anyone helps me...
Posted

1 solution

Hello,

Try putting following configuration in your web.xml if your are using Servlet 2.4 compliant container.
xm
<listener>
  <listener-class>
      org.springframework.web.context.request.RequestContextListener
  </listener-class>
</listener>


If you are using Servlet 2.3 compliant container then you will have to use following configuration in web.xml

XML
<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>


If this does not work then you will also have to add following line under each bean configuration which you want to make available with request scope.

XML
<aop:scoped-proxy/>



Your application-context file will look something like shown below.
XML
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="userMainModel" class="kp.rns.jog.user.model.UserMainModel" scope="request">
        <property name="userMainDao" ref="userMainDao"></property>
        <aop:scoped-proxy/>
    </bean>
	
    <bean id="userMainAction" class="kp.rns.jog.user.action.FreeUser" scope="request">
        <property name="userMainModel" ref="userMainModel"></property>
        <aop:scoped-proxy/>
    </bean>
</beans>


Regards,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900