您好,登录后才能下订单哦!
这篇文章主要为大家展示了“Spring MVC如何使用jstl 标签c:forEach遍历输出双层嵌套List的数据方式”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Spring MVC如何使用jstl 标签c:forEach遍历输出双层嵌套List的数据方式”这篇文章吧。
具体操作步骤如下:
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; } }
<%@ 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联系时,出现如下错误
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>
forEach的获取是通过getter来进行的,在实体类中添加getter方法即可。
以上是“Spring MVC如何使用jstl 标签c:forEach遍历输出双层嵌套List的数据方式”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。