1 4 package demo.townsend.action; 5 6 import demo.townsend.common.Constants; 7 import demo.townsend.form.AddToListForm; 8 import demo.townsend.service.DataKeeper; 9 import demo.townsend.service.Product; 10 import demo.townsend.service.ProductCatalog; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import javax.servlet.http.HttpSession ; 16 import org.apache.struts.action.Action; 17 import org.apache.struts.action.ActionForm; 18 import org.apache.struts.action.ActionForward; 19 import org.apache.struts.action.ActionMapping; 20 21 27 public class AddToListAction extends Action { 28 public ActionForward execute( ActionMapping mapping, 29 ActionForm form, 30 HttpServletRequest request, 31 HttpServletResponse response) 32 throws Exception { 33 34 String newProdId = ((AddToListForm)form).getId(); 35 Product newProd = null; 36 ArrayList catalog = new ProductCatalog().getCatalog(); 37 for (Iterator iter = catalog.iterator(); iter.hasNext(); ) { 38 Product p = (Product) iter.next(); 39 if (p.getId().equals(newProdId)) { 40 newProd = p; 41 } 42 } 43 44 HttpSession session = (HttpSession )request.getSession(); 45 46 DataKeeper dkeeper = (DataKeeper)session.getAttribute( Constants.DATA_KEY); 47 if (dkeeper == null) { 48 dkeeper = new DataKeeper(); 49 } 50 51 dkeeper.addListItem(newProd); 52 53 session.setAttribute( Constants.DATA_KEY, dkeeper ); 54 55 return mapping.findForward(Constants.SUCCESS_KEY ); 56 } 57 } 58 | Popular Tags |