I want to share model(form data) among multiple requests so have implemented ScopedModelDriven Interceptor in the action class.
Below is my code
Model - EventSearchBean.java
public class EventSearchBean {
private Integer eventId;
private String location;
//getters and setters
}
Action - 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) New model is getting created on every request(Model data is not getting copied from session for subsequent request).
What needs to be changed in code to put model in session scope?
How to make model behave like ActionForm of session scope in Struts1?
2) If I remove new operator in action class while declaring model, that is
private EventSearchBean eventSearchBean;
I get Null Pointer exception when I access model in action class.
How to declare/initialize model?
3) How to override/update model in session only when form is submitted in UI?