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