- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Spring MVC如何使用jstl 標簽c:forEach遍歷輸出雙層嵌
這篇文章主要為大家展示了“Spring MVC如何使用jstl 標簽c:forEach遍歷輸出雙層嵌套List的數據方式”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“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("蘋(píng)果"+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>購物車(chē)</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>
不然會(huì )出現以下錯誤:
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
在進(jìn)行springMVC的forEach聯(lián)系時(shí),出現如下錯誤
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的獲取是通過(guò)getter來(lái)進(jìn)行的,在實(shí)體類(lèi)中添加getter方法即可。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站