当前位置:   article > 正文

Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据_jstl的foreach可以遍历两个数组吗

jstl的foreach可以遍历两个数组吗

具体操作步骤如下:

1、创建Controller。

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import com.mahaochen.springmvc.domain.Goods;
  9. import com.mahaochen.springmvc.domain.Inventory;
  10. @Controller
  11. @RequestMapping("/shop")
  12. public class ShoppingController {
  13. @RequestMapping("/shoppingCart")
  14. public String getShoppingCart(HttpServletRequest request, HttpServletResponse response,Model model){
  15. model.addAttribute("list", generateData());
  16. return "shoppingCart";
  17. }
  18. private List<Inventory> generateData(){
  19. List<Inventory> inventories = new ArrayList<Inventory>();
  20. for(int i=0 ; i<2 ; i++){
  21. switch (i) {
  22. case 0:
  23. Inventory inventory1 = new Inventory();
  24. inventory1.setInventoryType("水果");
  25. List<Goods> goodsList1 = new ArrayList<Goods>();
  26. for(int j=0;j<5;j++){
  27. Goods goods = new Goods();
  28. goods.setGoodsName("苹果"+j);
  29. goodsList1.add(goods);
  30. }
  31. inventory1.setGoodList(goodsList1);
  32. inventories.add(inventory1);
  33. break;
  34. default:
  35. Inventory inventory2 = new Inventory();
  36. inventory2.setInventoryType("蔬菜");
  37. List<Goods> goodsList2 = new ArrayList<Goods>();
  38. for(int j=0;j<5;j++){
  39. Goods goods = new Goods();
  40. goods.setGoodsName("茄子"+j);
  41. goodsList2.add(goods);
  42. }
  43. inventory2.setGoodList(goodsList2);
  44. inventories.add(inventory2);
  45. break;
  46. }
  47. }
  48. return inventories;
  49. }
  50. }

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

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

 Servlet2.3及以前,

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

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

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  3. <web-app>
  4. </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




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/201860
推荐阅读
相关标签
  

闽ICP备14008679号