📄 shoppingaction.java
字号:
package com.tarena.shoppingcar.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;import org.apache.struts.actions.MappingDispatchAction;import com.tarena.shoppingcar.biz.ProductBiz;import com.tarena.shoppingcar.biz.UserBiz;import com.tarena.shoppingcar.entity.Cart;import com.tarena.shoppingcar.entity.Item;import com.tarena.shoppingcar.entity.Product;import com.tarena.shoppingcar.entity.User;import com.tarena.shoppingcar.util.PaginationInfo;public class ShoppingAction extends MappingDispatchAction { private ProductBiz pb = new ProductBiz(); private UserBiz ub = new UserBiz(); public ActionForward productList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = new PaginationInfo(); info.setRowPerPage(6); int rowCount = pb.getRowCount(); info.setRowCount(rowCount); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); session.setAttribute("info", info); forward = mapping.findForward("productList"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("product.list")); forward = mapping.findForward("error"); } return forward; } public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName == null || userName.trim().length() == 0 || password == null || password.trim().length() == 0) { return mapping.findForward("login"); } User user = ub.find(userName, password); if (user != null) { HttpSession session = request.getSession(); session.setAttribute("user", user); String path = (String) session.getAttribute("path"); if (path == null) { forward = mapping.findForward("success"); } else { forward = mapping.findForward("back"); } } else { forward = mapping.findForward("login"); } } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("user.login")); forward = mapping.findForward("error"); } return forward; } public ActionForward cartLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); String path = request.getContextPath(); session.setAttribute("path", path); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("user.cartLogin")); forward = mapping.findForward("error"); } return forward; } public ActionForward addProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); if (session.getAttribute("car") == null) { Cart car = new Cart(); session.setAttribute("car", car); } int id = Integer.parseInt(request.getParameter("id")); Product product = pb.findById(id); Item item = new Item(); item.addProduct(product); Cart car = (Cart) session.getAttribute("car"); car.addItem(item); session.setAttribute("car", car); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("product.add")); forward = mapping.findForward("error"); } return forward; } public ActionForward viewCar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } //分页显示 //第一页 public ActionForward firstPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); info.firstPage(); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("page.first")); forward = mapping.findForward("error"); } return forward; } //最后一页 public ActionForward lastPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); info.lastPage(); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("page.last")); forward = mapping.findForward("error"); } return forward; } //下一页 public ActionForward nextPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); info.nextPage(); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("page.next")); forward = mapping.findForward("error"); } return forward; } //上一页 public ActionForward previousPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); info.previousPage(); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("page.previous")); forward = mapping.findForward("error"); } return forward; } public ActionForward gotoPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); String pageNo = request.getParameter("pageNo"); info.gotoPage(Integer.parseInt(pageNo)); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("page.goto")); forward = mapping.findForward("error"); } return forward; } public ActionForward findBySession(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); PaginationInfo info = (PaginationInfo) session.getAttribute("info"); int currentPage = info.getCurrentPage(); info.gotoPage(currentPage); List<Product> products = pb.findByPage(info.getStartRow(), info .getRowPerPage()); request.setAttribute("products", products); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("system.center")); forward = mapping.findForward("error"); } return forward; } public ActionForward logout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); session.invalidate(); forward = mapping.findForward("success"); } catch (Exception e) { e.printStackTrace(); saveErrors(request, getMessages("user.logout")); forward = mapping.findForward("error"); } return forward; } private ActionMessages getMessages(String key) { ActionMessages errors = new ActionMessages(); ActionMessage message = new ActionMessage(key); errors.add("error", message); return errors; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -