Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java struts
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
<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
 
<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 12-Mar-13 8:12am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Try putting following configuration in your web.xml if your are using Servlet 2.4 compliant container.
<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
 
<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.
 
<aop:scoped-proxy/>
 

Your application-context file will look something like shown below.
<?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,
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 12 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100