Java之Struts——简单的实现购物车

首先创建一个工程文件 web project
需要以下的文件:
action 动作执行的包
bean 存储实体的包
Struts配置文件 Struts.xml

首先构建实体, 一个产品类的 Id 名字 类实现 序列化结构

序列化:指堆内存中的java对象数据,通过某种方式把对存储到磁盘文件中,或者传递给其他网络节点(网络传输)。这个过程称为序列化,通常是指将数据结构或对象转化成二进制的过程。
作用为
① 想把内存中的对象保存到一个文件中或者数据库中时候;
② 想用套接字在网络上传送对象的时候;
③ 想通过RMI传输对象的时候

本文是将数据存到session中

添加 get set 方法


public class Product implements Serializable {
	private Integer pid;
	private String pname;

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	private float price;
	private float num;
	private float acount;

	public Product() {
		// TODO Auto-generated constructor stub
	}

	public Product(Integer pid, String name, float price, float num,
			float acount) {
		super();
		this.pid = pid;

		this.price = price;
		this.num = num;
		this.acount = acount;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public float getNum() {
		return num;
	}

	public void setNum(float num) {
		this.num = num;
	}

	public float getAcount() {// 在Get Acount 里面 就直接计算结果值
		acount = price * num;
		return acount;
	}

	public void setAcount(float acount) {
		this.acount = acount;
	}

}

而后 创建action 类 实现功能具体实现

  1. 首先是创建对象、 创建对象索引节点、创建在框架中跳转的路径 创建一个 session
  2. 具体方法
  3. HttpSession session = ServletActionContext.getRequest();.getSession;
  4. 之后添加 几个对象的 get set 的私有方法 ;
package com.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.bean.Product;

public class ProductAction {
	private Product pd;
	private String path; // 转向当中所需要的 路径
	private int index; // 获得当前 物品的索引位置
	private HttpSession session = ServletActionContext.getRequest()
			.getSession();

	public Product getPd() {
		return pd;
	}

	public void setPd(Product pd) {
		this.pd = pd;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String add() {
		// 得到一个sesion 对象
		// 获取商品的集合
		List<Product> lspd = (List<Product>) session.getAttribute("lspd");
		// 拿到以后 需要做一个判断 判断是否为空 ;当第一个对象为空的时候 就new 一个新的
		if (lspd == null) {
			lspd = new ArrayList<Product>();
		}
		lspd.add(pd);
		if (lspd.add(pd)) {
			session.setAttribute("lspd", lspd);
			path = "findAll_Product";
			return "ok";
		}

		return "fail";
	}

	public String update() {
		List<Product> lspd = (List<Product>) session.getAttribute("lspd");
		// 拿到以后 需要做一个判断 判断是否为空 ;当第一个对象为空的时候 就new 一个新的
		if (lspd != null) {
			lspd.set(index, pd);
			// 把索引位置的對象  換成新的对象 
			path = "findAll_Product";
			return "ok";
		}
		return "fail";
	}

	public String delByIndex() {
		List<Product> lspd = (List<Product>) session.getAttribute("lspd");
		// 拿到以后 需要做一个判断 判断是否为空 ;当第一个对象为空的时候 就new 一个新的
		if (lspd != null) {
			lspd.remove(index);
			//删除结束后需要重新设置一遍 session 的值 
			session.setAttribute("lspd", lspd); // 因为发生了变化 所以需要重新 在 session 中 记录一次 
			// 把索引位置的對象  換成新的对象 
			path = "findAll_Product";
			return "ok";
		}
		return "fail";
	}

	public String findByIndex() {
		List<Product> lspd = (List<Product>) session.getAttribute("lspd");
		Product oldpd = lspd.get(index); 
		// 如果 不为空的话 , 也就是找到了你需要寻找的索引节点的 物品 此时 
		// 就把这个 找到的 物品 放在修改界面里  
		if(oldpd != null){
			session.setAttribute("oldpd", oldpd);
			path = "update.jsp";
			return "ok";
		}
		
		return "fail";
	}

	public String findAll() {
		List<Product> lspd = (List<Product>) session.getAttribute("lspd");
		float sum = 0.0f;
		if(lspd != null){
			for (Product pd : lspd) {
				sum+= pd.getAcount();
			}
			session.setAttribute("sum", sum);
			path = "list.jsp";
			return "ok";
		}
		return "fail";
	}

}

可以简单的实现 一个简略购物车的跳转 。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值