国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式

瀏覽:11日期:2023-12-03 11:26:08
目錄Spring MVC jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List數(shù)據(jù)1、創(chuàng)建Controller2、創(chuàng)建對應(yīng)的jsp頁面注意事項:springMVC的forEach不能正常顯示1、問題2、解決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('蘋果'+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)建對應(yīng)的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>

不然會出現(xiàn)以下錯誤:

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、問題

在進(jìn)行springMVC的forEach聯(lián)系時,出現(xiàn)如下錯誤

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

<table border='2' bgcolor='aqua' > <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來進(jìn)行的,在實體類中添加getter方法即可。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲欧洲一区二区三区在线 | 久久久毛片免费全部播放 | 欧美亚洲欧美区 | 欧美成人观看 | 男人女人做性全程视视频 | 国产三级精品最新在线 | 国内自拍欧美 | 亚洲日本免费 | 直接在线观看的三级网址 | 美女被免费网站视频软件 | 成 人 黄 色 大 片 | 久久999精品 | 波多野结衣在线观看高清免费资源 | 日韩视频专区 | 欧美性夜欢 | 免费国产黄网站在线观看视频 | 欧美国产综合日韩一区二区 | 99久视频| 免费一级a毛片在线播放 | 在线成人天天鲁夜啪视频 | 欧美成人高清性色生活 | 日本午色www高清视频 | 91精品综合久久久久m3u8 | 亚洲成人xxx | a级午夜毛片免费一区二区 a级性生活视频 | 亚洲国产一区二区三区在线观看 | 毛片a片免费观看 | 久久精品中文字幕第一页 | 国内精品自产拍在线观看91 | 亚洲a级片在线观看 | 日韩区在线| 国产精品久久久久影院 | 久久综合色播 | 亚洲第五色综合网啪啪 | 国产欧美日韩中文久久 | 一道本久久 | 日本午色www高清视频 | 高清不卡毛片 | 男人天堂avav| 国产精品国产三级国产an | 久久99精品久久久久久久不卡 |