1. 后端数据准备

主要是准备商品名称和数量信息
image.png

  1. package com.dyq.bean;
  2. public class Commodity {
  3. private String name;
  4. private Integer num;
  5. private Double price;
  6. public Commodity() {
  7. }
  8. public Commodity(String name, Integer num, Double price) {
  9. this.name = name;
  10. this.num = num;
  11. this.price = price;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Integer getNum() {
  20. return num;
  21. }
  22. public void setNum(Integer num) {
  23. this.num = num;
  24. }
  25. public Double getPrice() {
  26. return price;
  27. }
  28. public void setPrice(Double price) {
  29. this.price = price;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Commodity{" +
  34. "name='" + name + '\'' +
  35. ", num=" + num +
  36. ", price=" + price +
  37. '}';
  38. }
  39. }
  1. package com.dyq.bean;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class Shop {
  5. private static Map<String, Object> commodities;
  6. static {
  7. commodities = new HashMap<>();
  8. commodities.put("可乐", 0);
  9. commodities.put("薯条", 0);
  10. commodities.put("面包", 0);
  11. commodities.put("矿泉水", 0);
  12. }
  13. public static Integer getCommodity(String name) {
  14. return (Integer) commodities.get(name);
  15. }
  16. public static void setCommodity(String name, Integer num) {
  17. commodities.put(name, num);
  18. }
  19. public static Map<String, Object> getCommodities() {
  20. return commodities;
  21. }
  22. }

2. 后端接口准备(servlet)

image.png

  1. package com.dyq.servlet;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.dyq.bean.Shop;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.Map;
  12. @WebServlet("/shop")
  13. public class shop extends HttpServlet {
  14. //由于get/post只是请求实现的不同方式,所以可以相互调用,业务逻辑都一样
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  17. req.setCharacterEncoding("UTF-8");
  18. resp.setCharacterEncoding("UTF-8");
  19. JSONObject json = new JSONObject(Shop.getCommodities());
  20. resp.getWriter().write(json.toJSONString());
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  24. super.doPost(req, resp);
  25. }
  26. }
  1. 注意@WebServlet(“”)的作用
    • @WebServlet(“/shop”)
  2. 导入fastjson
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.75</version>
  5. </dependency>
  1. fastjson的使用
    1. Map转 json
    2. json转 String
  2. resp.getWriter().write()的作用

3. 前端页面准备

  1. <script>
  2. $(function(){
  3. init();
  4. });
  5. /** init 函数用来初始化商品 数目 */
  6. function init() {
  7. $.ajax({
  8. type:'get',
  9. url:'/dyq/shop'
  10. }).then(function (result) {
  11. let res = JSON.parse(result);
  12. $("#table").append("<tr><td>商品名称</td><td>商品数量</td></tr>")
  13. for (let key in res) {
  14. $("#table").append("<tr><td>"+key+"</td><td>"+res[key]+"</td></tr>")
  15. }
  16. })
  17. }
  18. function edit() {
  19. $.ajax({
  20. type:'get',
  21. url:'dyq/edit',
  22. data:{
  23. shopName:document.getElementById("shopName").value,
  24. number:$("#number").val()
  25. }
  26. }).then(function (res) {
  27. console.log(res)
  28. })
  29. }
  30. </script>