Struts 2中的Scoped Model Driven拦截器没有将模型绑定到会话范围

时间:2016-01-19 13:18:13

标签: java configuration struts2 struts2-interceptors interceptorstack

我想在多个请求中共享模型(表单数据),因此在操作类中实现了ScopedModelDriven Interceptor。

以下是我的代码

模型 - EventSearchBean.java

public class EventSearchBean {

   private Integer eventId;

   private String location;

   //getters and setters

}

操作 - EventSearchAction

public class EventSearchAction implements ScopedModelDriven<EventSearchBean>
{
   private EventSearchBean eventSearchBean;

   public static final String EVENT_MODEL_SESSION_KEY = "eventSearchBean"; 

   public EventSearchBean getModel() {
        return eventSearchBean;
   }

   public String getScopeKey() {
        return EVENT_MODEL_SESSION_KEY;
   }

   public void setModel(EventSearchBean eventSearchBean) {
        this.eventSearchBean = eventSearchBean;  
   }

   public void setScopeKey(String arg0) {
        // TODO Auto-generated method stub
   }

   public String execute();
   {
     String locale = eventSearchBean.getLocation();

     //Calling business service to fetch events based on location
     List<> eventList = eventManager.getEvents(locale);

     return "success";

   }
}

struts.xml中

     <!-- old stack used for other action classes -->

    <interceptor-stack name="oldStack">
      <interceptor-ref name="servletConfig"/>
      <interceptor-ref name="modelDriven"/>
      <interceptor-ref name="exception"/>  
      <interceptor-ref name="fileUpload"/>
      <interceptor-ref name="params"/>
    </interceptor-stack>

   <!-- new stack used for EventSearchAction class -->
    <interceptor-stack name="newStack">
      <interceptor-ref name="servletConfig"/>
      <interceptor-ref name="scopedModelDriven"/>
      <interceptor-ref name="exception"/>  
      <interceptor-ref name="fileUpload"/>
      <interceptor-ref name="params"/>
    </interceptor-stack> 

    <action name="eventSearch" class="com.karthik.EventSearchAction">
       <interceptor-ref name="newStack">
         <param  name="scope">session</param>
         <param  name="name">eventSearchBean</param>
         <param  name="className">com.karthik.beans.EventSearchBean</param> 
       </interceptor-ref>
       <result name="success">/jsp/eventlist.jsp</result>
       <result name="error">/jsp/generalExceptionPage.jsp</result>
    </action>

1)每个请求都会创建新模型(模型数据不会从会话中复制以用于后续请求)。    需要在代码中更改什么才能将模型放入会话范围?    如何使模型的行为类似于Struts1中会话范围的ActionForm?

2)如果我在声明模型时删除了动作类中的新操作符,那就是私有的EventSearchBean eventSearchBean;
   当我在动作类中访问模型时,我得到Null Pointer异常。
如何声明/初始化模型?

3)如果在UI中提交表单,如何在会话中覆盖/更新模型?

2 个答案:

答案 0 :(得分:1)

爆炸defaultStack,这就是你要定义的内容:

<interceptor-stack name="defaultStack">
    <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/> <!-- here -->
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="datetime"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params"/>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>
    <interceptor-ref name="deprecation"/>
</interceptor-stack>

<interceptor-ref name="scopedModelDriven">
    <param  name="scope">session</param>
    <param  name="name">eventSearchBean</param>
    <param  name="className">com.karthik.beans.EventSearchBean</param> 
</interceptor-ref>

如您所见,它已经包含了ScopedModelDriven拦截器。因此,你定义它两次,第一次在defaultStack中,第二次是手动定义,但只有第二次设置参数,第一次将所有内容都清空:)

然后,而不是

<interceptor-ref name="defaultStack"/>
<interceptor-ref name="scopedModelDriven">
    <param  name="scope">session</param>
    <param  name="name">eventSearchBean</param>
    <param  name="className">com.karthik.beans.EventSearchBean</param> 
</interceptor-ref>

只需使用

<interceptor-ref name="defaultStack">
    <param  name="scopedModelDriven.scope">session</param>
    <param  name="scopedModelDriven.name">eventSearchBean</param>
    <param  name="scopedModelDriven.className">com.karthik.beans.EventSearchBean</param> 
</interceptor-ref>

答案 1 :(得分:0)

    <interceptor-stack name="newStack">
       <interceptor-ref name="servletConfig"/>
       <interceptor-ref name="scopedModelDriven"/>
       <interceptor-ref name="modelDriven"/>
       <interceptor-ref name="exception"/>  
       <interceptor-ref name="fileUpload"/>
       <interceptor-ref name="params"/>
    </interceptor-stack> 

    <action name="eventSearch" class="com.karthik.EventSearchAction">
       <interceptor-ref name="newStack">
         <param name="scopedModelDriven.scope">session</param>
         <param name="scopedModelDriven.name">eventSearchBean</param>
         <param name="scopedModelDriven.className">com.karthik.beans.EventSearchBean</param> 
       </interceptor-ref>
       <result name="success">/jsp/eventlist.jsp</result>
       <result name="error">/jsp/generalExceptionPage.jsp</result>
    </action>

代码中进行了以下更改:

  • 如果使用自定义堆栈,则自定义堆栈应包含modelDriven 除了scopedModelDriven拦截器之外的拦截器。

  • 如果在动作类中指定了参数,如上所示, params必须以scopedModelDriven为前缀。那是, scopedModelDriven.paramName,如果在动作类外部的拦截器堆栈中指定了scopedModelDriven参数,则不需要前缀。

  • 不得使用new初始化action类中的模型 运营商。该模型必须刚刚宣布。那就是私人 EventSearchBean eventSearchBean;