SSM后端 controller 向前端表格传递数据

前端JS:

$.ajax({
            url: "/generalManager/queryProductInfo", //后台url
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(product),
            success: function (data) {
                $("#btn_clear").click();//情空查询表单
                //成功,回调函数
                var json = JSON.stringify(data);//格式化Json数据
                var a = eval(json);//转json字符串
                var str = '';
                $('#product_tbody tr').empty();//表格去重
                $.each(a, function (index, item) {
                    if (0 == index) {
                        str += '';
                    } else if (1 == index) {
                        str += '';
                    } else if (2 == index) {
                        str += '';
                    } else if (3 == index) {
                        str += '';
                    } else if (4 == index) {
                        str += '';
                    } else {
                    }
                    str += '' + (index + 1) + ''
                        + '' + item.categoryName + ''
                        + '' + item.productName + ''
                        + '' + item.supplierCompany + ''
                        + '' + item.productNum + ''
                        + ''
                        + ''
                        + ''
                        + ''
                        + ''
                        + '';
                });
                $('#product_tbody').append(str);//注意放在遍历函数外面
            },
            error: function (er) {          //失败,回调函数
   
            }
        });

后台controller:

    @RequestMapping(value = "/queryProductInfo", method = RequestMethod.POST)
    @ResponseBody
    public void queryProductInfo(@RequestBody(required = false) Product product, HttpServletResponse response) throws UnsupportedEncodingException {
        SetEncoding.getResponse(response);//设置utf-8编码

//模糊查询
        if (product.getProductName().isEmpty()){product.setProductName(null);}
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria1 = example.createCriteria();

        if (product.getCategoryName() != "" && product.getCategoryName()!=null) {
            criteria1.andCategoryNameEqualTo(product.getCategoryName());
        }

        if (product.getSupplierCompany() !="" && product.getSupplierCompany()!=null){
            criteria1.andSupplierCompanyEqualTo(product.getSupplierCompany());
        }

        if (product.getProductName() != "" && product.getProductName()!=null){
            criteria1.andProductNameEqualTo(product.getProductName());
"+product.getProductName());
        }

        List list =  productService.selectByExample(example);
        System.out.println("SIZE------->  "+list.size());

        String productList = JSON.toJSONString(list);

        //  将后台信息传至前台
        try {
            PrintWriter out = response.getWriter();

            out.println(productList);
            //   System.out.println(categoryList);
            out.flush();
            out.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }

jsp页面:

	

商品信息

编号 类别 商品名 供应商 商品数量 操作

效果:

SSM后端 controller 向前端表格传递数据_第1张图片

你可能感兴趣的:(JS,SSM)