【问题标题】:Struts2+ Spring : Request Paremeters dont get reset in Action objects?Struts 2 Spring:请求参数不会在 Action 对象中重置?
【发布时间】:2012-08-17 15:14:33
【问题描述】:

我正在开发 Spring 3 + Struts2 应用程序,我在 Spring 中配置我的操作如下:

  <bean id="patientSearchAPIClass" class="com.axiohelix.nozoki.web.action.api.PatientSearch">     
       <property name="searchService" ref="searchService"/>        
    </bean> 

但在我的 Action 类中,我保留字段来存储请求参数,

public class PatientSearch extends ActionSupport {


        public String getPharumoId() {      
        return pharumoId;
        }

        public void setPharumoId(String pharumoId) {
        this.pharumoId = pharumoId;
         }

        public String getName() {
        return name;
        }
        public void setName(String name) {
        this.name = name;
        }

        private String name;
        private String pharumoId;
        ..


        public String execute(){        
        searchResults=searchService.searchPatients(pharumoId,                                                  name,
                                                   birthday,
                                                   pharmacyId,
                                                   clinic,
                                                   doctorName,
                                                   drugName,
                                                   supplyDate,                                                 
                                                   offset, 
                                                   pageSize);       

        return Action.SUCCESS;
    }

此操作返回 JSON 输出,我使用以下 URL 访问它:

http://localhost/app/searchAPI.action?name=UserName

如果我下次使用 URL 访问:

http://localhost/app/searchAPI.action

“名称”字段设置为之前的“用户名”值。

1.如何根据请求重置这些值?

2.我认为 Action 类是根据请求实例化的,不是吗?

【问题讨论】:

  • 如果是,你是否在使用 struts2-spring 插件,将 bean 范围更改为 prototype因为默认情况下它的单例和 S2 也使用 Action 作为数据传输对象。就像 &lt;bean id="patientSearchAPIClass" class="com.axiohelix.nozoki.web.action.api.PatientSearch" scope=prototype&gt;
  • 是的,我正在使用 struct2-spring 插件。感谢您的提示。让我试试吧。
  • @UmeshAwasthi 谢谢它的工作。你能把这个作为答案发布,以便我可以选择它作为正确的。

标签: java spring struts2 httprequest


【解决方案1】:

问题在于 Spring 创建 Action 类的方式。默认情况下,Spring 创建单例实例,对于 Struts2,Action 类也可以作为模型工作,因为这个框架会创建一个新的 Action 实例并将其放入值堆栈.

在使用 Spring 创建动作类时,请确保将范围定义为原型,如

<bean id="patientSearchAPIClass" 
 class="com.axiohelix.nozoki.web.action.api.PatientSearch" scope=prototype>

因此 Spring 应该为每个请求创建新的 Action 实例。

【讨论】:

  • @StevenBenitez:是的,同意,我见过很多人在使用 spring 作为 DI 时遇到同样的问题
  • 我正在使用 Spring 维护一个 Struts 项目。老实说,我讨厌 Struts,因为 Action 必须用 prototype 范围声明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多