KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > jpetstore > web > struts > ViewCategoryAction


1 package org.springframework.samples.jpetstore.web.struts;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9
10 import org.springframework.beans.support.PagedListHolder;
11 import org.springframework.samples.jpetstore.domain.Category;
12
13 public class ViewCategoryAction extends BaseAction {
14
15   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
16     String JavaDoc categoryId = request.getParameter("categoryId");
17     if (categoryId != null) {
18             Category category = getPetStore().getCategory(categoryId);
19       PagedListHolder productList = new PagedListHolder(getPetStore().getProductListByCategory(categoryId));
20             productList.setPageSize(4);
21             request.getSession().setAttribute("ViewProductAction_category", category);
22             request.getSession().setAttribute("ViewProductAction_productList", productList);
23             request.setAttribute("category", category);
24             request.setAttribute("productList", productList);
25     }
26         else {
27             Category category = (Category) request.getSession().getAttribute("ViewProductAction_category");
28             PagedListHolder productList = (PagedListHolder) request.getSession().getAttribute("ViewProductAction_productList");
29             if (category == null || productList == null) {
30                 throw new IllegalStateException JavaDoc("Cannot find pre-loaded category and product list");
31             }
32       String JavaDoc page = request.getParameter("page");
33       if ("next".equals(page)) {
34         productList.nextPage();
35       }
36             else if ("previous".equals(page)) {
37         productList.previousPage();
38       }
39             request.setAttribute("category", category);
40             request.setAttribute("productList", productList);
41     }
42     return mapping.findForward("success");
43   }
44
45 }
46
Popular Tags