📄 productaction.java
字号:
package com.tarena.shoppingcart.action;import java.util.ArrayList;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.actions.MappingDispatchAction;import com.tarena.shoppingcart.dao.ShoppingCartHibernateImpl;import com.tarena.shoppingcart.entity.*;import com.tarena.shoppingcart.entity.User;public class ProductAction extends MappingDispatchAction{ private ShoppingCartHibernateImpl service = new ShoppingCartHibernateImpl(); public ActionForward manager_listProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward=null; ShoppingCartHibernateImpl service=new ShoppingCartHibernateImpl(); try{ List list=(List)service.listProducts(); Cart cart=new Cart(); if(list!=null){ HttpSession session=request.getSession(false); if(session.getAttribute("cart")==null){ session.setAttribute("cart",cart); } session.setAttribute("products",list); forward=mapping.findForward("manager_list"); } }catch(Exception e){ e.printStackTrace(); forward=mapping.findForward("error"); } return forward; } public ActionForward deleteSelectedProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(false); Integer id=Integer.valueOf(request.getParameter("productid")); service.deleteProductById(id); forward = mapping.findForward("listProduct"); } catch (Exception e) { e.printStackTrace(); forward = mapping.findForward("error"); } return forward; } public ActionForward toModifyProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(false); Integer id=Integer.valueOf(request.getParameter("productid")); Product product = service.findProductById(id); session.setAttribute("selectedProduct", product); forward = mapping.findForward("toModifyProduct"); } catch (Exception e) { e.printStackTrace(); forward = mapping.findForward("error"); } return forward; } public ActionForward modifyProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(false); Integer id= Integer.valueOf(request.getParameter("id")); String name = request.getParameter("name"); String description = request.getParameter("description"); double price=Double.parseDouble(request.getParameter("price")); Product product = new Product(); product.setName(name); product.setPrice(price); product.setDescription(description); product.setId(id); service.updateProduct(product); forward = mapping.findForward("manager_list"); } catch (Exception e) { e.printStackTrace(); 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(false); String name = request.getParameter("name"); String description = request.getParameter("description"); double price=Double.parseDouble(request.getParameter("price")); Product product = new Product(); product.setName(name); product.setPrice(price); product.setDescription(description); service.saveProduct(product); forward = mapping.findForward("manager_list"); } catch (Exception e) { e.printStackTrace(); forward = mapping.findForward("error"); } return forward; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -