您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
07-案例-查询所有(处理java的fastjson转json缺少字段属性问题)
发布时间:2024-12-11 21:00:24编辑:雪饮阅读()
-
像是之前了解了java转java对象为json之后,那么我们可以结合vue和axios实现之前的品牌列表以更优雅的实现
那么我这里是建立独立的brandAjax.jsp来完成,不过肯定是要先登录之后才能访问这个,因为之前有写没有登录就跳转到登录页面的内部请求转发的。
<%--
Created by IntelliJ IDEA.
User: 1
Date: 2024/12/2
Time: 15:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
window.onload=function(){
const { createApp, ref } = Vue
createApp({
setup() {
const brands = ref([])
axios.get('/zeroEightUserLoginCaseModule/ServletQueryAllAjax')
.then(function (response) {
console.log(response);
brands.value=response.data;
})
.catch(function (error) {
console.log(error);
});
return {
brands
}
}
}).mount('#app');
}
</script>
</head>
<body>
<div id="app">
<p>欢迎你,${pageContext.request.getSession().getAttribute("user").username}</p>
<a href="addBrand.jsp">添加品牌</a>
<table border="1" cellspacing="0" width="800">
<tr>
<th>ID</th>
<th>品牌名称</th>
<th>企业名称</th>
<th>排序</th>
<th>品牌介绍</th>
<th>状态</th>
<th>操作</th>
</tr>
<template v-for="(brand, index) in brands">
<tr>
<th>{{brand.id}}</th>
<th>{{brand.brandName}}</th>
<th>{{brand.companyName}}</th>
<th>{{brand.ordered}}</th>
<th>{{brand.description}}</th>
<th>
<template v-if="brand.status == 1">启用</template>
<template v-if="brand.status == 0">禁用</template>
</th>
<th>
<a :href="'/threeTierArchitectureModule/BrandUpdateById?id='+brand.id">修改</a>
</th>
</tr>
</template>
</table>
</div>
</body>
</html>
那么这些都是小case,需要注意的反而是转换为json的时候,这里发现fastjson的toJSONString默认情况下转换的json中,如果某个值在对象的属性中没有值,那么该值在json字符串中对应的字段也就不存在。所以还要传递如SerializerFeature.WriteMapNullValue作为第二个参数,将没有值的字段也显示出来,只不过作为null处理。另外一个就是默认的响应编码是iso-8859编码,这个需要与我们当前的utf-8相同才行。
所以这次的servlet实现如:
package com.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Brand;
import com.service.BrandService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/ServletQueryAllAjax")
public class ServletQueryAllAjax extends HttpServlet {
BrandService brandService=new BrandService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Brand> brands=brandService.selectAll();
String brandsJson= JSON.toJSONString(brands, SerializerFeature.WriteMapNullValue);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(brandsJson);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
关键字词:java,fastjson,json,07,案例,查询所有