spring 如何使用JSTL获取jsp中列表的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14353054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:38:29  来源:igfitidea点击:

how to get the element of a list inside jsp using JSTL?

springjspspring-mvcjstl

提问by Mehdi

I have such this code inside my Spring MVC java controller class:

我的 Spring MVC java 控制器类中有这样的代码:

@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);
    }

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 :

哪个 uiClient 将使用 RMI 从数据库中获取一些数据......现在我想使用 JSTL 在我的 JSP 文件中显示作业和相关统计信息:

<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>

How do I get the LinkedListelements of Model inside my JSP using JSTL? There might be no no counter ibeen put in scope for me.

如何LinkedList使用 JSTL 在我的 JSP 中获取Model的元素?可能没有任何计数器i被放在我的范围内。

回答by Adrián

In my opinion, the right answer is a combination of both of the answers you got:

在我看来,正确答案是您得到的两个答案的组合:

use varStatus attribute of c:foreach tag

使用 c:foreach 标签的 varStatus 属性

but:

但:

"get" is not a jstl function.

“get”不是一个jstl函数。

<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>

EDIT: 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>

回答by wolfg

getis not a jstl function.

get不是 jstl 函数。

<td>${stats[i.index].No}</td>

回答by Netorica

use varStatusattribute of c:foreachtag

使用 标签varStatus属性c:foreach

<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.get(i.index).No}</td>
    </tr>
  </table>
</c:forEach>