01: /*
02: @COPYRIGHT@
03: */
04: package demo.tasklist.action;
05:
06: import demo.tasklist.common.Constants;
07: import demo.tasklist.form.AddToListForm;
08: import demo.tasklist.service.DataKeeper;
09: import demo.tasklist.service.ErrorKeeper;
10: import javax.servlet.http.HttpServletRequest;
11: import javax.servlet.http.HttpServletResponse;
12: import javax.servlet.http.HttpSession;
13: import org.apache.struts.action.Action;
14: import org.apache.struts.action.ActionForm;
15: import org.apache.struts.action.ActionForward;
16: import org.apache.struts.action.ActionMapping;
17:
18: /**
19: * AddToListAction processes the request to add an item to the task list.
20: * Task list is fetched from the HttpSession object, the item indicated in
21: * the AddToListForm is added to the list, and the modified list is loaded back
22: * into the HttpSession object.
23: */
24: public class AddToListAction extends Action {
25: public ActionForward execute(ActionMapping mapping,
26: ActionForm form, HttpServletRequest request,
27: HttpServletResponse response) throws Exception {
28: HttpSession session = (HttpSession) request.getSession();
29:
30: AddToListForm addToListForm = (AddToListForm) form;
31: String newListItem = addToListForm.getNewListItem();
32: String errorMsg = addToListForm.getErrorMsg();
33:
34: if (errorMsg != null) {
35: session.setAttribute(Constants.ERROR_KEY, new ErrorKeeper(
36: errorMsg));
37: } else {
38: session.removeAttribute(Constants.ERROR_KEY);
39: }
40:
41: DataKeeper dkeeper = (DataKeeper) session
42: .getAttribute(Constants.DATA_KEY);
43: if (dkeeper == null) {
44: dkeeper = new DataKeeper();
45: }
46: dkeeper.addListItem(newListItem);
47:
48: session.setAttribute(Constants.DATA_KEY, dkeeper);
49:
50: return mapping.findForward(Constants.SUCCESS_KEY);
51: }
52: }
|