在迭代多个对象时使用 JSTL 显示 JSP Table
Display JSP Table using JSTL while iterating multiple objects
我实际上仍然是这个 java ee 东西的初学者,而且我已经成功地走了很远..
我会尽量用我能忍受的最简单的方式来解释我的问题。:)
我正在使用我的 Servlet "MajTablesController.java" 从多个对象中检索对象信息(名称、引用等..)[目标是展示一个 Table 将所有这些信息收集到一个对象 ]
使用同一个 servlet,我发送一个列表作为名为“Resultat”的会话属性,它本身包含前面提到的不同对象的列表。
总而言之,我的 List Resultat(由我的 JSP 接收)包含 4 个 ListObject。
问题是在创建 table 时,我需要使用多个循环来从不同的对象中检索数据,无论我如何放置 forEach 标签,我都无法获取table 正确的方式(见图片),我最终要么在一行或一列中获取所有信息......
为了帮助您更清楚地了解正在发生的事情,下面是我使用此代码得到的示例:
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.estia.tai.UsersBeanModel"%>
<%@ page import="com.estia.tai.EchantillonBeanModel"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.estia.tai.BDDConnect"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" media="screen"
href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen"
href="../css/styles.css">
<script src="../js/scripts.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"
type="text/javascript"></script>
<link rel="stylesheet"
href="https://unpkg.com/bootstrap-table@1.13.4/dist/bootstrap-table.min.css">
<script
src="https://unpkg.com/bootstrap-table@1.13.4/dist/bootstrap-table.min.js"></script>
<head>
</head>
<div class="container-fluid">
<div class="col-md-12">
<div class="table-responsive table-dark" id="preptable">
<table class="table table-bordered table-hover text-center"
data-toggle="" data-search="false" data-filter-control="true"
data-show-export="true" data-click-to-select="true">
<thead>
<tr class="">
<th data-field=id data-filter-control="select"
data-sortable="true">Id Commande</th>
<th data-field=echantillons data-filter-control="select"
data-sortable="true">Echantillons</th>
<th data-field=conditionnement data-filter-control="select"
data-sortable="true">Conditionnement</th>
<th data-field=transporteur data-filter-control="select"
data-sortable="true">Transporteur</th>
<th data-field=etat data-filter-control="select"
data-sortable="true">Etat</th>
<th data-field=date data-filter-control="select"
data-sortable="true">Date</th>
<th data-field=client data-filter-control="select"
data-sortable="true">Clientss</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Resultat}" var="variable">
<tr>
<td><a href="../CommandeVueController?id=${variable.id}">${variable.id}</a></td>
<td>${variable.echantillonRef}</td>
<td>${variable.conditionnement}</td>
<td><a
href="../TransporteurVueController?id=${variable.transporteurId}">${variable.transporteur}</a></td>
<td>${variable.etat}</td>
<td>${variable.date}</td>
<td>${variable.client}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<!--end of .table-responsive-->
<script>
var $table = $('#preptable');
$(function() {
$('#toolbar').find('select').change(function() {
$table.bootstrapTable('refreshOptions', {
exportDataType : $(this).val()
});
});
})
var trBoldBlue = $("preptable");
$(trBoldBlue).on("click", "tr", function() {
$(this).toggleClass("bold-blue");
});
$(document)
.ready(
function() {
td_array = document
.getElementsByTagName("td");
check_prep = "En preparation";
for (i = 0; i < td_array.length; i++) {
if (td_array[i].textContent == check_prep) {
td_array[i].style.backgroundColor = "#FF8C00";
}
;
}
;
});
</script>
</div>
</div>
TablePrep.jsp 实际显示的内容:https://screenshotscdn.firefoxusercontent.com/images/2e48ceaf-3cd6-422a-a21c-b01d215b5d0e.png
目标是让下面列表中的每个项目都有自己的行(就像 table )
注意 prepTable 方法(从其他对象收集和协调信息)在它们的 getter 方法中发送列表,这解释了为什么我们在 table.[=15= 中有列表]
!ServletSide
BDDConnect connexionBDDModele = new BDDConnect();
Connection connection = connexionBDDModele.getConnexion();
List<PrepTableVueModel> prepTableList = new ArrayList<PrepTableVueModel>();
int TransporteurId = 0;
if (request.getSession().getAttribute("p") == "p") {
PrepTableVueModel prepTable = new PrepTableVueModel();
//request.getSession().setAttribute("commandeResultat", CommandeDAOModel.lireList());
//request.getSession().setAttribute("echantillonResultat", EchantillonDAOModel.lireListe());
//request.getSession().setAttribute("conditionnementResultat", ConditionnementDAOModel.lireListe());
//request.getSession().setAttribute("clientResultat", ClientDAOModel.lireListe());
//request.getSession().setAttribute("transporteurResultat", TransporteurDAOModel.lireListe());
prepTable.addCommande(CommandeDAOModel.lireList());
for (int i = 0 ; i < CommandeDAOModel.lireList().size(); i++) {
prepTable.addEchantillon(EchantillonDAOModel.lireListeEch(CommandeDAOModel.lireList().get(i).getId()));
}
for (int i = 0 ; i< prepTable.EchantillonList.size(); i++) {
prepTable.addEchantillonRef(EchantillonDAOModel.lireListeEchFinal(prepTable.EchantillonList.get(i)));
}
for (int i =0; i < prepTable.EchantillonListRef.size(); i++) {
}
//prepTable.addConditionnement(ConditionnementDAOModel.lireListe());
//prepTable.addClient(ClientDAOModel.lireListe());
//prepTable.addTransporteur(TransporteurDAOModel.lireListe());
System.out.println(prepTable.getId() + "id dans le maj ");
System.out.println(prepTable.getEchantillon() + "ech dans le maj ");
prepTableList.add(prepTable);
request.getSession().setAttribute("Resultat", prepTableList);
request.getRequestDispatcher("/panel/tableprep.jsp").forward(request, response);
我尝试过,而不是 return 从 PrepTable 到 return 单个元素的元素列表,但没有成功..
我已经成功了,
诀窍是向 JSP 页面发送一个附加字符串属性,指示我拥有的元素数量(以设置循环)代码变为:
Blockquote
<tbody>
<c:forEach begin="0" end="${ResultNumberCom}" varStatus="loop">
<c:forEach items="${Resultat}" var="variable">
<tr>
<td><a href="../CommandeVueController?id=${variable.id[loop.index]}">${variable.id[loop.index]}</a></td>
<td>${variable.echantillonRef[loop.index]}</td>
<td>${variable.conditionnement[loop.index]}</td>
<td><a
href="../TransporteurVueController?id=${variable.transporteurId[loop.index]}">${variable.transporteur[loop.index]}</a></td>
<td>${variable.etat[loop.index]}</td>
<td>${variable.date}[loop.index]</td>
<td>${variable.client}</td>
</tr>
</c:forEach>
</c:forEach>
我实际上仍然是这个 java ee 东西的初学者,而且我已经成功地走了很远..
我会尽量用我能忍受的最简单的方式来解释我的问题。:)
我正在使用我的 Servlet "MajTablesController.java" 从多个对象中检索对象信息(名称、引用等..)[目标是展示一个 Table 将所有这些信息收集到一个对象 ]
使用同一个 servlet,我发送一个列表作为名为“Resultat”的会话属性,它本身包含前面提到的不同对象的列表。
总而言之,我的 List Resultat(由我的 JSP 接收)包含 4 个 ListObject。
问题是在创建 table 时,我需要使用多个循环来从不同的对象中检索数据,无论我如何放置 forEach 标签,我都无法获取table 正确的方式(见图片),我最终要么在一行或一列中获取所有信息......
为了帮助您更清楚地了解正在发生的事情,下面是我使用此代码得到的示例:
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.estia.tai.UsersBeanModel"%>
<%@ page import="com.estia.tai.EchantillonBeanModel"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.estia.tai.BDDConnect"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" media="screen"
href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen"
href="../css/styles.css">
<script src="../js/scripts.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"
type="text/javascript"></script>
<link rel="stylesheet"
href="https://unpkg.com/bootstrap-table@1.13.4/dist/bootstrap-table.min.css">
<script
src="https://unpkg.com/bootstrap-table@1.13.4/dist/bootstrap-table.min.js"></script>
<head>
</head>
<div class="container-fluid">
<div class="col-md-12">
<div class="table-responsive table-dark" id="preptable">
<table class="table table-bordered table-hover text-center"
data-toggle="" data-search="false" data-filter-control="true"
data-show-export="true" data-click-to-select="true">
<thead>
<tr class="">
<th data-field=id data-filter-control="select"
data-sortable="true">Id Commande</th>
<th data-field=echantillons data-filter-control="select"
data-sortable="true">Echantillons</th>
<th data-field=conditionnement data-filter-control="select"
data-sortable="true">Conditionnement</th>
<th data-field=transporteur data-filter-control="select"
data-sortable="true">Transporteur</th>
<th data-field=etat data-filter-control="select"
data-sortable="true">Etat</th>
<th data-field=date data-filter-control="select"
data-sortable="true">Date</th>
<th data-field=client data-filter-control="select"
data-sortable="true">Clientss</th>
</tr>
</thead>
<tbody>
<c:forEach items="${Resultat}" var="variable">
<tr>
<td><a href="../CommandeVueController?id=${variable.id}">${variable.id}</a></td>
<td>${variable.echantillonRef}</td>
<td>${variable.conditionnement}</td>
<td><a
href="../TransporteurVueController?id=${variable.transporteurId}">${variable.transporteur}</a></td>
<td>${variable.etat}</td>
<td>${variable.date}</td>
<td>${variable.client}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<!--end of .table-responsive-->
<script>
var $table = $('#preptable');
$(function() {
$('#toolbar').find('select').change(function() {
$table.bootstrapTable('refreshOptions', {
exportDataType : $(this).val()
});
});
})
var trBoldBlue = $("preptable");
$(trBoldBlue).on("click", "tr", function() {
$(this).toggleClass("bold-blue");
});
$(document)
.ready(
function() {
td_array = document
.getElementsByTagName("td");
check_prep = "En preparation";
for (i = 0; i < td_array.length; i++) {
if (td_array[i].textContent == check_prep) {
td_array[i].style.backgroundColor = "#FF8C00";
}
;
}
;
});
</script>
</div>
</div>
TablePrep.jsp 实际显示的内容:https://screenshotscdn.firefoxusercontent.com/images/2e48ceaf-3cd6-422a-a21c-b01d215b5d0e.png
目标是让下面列表中的每个项目都有自己的行(就像 table )
注意 prepTable 方法(从其他对象收集和协调信息)在它们的 getter 方法中发送列表,这解释了为什么我们在 table.[=15= 中有列表]
!ServletSide
BDDConnect connexionBDDModele = new BDDConnect();
Connection connection = connexionBDDModele.getConnexion();
List<PrepTableVueModel> prepTableList = new ArrayList<PrepTableVueModel>();
int TransporteurId = 0;
if (request.getSession().getAttribute("p") == "p") {
PrepTableVueModel prepTable = new PrepTableVueModel();
//request.getSession().setAttribute("commandeResultat", CommandeDAOModel.lireList());
//request.getSession().setAttribute("echantillonResultat", EchantillonDAOModel.lireListe());
//request.getSession().setAttribute("conditionnementResultat", ConditionnementDAOModel.lireListe());
//request.getSession().setAttribute("clientResultat", ClientDAOModel.lireListe());
//request.getSession().setAttribute("transporteurResultat", TransporteurDAOModel.lireListe());
prepTable.addCommande(CommandeDAOModel.lireList());
for (int i = 0 ; i < CommandeDAOModel.lireList().size(); i++) {
prepTable.addEchantillon(EchantillonDAOModel.lireListeEch(CommandeDAOModel.lireList().get(i).getId()));
}
for (int i = 0 ; i< prepTable.EchantillonList.size(); i++) {
prepTable.addEchantillonRef(EchantillonDAOModel.lireListeEchFinal(prepTable.EchantillonList.get(i)));
}
for (int i =0; i < prepTable.EchantillonListRef.size(); i++) {
}
//prepTable.addConditionnement(ConditionnementDAOModel.lireListe());
//prepTable.addClient(ClientDAOModel.lireListe());
//prepTable.addTransporteur(TransporteurDAOModel.lireListe());
System.out.println(prepTable.getId() + "id dans le maj ");
System.out.println(prepTable.getEchantillon() + "ech dans le maj ");
prepTableList.add(prepTable);
request.getSession().setAttribute("Resultat", prepTableList);
request.getRequestDispatcher("/panel/tableprep.jsp").forward(request, response);
我尝试过,而不是 return 从 PrepTable 到 return 单个元素的元素列表,但没有成功..
我已经成功了, 诀窍是向 JSP 页面发送一个附加字符串属性,指示我拥有的元素数量(以设置循环)代码变为:
Blockquote
<tbody>
<c:forEach begin="0" end="${ResultNumberCom}" varStatus="loop">
<c:forEach items="${Resultat}" var="variable">
<tr>
<td><a href="../CommandeVueController?id=${variable.id[loop.index]}">${variable.id[loop.index]}</a></td>
<td>${variable.echantillonRef[loop.index]}</td>
<td>${variable.conditionnement[loop.index]}</td>
<td><a
href="../TransporteurVueController?id=${variable.transporteurId[loop.index]}">${variable.transporteur[loop.index]}</a></td>
<td>${variable.etat[loop.index]}</td>
<td>${variable.date}[loop.index]</td>
<td>${variable.client}</td>
</tr>
</c:forEach>
</c:forEach>