⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 productaction.java

📁 网上商店。提供网上销售
💻 JAVA
字号:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package ydh.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

import ydh.business.CatalogManager;
import ydh.business.ProductManager;
import ydh.catalog.Catalog;
import ydh.catalog.CatalogDAO;
import ydh.product.Product;
import ydh.product.Shopbike;


public class ProductAction extends Action {
	private ProductManager pm;
	private CatalogManager cm;

	/*
	 * Generated Methods
	 */

	public ProductManager getPm() {
		return pm;
	}

	public void setPm(ProductManager pm) {
		this.pm = pm;
	}
	
	 private   ServletConfig   config; 
	 public   void   init(ServletConfig   config)    
     {   
         this.config   =   config;   
     }   

	public CatalogManager getCm() {
		return cm;
	}

	public void setCm(CatalogManager cm) {
		this.cm = cm;
	}

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if (action == null || action.equals("")) {
			return mapping.findForward("fail");
		} else if (action.equals("product")) {
			try {
				return product(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (action.equals("addbike")) {
			try {
				return addbike(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (action.equals("changquantity")) {
			try {
				return changquantity(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("deleteonebike")) {
			try {
				return deleteonebike(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("deletebike")) {
			try {
				return deletebike(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("newgoods")) {
			try {
				return newgoods(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("salegoods")) {
			try {
				return salegoods(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("search")) {
			try {
				return search(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("addproduct1")) {
			try {
				return addproduct(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("update")) {
			try {
				return updateproduct(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("delete")) {
			try {
				return deleteproduct(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (action.equals("selectone")) {
			try {
				return selectone(mapping, form, request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return mapping.findForward("fail");

	}

	public ActionForward product(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String catalogid=request.getParameter("catalogid");
		String msg=request.getParameter("msg");
		int nowPage=request.getParameter("nowPage")==null?(1):Integer.valueOf(request.getParameter("nowPage").toString());
		int maxResults=0;
		int firstRow=0;
		int Count=0; List o=null;
		if(catalogid==null){
			maxResults=9;
			firstRow=maxResults*(nowPage-1);
		 o=getPm().findAll(firstRow, maxResults);		 
		 Count=getPm().findAll().size();
            if(msg==null){
			List catalog = getCm().findAll();
			request.getSession().setAttribute("catalog", catalog);}
		}
		else {
			maxResults=15;
			firstRow=maxResults*(nowPage-1);
			 o=getPm().findAll(firstRow, maxResults,catalogid);		 
			 Count=getPm().findAll(catalogid).size();
		}
		 int b=(int)(Count/(maxResults));
		 int countRow=(Count%maxResults)==0?(Count/maxResults):(b+1);
		 request.setAttribute("countRow",countRow);
		 request.setAttribute("Count",Count);
		 request.setAttribute("nowPage",nowPage);
        Iterator i=o.iterator();
        while(i.hasNext()){
        	Product p=(Product)i.next();
        	System.out.println("catalog:"+p.getCatalog());
        	String catalogname=p.getCatalog().getName();
        	System.out.println("商品目录为:"+catalogname);
        }
		request.getSession().setAttribute("product", o);
 
		
		
		return mapping.findForward("success");
	}
	public ActionForward newgoods(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int nowPage=request.getParameter("nowPage")==null?(1):Integer.valueOf(request.getParameter("nowPage").toString());
		int maxResults=9;
		int firstRow=maxResults*(nowPage-1);
		 List o=getPm().newgoods(firstRow, maxResults);		 
		 int Count=getPm().newgoods().size();
		 int b=(int)(Count/(maxResults));
		 int countRow=(Count%maxResults)==0?(Count/maxResults):(b+1);
		 request.setAttribute("countRow",countRow);
		 request.setAttribute("Count",Count);
		 request.setAttribute("nowPage",nowPage);
		request.getSession().setAttribute("product", o);
		List catalog = getCm().findAll();
		request.setAttribute("catalog", catalog);
		return mapping.findForward("success");
	}
	public ActionForward salegoods(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int nowPage=request.getParameter("nowPage")==null?(1):Integer.valueOf(request.getParameter("nowPage").toString());
		int maxResults=9;
		int firstRow=maxResults*(nowPage-1);
		 List o=getPm().salegoods(firstRow, maxResults);		 
		 int Count=getPm().salegoods().size();
		 int b=(int)(Count/(maxResults));
		 int countRow=(Count%maxResults)==0?(Count/maxResults):(b+1);
		 request.setAttribute("countRow",countRow);
		 request.setAttribute("Count",Count);
		 request.setAttribute("nowPage",nowPage);
		request.getSession().setAttribute("product", o);
		List catalog = getCm().findAll();
		request.setAttribute("catalog", catalog);
		return mapping.findForward("success");
	}

	public ActionForward addbike(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = (String) request.getParameter("id");
		System.out.println("id=" + id);
		List l = getPm().findid(id);
		Iterator i = l.iterator();
		Product p;
		Shopbike sb = (Shopbike) request.getSession().getAttribute("shopbike");
		if (sb == null)
			sb = new Shopbike();
		while (i.hasNext()) {
			 p= (Product) i.next();
			sb.addbike(p);
			request.getSession().setAttribute("shopbike", sb);
		}
		return mapping.findForward("success");
	}

	public ActionForward changquantity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String productid = request.getParameter("productid");
		int amount = Integer.parseInt((request.getParameter("amount")));
		HttpSession session = request.getSession();
		Shopbike sb = null;
		synchronized(session) {
			sb= (Shopbike) session.getAttribute("shopbike");
			if(sb== null) {
				sb = new Shopbike();
			}
		}
		
		sb.changeQuntity(new Integer(productid), amount);
	
		return mapping.findForward("success");
	}
	
	public ActionForward deleteonebike(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String productid = request.getParameter("productid");
		HttpSession session = request.getSession();
		Shopbike sb = null;
		synchronized(session) {
			sb= (Shopbike) session.getAttribute("shopbike");
			if(sb== null) {
				sb = new Shopbike();
			}
		}		 
			List l = getPm().findid(productid);
			Iterator i = l.iterator();
			while (i.hasNext()) {
				Product p = (Product) i.next();
				sb.removeone(p.getId());
				session.setAttribute("shopbike", sb);
			}

	
		return mapping.findForward("success");
	}
	
	public ActionForward deletebike(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		Shopbike sb = null;
		synchronized(session) {
			sb= (Shopbike) session.getAttribute("shopbike");
			if(sb== null) {
				sb = new Shopbike();
			}
		}
			sb.removeall();
			session.setAttribute("shopbike", sb);

	
		return mapping.findForward("success");
	}
    
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        String name=request.getParameter("search");
        List o=getPm().search(name);
        request.getSession().setAttribute("product", o);
		return mapping.findForward("success");
	}
	
	public ActionForward addproduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SmartUpload s=new SmartUpload();
		s.initialize(this.getServlet().getServletConfig(),request,response);
		s.upload();	
		Request myrequest=s.getRequest();
		Product p=new Product();
		Float price=myrequest.getParameter("price")==null?(0):Float.valueOf(myrequest.getParameter("price"));
		p.setProductname(myrequest.getParameter("productname"));
		p.setPrice(price);
		p.setForm(myrequest.getParameter("form"));
		p.setDescription(myrequest.getParameter("description"));
		String catalogid=myrequest.getParameter("catalog");
		List l=getCm().findname(catalogid);
		Iterator it = l.iterator();
		while (it.hasNext()) {
			Catalog c = (Catalog) it.next();
			p.setCatalog(c);
		}	
		  p.setImg("1.jpg");
			
		for(int i=0;i<s.getFiles().getCount();i++){
			  com.jspsmart.upload.File myFile=s.getFiles().getFile(i);
			  String savepath=request.getSession().getServletContext().getRealPath("/")+"img";	
			  
			  if(!myFile.isMissing())
			  {myFile.saveAs(savepath+"\\"+myFile.getFileName(),s.SAVE_PHYSICAL);
			  p.setImg(myFile.getFileName());
			  }
			  }   
		getPm().save(p);
        request.getSession().setAttribute("msg","添加成功");
		return mapping.findForward("success");
	}
	
	public ActionForward updateproduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Float price=request.getParameter("price")==null?(0):Float.valueOf(request.getParameter("price"));
		String name=request.getParameter("id");
        List o=getPm().findid(name);
        Iterator i = o.iterator();
		while (i.hasNext()) {
			Product p = (Product) i.next();
			p.setProductname(request.getParameter("productname"));
			p.setPrice(price);
			p.setForm(request.getParameter("form"));
			p.setDescription(request.getParameter("description"));
           getPm().update(p);
		}
		return mapping.findForward("success");
	}
	public ActionForward deleteproduct(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        String name=request.getParameter("productid");
        List o=getPm().findid(name);
        Iterator i = o.iterator();
		while (i.hasNext()) {
			Product p = (Product) i.next();
           getPm().delete(p);
		}
		return mapping.findForward("success");
	}
	
	public ActionForward selectone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        String name=request.getParameter("productid");
        List o=getPm().findid(name);
        Iterator i = o.iterator();
		while (i.hasNext()) {
			Product p = (Product) i.next();
           request.setAttribute("product",p);
		}
		return mapping.findForward("success");
	}
	
	
	
	
	
	
	
	
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -