简单的购物车应用案例 相关代码2

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
   
    <title>购买球类页面</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
     <%
    request.setCharacterEncoding("utf-8");
    if(request.getParameter("b1")!=null)
    session.setAttribute("s4", request.getParameter("b1"));
     if(request.getParameter("b2")!=null)
    session.setAttribute("s5", request.getParameter("b2")) ;
    if(request.getParameter("b3")!=null)
    session.setAttribute("s6", request.getParameter("b3"));
   
     %>
     各类球大甩卖,一律八块:<br>
     <form method="post" action="b.jsp">
     <p>
     <input type="checkbox" name="b1" value="篮球">篮球&nbsp;
     <input type="checkbox" name="b2" value="足球">足球&nbsp;
     <input type="checkbox" name="b3" value="排球">排球
     </p>
     <p>
     <input type="submit" value="提交" name="x1">
     <a href="a.jsp">买点别的</a>&nbsp;
    
     <a href="c.jsp">查看购物车</a>
     </p>
    
     </form>
  </body>
</html>

转载于:https://www.cnblogs.com/www-hsy-com/p/7663680.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vuex 是一个专为 Vue.js 应用程序设计的状态管理模式,它提供了一种集中存储和管理应用状态的方式,特别适合处理像购物车这样的共享组件状态。下面是一个简单的 Vue.js 和 Vuex 购物车案例代码概览: ```javascript // store/index.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { cart: [], // 存放商品列表 totalPrice: 0, // 总价格 }, mutations: { addItem(item) { this.cart.push(item) this.totalPrice += item.price }, removeItem(index) { this.cart.splice(index, 1) this.totalPrice -= this.cart[index].price }, updateItem(index, updatedItem) { // 更新指定商品信息 this.cart[index] = updatedItem // 如果价格变化,更新总价 if (updatedItem.price !== this.cart[index].price) { this.totalPrice = this.cart.reduce((sum, product) => sum + product.price, 0) } }, clearCart() { this.cart.length = 0 this.totalPrice = 0 }, }, actions: { addToCart({ commit }, item) { commit('addItem', item) }, removeFromCart({ commit }, index) { commit('removeItem', index) }, // 更多动作(如更新商品或清空购物车) }, getters: { getCartItems(state) { return state.cart }, getTotalPrice(state) { return state.totalPrice }, }, }) ``` 在这个例子中,`store/index.js` 文件定义了购物车的状态、更改这些状态的方法(mutations)、与状态交互的动作(actions)以及用于读取状态的 getter。 在组件中,你可以这样使用这个状态: ```html <template> <div> <ul> <li v-for="(item, index) in cart" :key="index"> {{ item.name }} - {{ item.price }} <button @click="removeFromCart(index)">Remove</button> </li> </ul> <p>Total price: {{ totalPrice }}</p> <button @click="addToCart({ name: 'New Item', price: 10 })">Add to Cart</button> </div> </template> <script> import { mapState, mapActions } from 'vuex' export default { computed: { ...mapState(['getCartItems', 'getTotalPrice']), // 从 store 中获取数据 }, methods: { ...mapActions(['addToCart', 'removeFromCart']), // 调用 action }, } </script> ``` 以上代码展示了一个基础的购物车功能,实际项目中可能还需要添加错误处理、分页、商品详情等复杂功能。相关问题: 1. 在Vue中,为什么要使用Vuex来管理购物车? 2. getters在Vuex中的作用是什么? 3. 这段代码如何处理购物车中的商品数量变化和价格更新?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值