我想在多个请求之间共享模型(表单数据),因此在动作类中实现了 ScopedModelDriven 拦截器。
下面是我的代码
模型 - 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;
在操作类中访问模型时出现空指针异常。
如何声明/初始化模型?
3)只有在 UI 中提交表单时,如何在会话中覆盖/更新模型?