📄 useraction.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.actions.MappingDispatchAction;import com.tarena.shoppingcar.biz.OrderBiz;import com.tarena.shoppingcar.biz.UserBiz;import com.tarena.shoppingcar.entity.Order;import com.tarena.shoppingcar.entity.User;import com.tarena.shoppingcar.form.UserForm;public class UserAction extends MappingDispatchAction { private UserBiz ub = new UserBiz(); private OrderBiz ob = new OrderBiz();// public ActionForward add(ActionMapping mapping, ActionForm form,// HttpServletRequest request, HttpServletResponse response)// throws Exception {// HttpSession session = request.getSession();// User u = (User) session.getAttribute("user");// if (u == null) {// UserForm userForm = (UserForm) form;// User user = userForm.getUser();// ub.add(user);// session.setAttribute("user", user);// return mapping.findForward("add");// } else {// return mapping.findForward("hasadded");// }// } public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserForm userForm = (UserForm) form; User user = userForm.getUser(); if(ub.nameIsExist(user.getUserName())){ throw new NameExistException(); }else { ub.add(user); HttpSession session = request.getSession(); session.setAttribute("user", user); return mapping.findForward("add"); } } // public ActionForward orderList(ActionMapping mapping, ActionForm form, // HttpServletRequest request, HttpServletResponse response) // throws Exception { // ActionForward forward = null; // try{ // HttpSession session = request.getSession(); // User user = (User)session.getAttribute("user"); // List<Order> orders = ob.getOrders(user); // //ids 该用户的所有定单号 // // int[] ids = ob.getIds(user); // // Map<Integer,List<Product>> pp = new HashMap<Integer,List<Product>>(); // // for(int i : ids){ // // List<Item> items = ob.getItems(i); // // List<Product> products = getProducts(items); // // pp.put(products.size(), products); // // } // // Map<List<Item>,Order> oo = new HashMap<List<Item>,Order>(); // for(Order order : orders){ // List<Item> items = ob.getItems(order.getId()); // // List<Product> products = getProducts(items); // oo.put(items, order); // } // // request.setAttribute("orders", orders); // request.setAttribute("oo", oo); // forward = mapping.findForward("list"); // }catch(Exception e){ // e.printStackTrace(); // forward = mapping.findForward("error"); // } // return forward; // } // public List<Product> getProducts(List<Item> items) { // List<Product> products = new ArrayList<Product>(); // for(Item item : items){ // Product product = ob.getProduct(item); // products.add(product); // } // return products; // } public ActionForward orderList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); List<Order> orders = ob.getOrders(user); request.setAttribute("orders", orders); forward = mapping.findForward("list"); } catch (Exception e) { e.printStackTrace(); forward = mapping.findForward("error"); } return forward; } public ActionForward mlogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward forward = null; try { String mName = request.getParameter("name"); String mPassword = request.getParameter("password"); if (mName.equals("root") && mPassword.equals("11")) { forward = mapping.findForward("success"); }else{ forward = mapping.findForward("login"); } } catch (Exception e) { e.printStackTrace(); forward = mapping.findForward("error"); } return forward; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -