关于#java#的问题,自学中,请各位专家解答!


 <el-row slot-scope="scope">
                    <el-button type="danger"  @click.native.prevent="deleteById(scope.$index, brand)" >删除el-button>
                el-row>

<script>
    new Vue({
        el: "#app",
   // 删除
            deleteById(row){
                this.selectedid=row;
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    var _this= this;
                    //    发送ajax请求,添加数据
                    axios({
                        method:"post",
                        url:"http://localhost:8080/brand-case/brand/deleteById",
                        data:_this.selectedid
                    }).then(function (resp) {
                        if (resp.data == "success" ){
                            //    删除成功
                            //    重新查询数据
                            _this.selectAll();
                            //    弹出消息提示
                            _this.$message({
                                message:"删除成功",
                                type:"success"
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
                this.selectedid=''
            },
 data() {
            return {
  selectedid:'',
// 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                }
}
}
})

//后端代码
 删除
   public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{

//        BufferedReader reader = req.getReader();
//        String params = reader.readLine();
//
//
//
//       Brand brand = JSON.parseObject(params, Brand.class);
//            brandService.delete(brand.getId());

//       String id = req.getParameter("id");
//       System.out.println(id);
//        删除成功返回成功标志


       req.setCharacterEncoding("UTF-8");
       BufferedReader reader = req.getReader();
       String params = reader.readLine();
       System.out.println(params);
       int id  = JSON.parseObject(params, int.class);
       brandService.deleteById(id);

       resp.getWriter().write("success");



    }

代码运行一直提示我获取到的值为null

img

img

在前端的 deleteById 方法中,调用了 deleteById(scope.$index, brand),但是在后台删除方法中,获取参数的方式是通过读取请求体中的参数,即通过 BufferedReader reader = req.getReader(); 读取请求体中的数据。而在前端代码中没有传递任何请求体中的数据。因此,获取到的值为 null。

解决方法是需要将要传递的参数作为 data 属性传递给 axios 函数,例如:axios({method:"post", url:"http://localhost:8080/brand-case/brand/deleteById", data: {id: _this.selectedid}})。同时,在后台的接收方法中,可以使用 req.getParameter("id") 获取前端传递的参数。

json解析的问题,看看json是什么内容,还有字符编码问题

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/739468
  • 这篇博客也不错, 你可以看下如何避免在java中检查null语句(多种解决方案)
  • 除此之外, 这篇博客: 为什么表设计时必须把字段定义为NOT NULL并设默认值中的 查询的坑 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 如果要查询表的NULL需要使用 is nullis not null,如果直接使用=/!=/in/not inl将查询不到值

    mysql> SELECT * FROM `user`;
    +----+------+-----+---------+
    | id | name | age | address |
    +----+------+-----+---------+
    |  1 | wyf  |  32 | 合肥    |
    |  2 | xx   |  31 | 北京    |
    |  3 | yy   |  30 | 上海    |
    |  4 | zz   |  11 |         |
    |  5 | aa   |  21 | NULL    |
    +----+------+-----+---------+
    5 rows in set
    
    mysql> SELECT * FROM `user` WHERE address IS NULL;
    +----+------+-----+---------+
    | id | name | age | address |
    +----+------+-----+---------+
    |  5 | aa   |  21 | NULL    |
    +----+------+-----+---------+
    1 row in set
    
    mysql> SELECT * FROM `user` WHERE address = NULL;
    Empty set
    
    mysql>