• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. 淺談Ajax修改購(gòu)物車的方法

        時(shí)間:2024-06-29 15:14:52 AJAX 我要投稿
        • 相關(guān)推薦

        淺談Ajax修改購(gòu)物車的方法

          今天由yjbys小編來(lái)給大家聊聊Ajax修改購(gòu)物車的方法,希望對(duì)大家有所幫助。想了解更多相關(guān)資訊請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生培訓(xùn)網(wǎng)。

          1:購(gòu)物車類的設(shè)計(jì)

          ShoppingCartItem:書的封裝,包括書名,數(shù)量,價(jià)格三個(gè)屬性,以及對(duì)應(yīng)的getter和setter方法。

          ShoppingCart:購(gòu)物車封裝類,items為 Map<String, ShoppingCartItem> ,以及加入購(gòu)物車,得到購(gòu)物車中書的總數(shù)量以及總價(jià)格三個(gè)函數(shù)。

          2:jsp加入購(gòu)物車,超鏈接中帶入書名以及價(jià)格

          <body>

          <!-- 加入span的目的是為了定位 -->

          <p id="cartstatus">

          您已經(jīng)將

          <span id="bookName"></span>加入到購(gòu)物車中,購(gòu)物車中有

          <span id="totalBookNumber"></span>本書,總價(jià)格是

          <span id="totalMoney"></span>

          </p>

          <br>

          <br>

          java

          <a

          href="${pageContext.request.contextPath}/addToCart?id=java&price=100">加入購(gòu)物車</a>

          <br>

          ajax

          <a

          href="${pageContext.request.contextPath}/addToCart?id=ajax&price=200">加入購(gòu)物車</a>

          <br>

          jquery

          <a

          href="${pageContext.request.contextPath}/addToCart?id=jquery&price=300">加入購(gòu)物車</a>

          <br>

          </body>

          <!--${pageContext.request.contextPath}獲取該項(xiàng)目的絕對(duì)路徑 -->

          3:addToCart -----servlet的設(shè)計(jì)

          步驟如下:

          1) :獲取請(qǐng)求參數(shù) id(bookName),price,是從jsp頁(yè)面中的超鏈接來(lái)獲取的

          2):在session中獲取購(gòu)物車對(duì)象,如果session屬性中沒有購(gòu)物車,則新建一個(gè)購(gòu)物車對(duì)象放置在session屬性中

          3) : 加入購(gòu)物車操作Shopping.addToCart(bookName, price);

          4):想ajax傳遞Json對(duì)象,該對(duì)象包括 :{""bookName"":"totalBookNumber","totalMoney" },若從服務(wù)器端返回json對(duì)象,則屬性名必須使用雙引號(hào)!!

          5):響應(yīng)json請(qǐng)求,response.getWriter().print(json);

          public class AddToCartServlet extends HttpServlet {

          public void doGet(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

          this.doPost(request, response);

          }

          public void doPost(HttpServletRequest request, HttpServletResponse response)

          throws ServletException, IOException {

          //1:獲取請(qǐng)求參數(shù) id(bookName),price

          String bookName =request.getParameter("id");

          int price =Integer.parseInt(request.getParameter("price"));

          //2:獲取購(gòu)物車對(duì)象,在session中

          ShoppingCart sc=(ShoppingCart) request.getSession().getAttribute("sc");

          if(sc==null){

          sc=new ShoppingCart();

          request.getSession().setAttribute("sc",sc);

          }

          //3;將點(diǎn)擊的對(duì)象加入到購(gòu)物車中

          sc.addToCart(bookName, price);

          //4:準(zhǔn)備響應(yīng)的Json對(duì)象:{""bookName"":"totalBookNumber","totalMoney" }

          //若從服務(wù)器端返回json對(duì)象,則屬性名必須使用雙引號(hào)!!

          StringBuilder sBuilder=new StringBuilder();

          sBuilder.append("{")

          .append("\"bookName\":\""+bookName+"\"")

          .append(",")

          .append("\"totalBookNumber\":\""+sc.getTotalBookNumber()+"\"")

          .append(",")

          .append("\"totalMoney\":\""+sc.getTotalMoney()+"\"")

          .append("}");

          //響應(yīng)json請(qǐng)求

          response.setContentType("text/javascript");

          response.getWriter().print(sBuilder.toString());

          }

          }

          上述中的用StringBuilder來(lái)拼接JSON字符串的方式可以借助第三方開源Jackson來(lái)簡(jiǎn)化實(shí)現(xiàn):

          String jsonStr=null;

          ObjectMapper objectMapper=new ObjectMapper();

          jsonStr=objectMapper.writeValueAsString(sc);

          4:ajax接受從服務(wù)器傳來(lái)的參數(shù){""bookName"":"totalBookNumber","totalMoney" }

          步驟:

          1):為加入購(gòu)物車這個(gè)超鏈接增加單擊響應(yīng)函數(shù),并取消默認(rèn)行為(return false)

          2):通過(guò) HTTP GET 請(qǐng)求載入 JSON 數(shù)據(jù)。$.getJSON(url, [data], [callback])

          準(zhǔn)備url.agrs,并在回調(diào)函數(shù)內(nèi)部將購(gòu)物車中的內(nèi)容顯示在Jsp頁(yè)面中。

          3):通過(guò)jquery中的hide(),show()方法,判斷是不是第一使用購(gòu)物車,如果是第一次使用,則jsp頁(yè)面不顯示購(gòu)物車。

          <head>

          <!--${pageContext.request.contextPath}獲取該項(xiàng)目的絕對(duì)路徑 -->

          <script type="text/javascript"

          src="${pageContext.request.contextPath}/scripts/jquery-1.7.2.js"></script>

          <script type="text/javascript">

          $(function(){

          var isHasCart="${sessionScope.sc==null}";

          if(isHasCart=="true"){

          $("#cartstatus").hide();//隱藏顯示的元素

          }else{

          $("#cartstatus").show(); //顯示隱藏的匹配元素

          $("#bookName").text("${sessionScope.sc.bookName}");

          $("#totalBookNumber").text("${sessionScope.sc.totalBookNumber}");

          $("#totalMoney").text("${sessionScope.sc.totalMoney}");

          }

          $("a").click(function(){

          $("#cartstatus").show();

          var url=this.href; //url屬性

          var agrs={"time":new Date()}; //時(shí)間戳

          $.getJSON(url,agrs,function(data){

          $("#bookName").text(data.bookName);

          $("#totalBookNumber").text(data.totalBookNumber);

          $("#totalMoney").text(data.totalMoney);

          });

          return false;

          });

          });

          </script>

          </head>

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【淺談Ajax修改購(gòu)物車的方法】相關(guān)文章:

        使用ajax操作JavaScript對(duì)象的方法03-08

        公文的修改方法02-03

        基于ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)的方法03-18

        淺談水星MW325R路由器密碼的修改方法03-16

        CAD修改線型的方法03-04

        CAD修改線顏色的方法03-04

        JS AJAX前臺(tái)如何給后臺(tái)類的函數(shù)傳遞參數(shù)的方法03-18

        CAD修改圖案填充的方法05-23

        手機(jī)修改ip地址方法03-08

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 亚洲日韩在线a视频在线观看 | 亚洲精品中文字幕久久久久 | 亚洲欧美日韩污在线观看 | 日韩精品乱码AV一区二区蜜桃 | 婷婷六月国产在线 | 精品久久亚洲中国一级a |

            淺談Ajax修改購(gòu)物車的方法

              今天由yjbys小編來(lái)給大家聊聊Ajax修改購(gòu)物車的方法,希望對(duì)大家有所幫助。想了解更多相關(guān)資訊請(qǐng)持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生培訓(xùn)網(wǎng)。

              1:購(gòu)物車類的設(shè)計(jì)

              ShoppingCartItem:書的封裝,包括書名,數(shù)量,價(jià)格三個(gè)屬性,以及對(duì)應(yīng)的getter和setter方法。

              ShoppingCart:購(gòu)物車封裝類,items為 Map<String, ShoppingCartItem> ,以及加入購(gòu)物車,得到購(gòu)物車中書的總數(shù)量以及總價(jià)格三個(gè)函數(shù)。

              2:jsp加入購(gòu)物車,超鏈接中帶入書名以及價(jià)格

              <body>

              <!-- 加入span的目的是為了定位 -->

              <p id="cartstatus">

              您已經(jīng)將

              <span id="bookName"></span>加入到購(gòu)物車中,購(gòu)物車中有

              <span id="totalBookNumber"></span>本書,總價(jià)格是

              <span id="totalMoney"></span>

              </p>

              <br>

              <br>

              java

              <a

              href="${pageContext.request.contextPath}/addToCart?id=java&price=100">加入購(gòu)物車</a>

              <br>

              ajax

              <a

              href="${pageContext.request.contextPath}/addToCart?id=ajax&price=200">加入購(gòu)物車</a>

              <br>

              jquery

              <a

              href="${pageContext.request.contextPath}/addToCart?id=jquery&price=300">加入購(gòu)物車</a>

              <br>

              </body>

              <!--${pageContext.request.contextPath}獲取該項(xiàng)目的絕對(duì)路徑 -->

              3:addToCart -----servlet的設(shè)計(jì)

              步驟如下:

              1) :獲取請(qǐng)求參數(shù) id(bookName),price,是從jsp頁(yè)面中的超鏈接來(lái)獲取的

              2):在session中獲取購(gòu)物車對(duì)象,如果session屬性中沒有購(gòu)物車,則新建一個(gè)購(gòu)物車對(duì)象放置在session屬性中

              3) : 加入購(gòu)物車操作Shopping.addToCart(bookName, price);

              4):想ajax傳遞Json對(duì)象,該對(duì)象包括 :{""bookName"":"totalBookNumber","totalMoney" },若從服務(wù)器端返回json對(duì)象,則屬性名必須使用雙引號(hào)!!

              5):響應(yīng)json請(qǐng)求,response.getWriter().print(json);

              public class AddToCartServlet extends HttpServlet {

              public void doGet(HttpServletRequest request, HttpServletResponse response)

              throws ServletException, IOException {

              this.doPost(request, response);

              }

              public void doPost(HttpServletRequest request, HttpServletResponse response)

              throws ServletException, IOException {

              //1:獲取請(qǐng)求參數(shù) id(bookName),price

              String bookName =request.getParameter("id");

              int price =Integer.parseInt(request.getParameter("price"));

              //2:獲取購(gòu)物車對(duì)象,在session中

              ShoppingCart sc=(ShoppingCart) request.getSession().getAttribute("sc");

              if(sc==null){

              sc=new ShoppingCart();

              request.getSession().setAttribute("sc",sc);

              }

              //3;將點(diǎn)擊的對(duì)象加入到購(gòu)物車中

              sc.addToCart(bookName, price);

              //4:準(zhǔn)備響應(yīng)的Json對(duì)象:{""bookName"":"totalBookNumber","totalMoney" }

              //若從服務(wù)器端返回json對(duì)象,則屬性名必須使用雙引號(hào)!!

              StringBuilder sBuilder=new StringBuilder();

              sBuilder.append("{")

              .append("\"bookName\":\""+bookName+"\"")

              .append(",")

              .append("\"totalBookNumber\":\""+sc.getTotalBookNumber()+"\"")

              .append(",")

              .append("\"totalMoney\":\""+sc.getTotalMoney()+"\"")

              .append("}");

              //響應(yīng)json請(qǐng)求

              response.setContentType("text/javascript");

              response.getWriter().print(sBuilder.toString());

              }

              }

              上述中的用StringBuilder來(lái)拼接JSON字符串的方式可以借助第三方開源Jackson來(lái)簡(jiǎn)化實(shí)現(xiàn):

              String jsonStr=null;

              ObjectMapper objectMapper=new ObjectMapper();

              jsonStr=objectMapper.writeValueAsString(sc);

              4:ajax接受從服務(wù)器傳來(lái)的參數(shù){""bookName"":"totalBookNumber","totalMoney" }

              步驟:

              1):為加入購(gòu)物車這個(gè)超鏈接增加單擊響應(yīng)函數(shù),并取消默認(rèn)行為(return false)

              2):通過(guò) HTTP GET 請(qǐng)求載入 JSON 數(shù)據(jù)。$.getJSON(url, [data], [callback])

              準(zhǔn)備url.agrs,并在回調(diào)函數(shù)內(nèi)部將購(gòu)物車中的內(nèi)容顯示在Jsp頁(yè)面中。

              3):通過(guò)jquery中的hide(),show()方法,判斷是不是第一使用購(gòu)物車,如果是第一次使用,則jsp頁(yè)面不顯示購(gòu)物車。

              <head>

              <!--${pageContext.request.contextPath}獲取該項(xiàng)目的絕對(duì)路徑 -->

              <script type="text/javascript"

              src="${pageContext.request.contextPath}/scripts/jquery-1.7.2.js"></script>

              <script type="text/javascript">

              $(function(){

              var isHasCart="${sessionScope.sc==null}";

              if(isHasCart=="true"){

              $("#cartstatus").hide();//隱藏顯示的元素

              }else{

              $("#cartstatus").show(); //顯示隱藏的匹配元素

              $("#bookName").text("${sessionScope.sc.bookName}");

              $("#totalBookNumber").text("${sessionScope.sc.totalBookNumber}");

              $("#totalMoney").text("${sessionScope.sc.totalMoney}");

              }

              $("a").click(function(){

              $("#cartstatus").show();

              var url=this.href; //url屬性

              var agrs={"time":new Date()}; //時(shí)間戳

              $.getJSON(url,agrs,function(data){

              $("#bookName").text(data.bookName);

              $("#totalBookNumber").text(data.totalBookNumber);

              $("#totalMoney").text(data.totalMoney);

              });

              return false;

              });

              });

              </script>

              </head>