0

I have web RestClient with struts2 MVC and spring DI and use struts2-spring-plugin to inject after login Authenticated RestClient bean to struts2 Actions in session scope. If I have in web.xml ContextLoaderListener needed for struts2-spring-plugin

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

There is not session scope (my IDE mark session in red) only prototype and singleton are available:

<bean id="restClient" class="com.test.services.RestClient" scope="session">
        <constructor-arg ref="dataSource"/>
</bean>

If I have in web.xml RequestContextListener session scope appears

<listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

but this Exception appears full stacktrace

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?

I know that this problem not exist if I switch to spring MVC but can I use spring bean in session scope with struts2 framework or is there any alternative with struts2 ?

sytolk
  • 7,223
  • 3
  • 25
  • 38
  • Add both `ContextLoaderListener` and `RequestContextListener`. – Aleksandr M Apr 19 '16 at 09:06
  • Thanks but this exception appears whith both listeners too. – sytolk Apr 19 '16 at 19:51
  • Add some code snippets to show how and from where are you calling this bean. And post the whole stacktrace too. – Aleksandr M Apr 20 '16 at 10:58
  • I have not calling this session bean is injected in struts2 Actions and error appears while application start: Injection of autowired dependencies failed. I have add full stacktrace in question this is the link http://pastebin.com/Xz0wV0rB If I have change bean scope from session to singleton its not have this exception. – sytolk Apr 20 '16 at 11:31
  • 1
    S2 isn't related. See http://stackoverflow.com/q/21286675/1700321 and http://stackoverflow.com/q/7598412/1700321. – Aleksandr M Apr 20 '16 at 11:32
  • I not think so the problem is that scoped bean must be inside the request thread but struts2-spring-plugin use different thread.. – sytolk Apr 20 '16 at 11:43
  • What? Have you read linked questions? You cannot simple use session scoped beans anywhere you like. – Aleksandr M Apr 20 '16 at 11:54
  • 1
    Yes you are right I have add to the session bean and Exception is gone. Thanks – sytolk Apr 20 '16 at 11:58
  • 1
    Spring scopes are different than Struts scopes, however if Struts can work without AOP, Spring can't. – Roman C Apr 20 '16 at 17:47
  • @RomanC I`m not very happy with AOP. Do its have in struts2 analogical to Spring session scope bean? – sytolk Apr 21 '16 at 06:41
  • See this question http://stackoverflow.com/q/17244036/573032 – Roman C Apr 21 '16 at 07:22

0 Answers0