Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据方式

 更新时间:2021年08月16日 14:17:46   作者:Hoking  
这篇文章主要介绍了Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Spring MVC jstl 标签c:forEach 遍历输出双层嵌套List数据

具体操作步骤如下:

1、创建Controller

import java.util.ArrayList;
import java.util.List; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 
import com.mahaochen.springmvc.domain.Goods;
import com.mahaochen.springmvc.domain.Inventory;
 
@Controller
@RequestMapping("/shop")
public class ShoppingController { 
 
 @RequestMapping("/shoppingCart")
 public String getShoppingCart(HttpServletRequest request,  HttpServletResponse response,Model model){  
  model.addAttribute("list", generateData());  
  return "shoppingCart";
 }
 private List<Inventory> generateData(){
   List<Inventory> inventories = new ArrayList<Inventory>();
  for(int i=0 ; i<2 ; i++){
   switch (i) {
   case 0:
    Inventory inventory1 = new Inventory();
    inventory1.setInventoryType("水果");
    List<Goods> goodsList1 = new ArrayList<Goods>(); 
    for(int j=0;j<5;j++){
     Goods goods = new Goods();
     goods.setGoodsName("苹果"+j);
     goodsList1.add(goods);
    }
    inventory1.setGoodList(goodsList1);
    inventories.add(inventory1);
    break;
   default:
    Inventory inventory2 = new Inventory();
    inventory2.setInventoryType("蔬菜");
    List<Goods> goodsList2 = new ArrayList<Goods>(); 
    for(int j=0;j<5;j++){
     Goods goods = new Goods();
     goods.setGoodsName("茄子"+j);
     goodsList2.add(goods);
    }
    inventory2.setGoodList(goodsList2);
    inventories.add(inventory2);
    break;
   }
  }
  return inventories;
 }
}

2、创建对应的jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车</title>
</head>
<body>
<c:forEach items="${list }" var="item">
${item.inventoryType}<br />
<c:set value="${item.goodList }" var="subItem"/>
<c:forEach items="${subItem }" var="var">
--${var.goodsName }<br />
</c:forEach>
</c:forEach>
</body>
</html>

注意事项:

JSTL1.1的库 在JSP2.0(Servlet 2.4)及以后(推荐用JSTL1.1及以上)用:

<%@taglibprefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
</web-app>

在 Servlet2.3及以前,

<%@taglibprefix="c" uri="http://java.sun.com/jstl/core"%>  

与2.4比较,以后版本路径少了jsp。

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
<web-app>        
</web-app>  

不然会出现以下错误:

org.apache.jasper.JasperException:/WEB-INF/jsp/shoppingCart.jsp(line: 10, column: 1) According to TLD or attribute directive in tag file,attribute items does not accept any expressions

springMVC的forEach不能正常显示

1、问题

在进行springMVC的forEach联系时,出现如下错误

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/index.jsp at line 12

    <table border="2" bgcolor="aqua" width="500px" >
         <c:forEach items="${list}" var="student">
         <tr >
             <td height="50px">${student.id}</td>
            <td height="50px">${student.name}</td>
         </tr>
        </c:forEach>

2、解决

forEach的获取是通过getter来进行的,在实体类中添加getter方法即可。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • MyBatis的模糊查询mapper.xml的写法讲解

    MyBatis的模糊查询mapper.xml的写法讲解

    这篇文章主要介绍了MyBatis的模糊查询mapper.xml的写法讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 原生Java操作兔子队列RabbitMQ

    原生Java操作兔子队列RabbitMQ

    这篇文章主要介绍了原生Java操作兔子队列RabbitMQ,MQ全称为Message Queue,即消息队列,“消息队列”是在消息的传输过程中保存消息的容器,需要的朋友可以参考下
    2023-05-05
  • SpringBoot Application的exclude不生效问题及排查

    SpringBoot Application的exclude不生效问题及排查

    这篇文章主要介绍了SpringBoot Application的exclude不生效问题及排查,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • SpringBoot如何打印mybatis的执行sql问题

    SpringBoot如何打印mybatis的执行sql问题

    这篇文章主要介绍了SpringBoot如何打印mybatis的执行sql问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java值得使用Lambda的8个场景合集

    Java值得使用Lambda的8个场景合集

    可能对不少人来说,Lambda显得陌生又复杂,觉得Lambda会导致代码可读性下降,但毕竟2023年了,JDK都出了那么多新版本,是时候试试Lambda了
    2023-08-08
  • 如何利用JConsole观察分析Java程序的运行并进行排错调优

    如何利用JConsole观察分析Java程序的运行并进行排错调优

    从Java 5开始 引入了 JConsole。JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行。您可以轻松地使用 JConsole(或者,它更高端的 “近亲” VisualVM )来监控 Java 应用程序性能和跟踪 Java 中的代码
    2015-12-12
  • MyBatis Generator ORM层面的代码自动生成器(推荐)

    MyBatis Generator ORM层面的代码自动生成器(推荐)

    Mybatis Generator是一个专门为 MyBatis和 ibatis框架使用者提供的代码生成器,也可以快速的根据数据表生成对应的pojo类、Mapper接口、Mapper文件,甚至生成QBC风格的查询对象,这篇文章主要介绍了MyBatis Generator ORM层面的代码自动生成器,需要的朋友可以参考下
    2023-01-01
  • SpringBoot学习之全局异常处理设置(返回JSON)

    SpringBoot学习之全局异常处理设置(返回JSON)

    本篇文章主要介绍了SpringBoot学习之全局异常处理设置(返回JSON),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Spring Batch远程分区的本地Jar包模式的代码详解

    Spring Batch远程分区的本地Jar包模式的代码详解

    这篇文章主要介绍了Spring Batch远程分区的本地Jar包模式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Java基于redis实现分布式锁代码实例

    Java基于redis实现分布式锁代码实例

    这篇文章主要介绍了Java基于redis实现分布式锁代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04

最新评论