如何在struts2 + spring项目中使用请求范围? [英] How do I use request scope in struts2 + spring project?

查看:94
本文介绍了如何在struts2 + spring项目中使用请求范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目基于Struts2 + Spring + Hibernate。

在我的项目中,我正在使用''singleton''作为bean范围。

但是我想要为某些bean应用请求或会话范围。

我将描述我的项目的配置文件。



Web.xml

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







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>





所以我把他们的范围改为&#39; request&#39;但它没有工作。



错误报告如下。

无法实例化Action,kp.rns.jog .user.action.FreeUser,在命名空间''/ register''中为''freeUser''定义'创建名为''userMainModel''的bean的错误:范围''request''对于当前线程不活动;考虑为这个bean定义一个范围代理,如果你想从一个单例引用它;嵌套异常是java.lang.IllegalStateException:找不到线程绑定请求:您是指在实际Web请求之外的请求属性,还是在最初接收线程之外处理请求?如果您实际在Web请求中操作并仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。





****

任何帮助我...



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

推荐答案

你好,



如果您使用符合Servlet 2.4标准的容器,请尝试在您的web.xml中进行以下配置。

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>





如果您使用的是Servlet 2.3兼容的容器然后你将不得不在web.xml中使用以下配置





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>





如果这不起作用,那么你还必须在每个bean配置下添加以下行,你想要提供请求范围。





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,


这篇关于如何在struts2 + spring项目中使用请求范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