birthday.369
2021-08-29 20:17
采纳率: 100%
浏览 47
已结题

springMVC的ModelAndView数据要怎么传进layui的呀

我试过加@ResponseBody注解转换json数据,但是前台layui数据显示 “请求异常,错误提示:error” ,如果不转json数据的话layui是不是就接收不到list循环出来的数据了?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import susu.entity.Commodities;
import susu.service.CommoditisService;

import java.util.List;

@Controller
public class CommoditisController {
    @Autowired
    private CommoditisService service;

    @ResponseBody
    @RequestMapping(value = "/stock_supplier.do", method = RequestMethod.POST)
    public ModelAndView select_commoditis(){
        ModelAndView mv = new ModelAndView();
        List<Commodities> list = service.select_commodities(1);
        for (Commodities c : list) {
            System.out.println(c.getCdsid()+"\t"+c.getCdsname()+"\t"+c.getCdsnum());
            mv.addObject("cdsid",c.getCdsid());
            mv.addObject("cdsname",c.getCdsname());
            mv.addObject("cdsurl",c.getCdsurl());
            mv.addObject("cdsnum",c.getCdsnum());
            mv.addObject("catid",c.getCatid());
            mv.addObject("catname",c.getCategories().getCatname());
            System.out.println(mv);
            mv.setViewName("stock_supplier");
        }
        System.out.println("数据查询成功");
        return mv;
    }

}

此时控制台打印的数据是这样的:

1 风扇 风不大
ModelAndView: materialized View is [null]; model is {cdsid=1, cdsname=风扇, cdsurl=z1.png, cdsnum=风不大, catid=1, catname=植物}
2 风扇plus 风很凉快
ModelAndView: reference to view with name 'stock_supplier'; model is {cdsid=2, cdsname=风扇plus, cdsurl=z2.png, cdsnum=风很凉快, catid=1, catname=植物}

前台的layui:

img

layui的部分代码:

<script>
        //加载模块
        layui.use(['layer','laypage','table','form','util'], function() {
            var cxt = '${pageContext.request.contextPath}';
            //得到各种内置组件
            var layer = layui.layer //弹层
                , laypage = layui.laypage //分页
                , table = layui.table //表格
                , form = layui.form
                , util = layui.util


            //执行一个 table 实例
            table.render({
                elem: '#demo'
                ,id: 'tableId'
                ,height: 420
                ,method: 'post'
                // ,toolbar: '#toolbarDemo'
                // ,dataType: "json"
                // ,contentType: "application/json"
                ,url: 'stock_supplier.do' //数据接口
                ,parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": res.data.item //解析数据列表
                    };
                }
                ,title: '供应商'
                ,response: {
                    statusName: 'code' //规定数据状态的字段名称,默认:code
                    ,statusCode: 200 //规定成功的状态码,默认:0
                    ,msgName: 'msg' //规定状态信息的字段名称,默认:msg
                    ,countName: 'count' //规定数据总数的字段名称,默认:count
                    ,dataName: 'data' //规定数据列表的字段名称,默认:data
                }
                // ,page: true //开启分页
                // ,toolbar: 'default' //开启工具栏,此处显示默认图标,可以自定义模板,详见文档
                ,cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'cdsid', title: 'ID', width:100, sort: true}
                    ,{field: 'cdsname', title: '名称', width:100}
                    ,{field: 'catname', title: '种类', width: 100}
                    ,{field: 'cdsurl', title: '展示', width: 300}
                    ,{field: 'cdsnum', title: '描述'}
                    ,{field: 'right', toolbar: '#btn_bar', title: '操作', width: 100}
                ]]

            });


        });
    </script>

F12之后又是一片红:

img

救一下吧,球球叾

1条回答 默认 最新

相关推荐 更多相似问题