You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Niral Trivedi <ni...@gmail.com> on 2008/01/19 07:14:50 UTC

STRUTS2 and Spring2

Hi All,

I am using RAD7.0, JDK 1.5, Struts2 and Spring 2.x. We have been using
Struts1.x and Spring 1.x for a long time now and moving our app to Struts2 +
Spring2

We are trying to have Spring manage all the value objects and action
objects. And so followed steps in Struts document to add struts2-spring
plugin jar and made necessary changes but getting errors when try to create
a bean in session scope. Following are my configuration files:

web.xml
==================================================================================================================================================
.........................
<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.FilterDispatcher
</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
........
=========================================================================================================
Spring applicationContext.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-2.0.xsd
        http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
........

    <bean id="mVO" class="com.foo.bar.vo.MyVO" scope="session">
        <aop:scoped-proxy/>
    </bean>
    <bean id="myInterceptor" class="com.foo.bar.interceptors.MyInterceptor">
        <property name="mvo">
            <ref local="mVO" />
        </property>
    </bean>
.......
=======================================================================================================
With this configuration, server starts up correct without any issues, but
when I try to access my action class, I am getting following error:
=====================================================================================================
[1/18/08 23:49:51:855 EST] 00000032 WebApp        E   [Servlet Error]-[Error
creating bean with name 'scopedTarget.mVO': 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.]: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'scopedTarget.mVO': 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.
Caused by: 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.
    at
org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes
(RequestContextHolder.java:102)
    at org.springframework.web.context.request.SessionScope.get(
SessionScope.java:88)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:283)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:160)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(
SimpleBeanTargetSource.java:33)
    at
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget
(Cglib2AopProxy.java:660)
    at
org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept
(Cglib2AopProxy.java:610)
    at com.foo.bar.vo.MyVO$$EnhancerByCGLIB$$d821bb7e.setClassName
(<generated>)
    at com.foo.bar.interceptors.MyInterceptor.intercept(MyInterceptor.java
:56)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(
DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(
DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(
UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(
DefaultActionInvocation.java:221)
    at org.apache.struts2.impl.StrutsActionProxy.execute(
StrutsActionProxy.java:50)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(
Dispatcher.java:504)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(
FilterDispatcher.java:419)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(
FilterInstanceWrapper.java:190)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(
WebAppFilterChain.java:130)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(
WebAppFilterChain.java:87)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(
WebAppFilterManager.java:701)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(
WebAppFilterManager.java:646)
    at
com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(
DefaultExtensionProcessor.java:628)
    at
com.ibm.ws.wswebcontainer.extension.DefaultExtensionProcessor.invokeFilters(
DefaultExtensionProcessor.java:145)
    at
com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(
DefaultExtensionProcessor.java:595)
    at
com.ibm.ws.wswebcontainer.extension.DefaultExtensionProcessor.handleRequest(
DefaultExtensionProcessor.java:111)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3129)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java
:238)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java
:811)
    at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(
WebContainer.java:1433)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(
WCChannelLink.java:93)
    at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(
HttpInboundLink.java:465)
    at
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(
HttpInboundLink.java:394)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(
HttpICLReadCallback.java:102)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted
(AioReadCompletionListener.java:152)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(
AbstractAsyncFuture.java:213)
    at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(
AbstractAsyncFuture.java:195)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:194)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(
ResultHandler.java:741)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:863)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)
===========================================================================================================

So, I tried changing web.xml and replace
org.springframework.web.context.ContextLoaderListener with
org.springframework.web.context.request.RequestContextListener. At that
point, server starts up correctly, but while accessing action class, I am
getting error message saying :

[1/19/08 0:54:47:605 EST] 00000032 StrutsSpringO E
org.apache.commons.logging.impl.Jdk14Logger fatal ********** FATAL ERROR
STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid
ApplicationContext.
You might need to add the following to web.xml:
    <listener>
        <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

It looks like, Struts2 only works with ContextLoaderListener, but Spring2
requires to use RequestContextListener if you are using session/request
scoped bean in Servlet 2.4+.

I tried to search on mailing archive but didn't find any solution.

