今天在使用分页进行查询的时候,出现一个问题:每次我选中特定的条件进行查询,当我重新点击struts的action时,按道理,是应该重新回到最初的没有选择任何查询条件的页面,但不管我怎么点击,重新发起请求,每次的结果都是按刚开始的查询条件查询出来的结果。
因为使用的Spring + mybatis + struts的框架,经同事指点,发现spring配置文件的,相应的action配置如下:
<bean id="AlarmInfoAction" class="com.cstor.network.alarm.action.AlarmInfoAction" >
<property name="alarmInfoService" ref="alarmInfoServiceImpl"/>
</bean>
添加上scope范围之后,问题圆满解决:
<bean id="AlarmInfoAction" class="com.cstor.network.alarm.action.AlarmInfoAction"scope="prototype">
<property name="alarmInfoService" ref="alarmInfoServiceImpl"/>
</bean>
对于spring的prototype范围,Spring官方文档对prototype的解释如下:
The prototype scope
The non-singleton, prototype scope of bean deployment results in thecreation of a new bean instance every time a request for that specific bean is made. That is, the bean is injected into another bean or you request it through a getBean()method call on the container. As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans.The following diagram illustrates the Spring prototype scope.A data access object (DAO) is not typically configured as a prototype, because a typical DAO does not hold any conversational state; it was just easier for this author to reuse the core of the singleton diagram.

The following example defines a bean as a prototype in XML:
<!-- using spring-beans-2.0.dtd --><bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance. Thus, althoughinitialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configureddestructionlifecycle
callbacks arenotcalled. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding. To get the Spring container to release resources held by prototype-scoped beans, try using a custombeanpost-processor, which holds a reference to beans that
need to be cleaned up. In some respects, the Spring container's role in regard to a prototype-scoped bean is a replacement for the Javanewoperator. All lifecycle management past that point must be handled by the client. (For details on
the lifecycle of a bean in the Spring container, see the section called “Lifecycle callbacks”.)
scope="prototype"(多态)是在每次用户发起请求时重新生成action对象,对于多线程访问不会出现问题,如果没有配置scope=prototype则添加的时候不会新建一个action,他任然会保留上次访问的过记录的信息。
默认是scope="singleton"(单态),这些bean被spring初始化后,始终只有一份,很适用于无状态的bean,DAO、Service都采用的这种。当然,scope的值还有session,request等等。