Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式
Spring MVC jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List數(shù)據(jù)
具體操作步驟如下:
1、創(chuàng)建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("蘋(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; } }
2、創(chuàng)建對(duì)應(yīng)的jsp頁(yè)面
<%@ 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>購(gòu)物車(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>
注意事項(xiàng):
JSTL1.1的庫(kù) 在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ì)出現(xiàn)以下錯(cuò)誤:
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、問(wèn)題
在進(jìn)行springMVC的forEach聯(lián)系時(shí),出現(xiàn)如下錯(cuò)誤
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的獲取是通過(guò)getter來(lái)進(jìn)行的,在實(shí)體類中添加getter方法即可。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用idea+gradle編譯spring5.x.x源碼分析
這篇文章主要介紹了idea?+?gradle編譯spring5.x.x源碼,在編譯spring5源碼時(shí)需要將項(xiàng)目導(dǎo)入idea中然后編譯配置,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2022-04-04Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示?
這篇文章主要介紹了Intellij IDEA 關(guān)閉和開(kāi)啟自動(dòng)更新的提示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04maven引入mysql-connector-java包失敗的解決方案
這篇文章主要介紹了maven引入mysql-connector-java包失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例
這篇文章主要介紹了Java高并發(fā)編程之CAS實(shí)現(xiàn)無(wú)鎖隊(duì)列代碼實(shí)例,在多線程操作中,我們通常會(huì)添加鎖來(lái)保證線程的安全,那么這樣勢(shì)必會(huì)影響程序的性能,那么為了解決這一問(wèn)題,于是就有了在無(wú)鎖操作的情況下依然能夠保證線程的安全,需要的朋友可以參考下2023-12-12Mybatis的SqlRunner執(zhí)行流程實(shí)現(xiàn)
MyBatis提供了一個(gè)用于操作數(shù)據(jù)庫(kù)的SqlRunner工具類,對(duì)JDBC做了很好的封裝,本文主要介紹了Mybatis的SqlRunner執(zhí)行流程實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Java基礎(chǔ)之Thymeleaf的簡(jiǎn)單使用
這篇文章主要介紹了Java基礎(chǔ)之Thymeleaf的簡(jiǎn)單使用,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04