如何使用JSTL在jsp中获取列表的元素?
我的Spring MVC java控制器类中有以下代码:
I have such this code inside my Spring MVC java controller class:
@RequestMapping(value = "jobs", method = { RequestMethod.GET })
public String jobList(@PathVariable("username") String username, Model model) {
JobInfo[] jobInfo;
JobStatistics js;
LinkedList<JobStatistics> jobStats = new LinkedList<JobStatistics>();
try {
jobInfo = uiClient.getJobs(username);
for (int i = 0; i < jobInfo.length; i++) {
js = uiClient.getJobStatistics(jobInfo[i].getJobId());
jobStats.add(js);
}
model.addAttribute("jobs", jobInfo);
model.addAttribute("jobStats", jobStats);
}
哪个uiClient会使用RMI从数据库中获取一些数据... 现在我想显示工作&使用JSTL在我的JSP文件中提供相关的统计信息:
which uiClient will get some data from database using RMI ... now I want to show the jobs & related statistic inside my JSP file using JSTL :
<c:set var="stats" value="${jobStats}" />
<c:forEach var="jobs" items="${jobs}">
<c:set var="jobID" value="${jobs.JobId}"/>
<table>
<tr class="tr1">
<td>${jobs.Topic}</td>
<td>${stats.get(i).No}</td>
</tr>
</table>
</c:forEach>
如何使用JSTL在我的JSP中获取Model的LinkedList
元素?对于我来说,可能没有任何计数器i
.
How do I get the LinkedList
elements of Model inside my JSP using JSTL? There might be no no counter i
been put in scope for me.
我认为,正确的答案是您得到的两个答案的组合:
In my opinion, the right answer is a combination of both of the answers you got:
使用c:foreach标记的varStatus属性
use varStatus attribute of c:foreach tag
但是:
"get"不是jstl函数.
"get" is not a jstl function.
<c:forEach var="jobs" items="${jobs}" varStatus="i">
<c:set var="jobID" value="${jobs.jobId}"/>
<table>
<tr class="tr1">
<td>${jobs.topic}</td>
<td>${stats[i.index].no}</td>
</tr>
</table>
</c:forEach>
这是问题作者最终使用的代码:
this is the code finally used by the author of the question:
<c:set var="stats" value="${jobStats}" />
<c:forEach items="${jobs}" varStatus="i">
<c:set var="jobID" value="${jobs[i.index].jobId}"/>
<table>
<tr class="tr1">
<td>${jobs[i.index].topic}</td>
<td>${stats[i.index].no}</td>
<td>${jobID}</td>
</tr>
</table>
</c:forEach>