how to get the element of a list inside jsp using JSTL?
92,364
Solution 1
In my opinion, the right answer is a combination of both of the answers you got:
use varStatus attribute of c:foreach tag
but:
"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>
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>
Solution 2
get
is not a jstl function.
<td>${stats[i.index].No}</td>
Solution 3
use varStatus
attribute of c:foreach
tag
<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>
Author by
Mehdi
Music lover, Creative, Smart, experienced Looking for job offers and opportunities 1. Linkedin Profile 2. Community
Updated on October 30, 2020Comments
-
Mehdi over 3 years
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); }
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>
How do I get the
LinkedList
elements of Model inside my JSP using JSTL? There might be no no counteri
been put in scope for me. -
BalusC over 11 yearsOP's code would have worked when using EL 2.2. The only problem left is providing the proper value of
i
, which is nowhere visible in the code provided so far (and also explicitly been mentioned by the OP as the concrete problem). -
Mehdi over 11 yearsI tried ur solution but there is a problem: javax.el.PropertyNotFoundException: Property '0' not found on type rs.ui.JobInfo
-
Mehdi over 11 yearsI tried ur solution but there is a problem: javax.el.PropertyNotFoundException: Property '0' not found on type rs.ui.JobInfo
-
Adrián over 11 yearsmm that's weird. Is your 'stats' variable the
LinkedList
instance? -
Mehdi over 11 yearsyeah, I defined it like what I did above ... but my JobInfo is an array
-
Adrián over 11 yearsOh, I think I found the mistake. The attribute access' first letter should be lowercase. I'll edit the answer. Assuming those objects have 'getJobId', 'getTopic' and 'getNo' getters.
-
Mehdi over 11 yearsI edited your code & made that the final answer (for those who want to know about it *foreach item in java is like using 'var' in jstl & for iterating over a temporary variable use 'varStatus' instead in JSTL but never use both of them at the same time inside <c:forEach)... thnx for all contributions Fellas ...
-
Mehdi over 11 yearsplease confirm the edited document & I then I will rate it as an accepted answer .
-
Adrián over 11 years@Mehdi It's been rejected by the community so I couldn't find a way to accept it :/ Also, I think you can actually use both 'var' and 'varStatus' at the same time without any problem. Anyway, if using it like you say worked for you...
-
Adrián over 11 years@Mehdi I added your code to the answer though you won't get any reputation this way :/ sorry
-
Mehdi over 11 yearsI'm not 100% sure if we can use both var & varStatus at the same time but my problem got solved when I did that, there's no problem for the reputation man, at least we share something that other people can use also :) thanx so much
-
Andremoniy about 8 yearsIt's the most useful answer