<c:forEach />

作者: 文字抒意 | 来源:发表于2017-03-31 19:44 被阅读22次
    1遍历List<Map<String, Object>>

    后台代码

    @RequestMapping("/model")
    public ModelAndView prdModelSearch() {
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> model = modelService.getModel();  //此处map的key名字叫model
        data.put("model", model); //用这种方法传到前台,forEach取值时,按data的key名称取值,如下面items="${model}"
        
        return new ModelAndView("/model", data);
     }
        
    

    前台代码

    <select class="form-control" id="model">
        <option value="">请选择</option>
        <c:forEach items="${model}" var="item">
            <option value="${item.model}">${item.model}</option>  ////此处的model为后台map(也即model) 的key的名称
        </c:forEach>
    </select>
    
    
    2遍历List

    后台代码

    @RequestMapping("/model")
    public ModelAndView prdModelSearch() {
        Map<String, Object> data = new HashMap<>();
        List<String> model = modelService.getModel()  //此处的model是List
        data.put("model", model);
        
        return new ModelAndView("/model", data);
     }
    

    前台代码

    <SELECT name="model" id="model" multiple="multiple">
        <c:forEach items="${model}" var="item">
            <option value="${item}">${item}</option>
        </c:forEach>
    </SELECT>   
    
    3遍历Map

    后台代码

    @RequestMapping("/model")
    public ModelAndView prdModelSearch() {
        Map<String, Object> data = new HashMap<>();
        Map<String, String> model = modelService.getModel()  //此处的model是List
        data.put("model", model);
        
        return new ModelAndView("/model", data);
     }
    

    前台代码

    <SELECT name="model" id="model" multiple="multiple">
        <c:forEach items="${model}" var="item">
            <option value="${item.key}">${item.value}</option> //map的键值对
        </c:forEach>
    </SELECT>   
    

    相关文章

      网友评论

        本文标题:<c:forEach />

        本文链接:https://www.haomeiwen.com/subject/wfgvottx.html