Can somebody please help if they have tried implementing session/request
scoped beans using Spring2 with Struts2?

Thanks a bunch in advance.

Re: STRUTS2 and Spring2

Posted by Dave Newton <ne...@yahoo.com>.
--- Niral Trivedi <ni...@gmail.com> wrote:
> I have looked up api and spring doc for both the listeners. But I was
> trying
> to say, if I add RequestContextListener then Struts2 plugin gives error
> saying that You have to add ContextLoaderListener. And if I add
> ContextLoaderListener, spring gives error saying add
> RequestContextListener.
> 
> Can I have both listener at same time? I haven't tried doing that but will
> try and see if that works..

...

Both error messages say to *add* the listener you're missing. The API docs
are pretty clear that their purposes are *completely* different. The
reference guide explains what both of them do.

d.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: STRUTS2 and Spring2

Posted by Niral Trivedi <ni...@gmail.com>.
Hi Dave,

I have looked up api and spring doc for both the listeners. But I was trying
to say, if I add RequestContextListener then Struts2 plugin gives error
saying that You have to add ContextLoaderListener. And if I add
ContextLoaderListener, spring gives error saying add RequestContextListener.

Can I have both listener at same time? I haven't tried doing that but will
try and see if that works..

Thank you.

On Jan 19, 2008 8:57 AM, Dave Newton <ne...@yahoo.com> wrote:

> --- Niral Trivedi <ni...@gmail.com> wrote:
> > We are trying to have Spring manage all the value objects and action
> > objects. And so followed steps in Struts document to add struts2-spring
> > plugin jar and made necessary changes but getting errors when try to
> create
> > a bean in session scope. Following are my configuration files:
> > [...]
> > [1/18/08 23:49:51:855 EST] 00000032 WebApp        E   [Servlet
> > Error]-[Error
> > creating bean with name 'scopedTarget.mVO': 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.]:
> > [...]
> > So, I tried changing web.xml and replace
> > org.springframework.web.context.ContextLoaderListener with
> > org.springframework.web.context.request.RequestContextListener. At that
> > point, server starts up correctly, but while accessing action class, I
> am
> > getting error message saying :
>
> The reference manuals for both 2.0 and 2.5 say to *add* the
> RequestContextListener [1,2], not to remove the listener that sets up your
> application context.
>
> I'd recommend perusing the reference doc a bit more and also checking the
> API
> documentation for both ContextLoaderListener [3] and
> RequestContextListener
> [4] to understand what each of them do.
>
> d.
>
> [1]
>
> http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes-other
> [2]
>
> http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-other
> [3]
>
> http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/context/ContextLoaderListener.html
> [4]
>
> http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/context/request/RequestContextListener.html
> [tags: s2, spring, config, scope]
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: STRUTS2 and Spring2

Posted by Dave Newton <ne...@yahoo.com>.
--- Niral Trivedi <ni...@gmail.com> wrote:
> We are trying to have Spring manage all the value objects and action
> objects. And so followed steps in Struts document to add struts2-spring
> plugin jar and made necessary changes but getting errors when try to create
> a bean in session scope. Following are my configuration files:
> [...]
> [1/18/08 23:49:51:855 EST] 00000032 WebApp        E   [Servlet
> Error]-[Error
> creating bean with name 'scopedTarget.mVO': 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.]: 
> [...]
> So, I tried changing web.xml and replace
> org.springframework.web.context.ContextLoaderListener with
> org.springframework.web.context.request.RequestContextListener. At that
> point, server starts up correctly, but while accessing action class, I am
> getting error message saying :

The reference manuals for both 2.0 and 2.5 say to *add* the
RequestContextListener [1,2], not to remove the listener that sets up your
application context.

I'd recommend perusing the reference doc a bit more and also checking the API
documentation for both ContextLoaderListener [3] and RequestContextListener
[4] to understand what each of them do.

d.

[1]
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes-other
[2]
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-other
[3]
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/context/ContextLoaderListener.html
[4]
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/web/context/request/RequestContextListener.html
[tags: s2, spring, config, scope]



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org