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

📄 shopcartaction.java

📁 >项目名称:电脑电子商务平台 >1.运行环境JDK1.6+Oracle10g+Tomcat5.5 >2.开发工具: MyEclipse Enterprise Workbench
💻 JAVA
字号:
package omega.struts.action.shopcar;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import omega.domain.Member;
import omega.domain.Product;
import omega.domain.Shopcart;
import omega.logic.ProductLogic;
import omega.logic.ShopCarLogic;

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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class ShopCartAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		if (session.getAttribute("member") == null) {
			ActionMessages errors = new ActionMessages();
			errors.add("logic", new ActionMessage("error.loginFail"));
			saveErrors(request, errors);
			return mapping.findForward("false");
		} else {
			ProductLogic pl = new ProductLogic();
			ShopCarLogic scl = new ShopCarLogic();
			Shopcart shopcar = new Shopcart();
			Member m=(Member)session.getAttribute("member");
			String username = m.getUsername();
			/* 假如传来的productid不为NULL,插入数据到购物车表 */
			if (request.getParameter("id") != null) {
				int productid = Integer.parseInt(request.getParameter("id"));
				Product product = pl.getProductbyid(productid);

				int nextid = 1;
				int sum = scl.getCount();
				if (sum != 0) {
					Shopcart p = (Shopcart) scl.getMaxId();
					nextid = p.getId() + 1;
				}

				int count = 1;
				int saleprice = Integer.parseInt(product.getSaleprice());
				int price = count * saleprice;
				String productname = product.getName();
				shopcar.setId(nextid);
				shopcar.setOrderid(0);
				shopcar.setCount(count);
				shopcar.setProductid(productid);
				shopcar.setSaleprice(saleprice);
				shopcar.setUsername(username);
				shopcar.setCount(count);
				shopcar.setPrice(price);
				shopcar.setProductname(productname);
				scl.Insert(shopcar);
				
				/* 根据USERNAME列出购物车项目 */
				List sc = scl.getCarbyName(username);
				request.setAttribute("shopcar", sc);
			}

			
			
			return mapping.findForward("success");
		}
	}

}

⌨️ 快捷键说明

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