image.png

序列化和反序列话

:::info 将集合转换为JSON数据 是:序列化(对象序列化(转换)为数据)
反序列化(将数据转换为对象) — IO流中的输入流就是反序列化,将数据转换为对象输入到内存中 :::

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <a href="addBrand.html"><input type="button" value="新增"></a><br>
  9. <hr>
  10. <table id="brandTable" border="1" cellspacing="0" width="100%">
  11. <tr>
  12. <th>序号</th>
  13. <th>品牌名称</th>
  14. <th>企业名称</th>
  15. <th>排序</th>
  16. <th>品牌介绍</th>
  17. <th>状态</th>
  18. <th>操作</th>
  19. </tr>
  20. <tr align="center">
  21. <td>1</td>
  22. <td>三只松鼠</td>
  23. <td>三只松鼠</td>
  24. <td>100</td>
  25. <td>三只松鼠,好吃不上火</td>
  26. <td>启用</td>
  27. <td><a href="#">修改</a> <a href="#">删除</a></td>
  28. </tr>
  29. <tr align="center">
  30. <td>2</td>
  31. <td>优衣库</td>
  32. <td>优衣库</td>
  33. <td>10</td>
  34. <td>优衣库,服适人生</td>
  35. <td>禁用</td>
  36. <td><a href="#">修改</a> <a href="#">删除</a></td>
  37. </tr>
  38. <tr align="center">
  39. <td>3</td>
  40. <td>小米</td>
  41. <td>小米科技有限公司</td>
  42. <td>1000</td>
  43. <td>为发烧而生</td>
  44. <td>启用</td>
  45. <td><a href="#">修改</a> <a href="#">删除</a></td>
  46. </tr>
  47. </table>
  48. <script src="js/axios-0.18.0.js"></script>
  49. <script>
  50. // 1. 当页面加载完成后,发送ajax请求
  51. window.onload = function () {
  52. // 2. 发送ajax请求
  53. axios({
  54. method:"get",
  55. url:"http://localhost:8080/brand-demo2/selectAllServlet"
  56. }).then(function (resp) {
  57. // 获取数据 获取的是json类型的集合数据
  58. let brands = resp.data;
  59. let tableData = "<tr>\n" +
  60. " <th>序号</th>\n" +
  61. " <th>品牌名称</th>\n" +
  62. " <th>企业名称</th>\n" +
  63. " <th>排序</th>\n" +
  64. " <th>品牌介绍</th>\n" +
  65. " <th>状态</th>\n" +
  66. " <th>操作</th>\n" +
  67. " </tr>";
  68. // 遍历该集合
  69. for (let i = 0; i < brands.length; i++) {
  70. let brand = brands[i];
  71. tableData = "<tr align=\"center\">\n" +
  72. " <td>"+ (i + 1) +"</td>\n" +
  73. " <td>"+brand.brandName+"</td>\n" +
  74. " <td>"+brand.companyName+"</td>\n" +
  75. " <td>"+brand.ordered+"</td>\n" +
  76. " <td>"+brand.description+"</td>\n" +
  77. " <td>"+brand.status+"</td>\n" +
  78. "\n" +
  79. " <td><a href=\"#\">修改</a> <a href=\"#\">删除</a></td>\n" +
  80. " </tr>"
  81. }
  82. // 设置表格数据
  83. document.getElementById("brandTable").innerHTML = tableData;
  84. })
  85. }
  86. </script>
  87. </body>
  88. </html>

后端代码

  1. package com.itheima.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.itheima.pojo.Brand;
  4. import com.itheima.service.BrandService;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. import java.util.List;
  10. @WebServlet("/selectAllServlet")
  11. public class SelectAllServlet extends HttpServlet {
  12. private BrandService brandService = new BrandService();
  13. @Override
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. // 1. 调用Service查询
  16. List<Brand> brands = brandService.selectAll();
  17. // 2. 将集合转换为JSON数据(JSON数据都是字符串类型) 序列化(对象序列化为数据) 反序列化(将数据转换为对象)
  18. String jsonString = JSON.toJSONString(brands);
  19. // 3. 响应数据
  20. response.setContentType("text/json;charset=utf-8");
  21. response.getWriter().write(jsonString);
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. this.doGet(request, response);
  26. }
  27. }