Is there a way to access HashMap element in EL with key which is concatenation of String and int value. Something like this:
<c:forEach begin="1" end="5" var="current">
<c:out value="${myHashMap['elem-' + current]}"/>
</c:forEach>
This code can accomplish desired behavior
<c:forEach begin="1" end="5" var="current">
<c:set var="key" value="elem-${current}" />
<c:out value="${myHashMap[key]}"/>
</c:forEach>
Related
I would like to show all the right answers(Green) in one TD if there are more than one and all the wrong answers(Red) in one TD if there are more than one. Is there anyway in the controller or at the front end that I can achieve this?
With my current code this is what I get.
The aim is to display like this
Controller
#GetMapping("/quesInAss/{id}")
public String quesInAssessment(#PathVariable("id") Integer id, Model model, HttpSession session) {
Integer insId = (Integer) session.getAttribute("instId");
List<Answers> rlist = new ArrayList<Answers>();
List<Answers> wlist = new ArrayList<Answers>();
Assessments ass = as.getAllByAssInst(id, insId);
model.addAttribute("assName", ass.getAssName());
List<Questions> queslist = qs.getQuesByAssessment(ass);
for (Questions ques : queslist) {
List<Answers> anslist = ansService.getAnswersByQuestion(ques);
for (Answers answer : anslist) {
if (answer.getAnsStatusCode().equals("CORR")) {
rlist.add(answer);
Answers[] array = new Answers[rlist.size()];
array = rlist.toArray(array);
model.addAttribute("rightAnsList", array);
} else {
wlist.add(answer);
Answers[] array = new Answers[wlist.size()];
array = wlist.toArray(array);
model.addAttribute("wrongAnsList", array);
}
}
}
model.addAttribute("queslist", queslist);
return "listOfQuesInAss";
}
JSP
<c:forEach items="${queslist}" var="ques">
<tr>
<td>${ques.quesText}</td>
<c:forEach items="${rightAnsList}" var="rightans">
<c:if test="${rightans.questions.quesId == ques.quesId}">
<td>${rightans.answer}</td>
</c:if>
</c:forEach>
<c:forEach items="${wrongAnsList}" var="wrongans">
<c:if test="${wrongans.questions.quesId == ques.quesId}">
<td>${wrongans.answer}</td>
</c:if>
</c:forEach>
</tr>
</c:forEach>
I'm not sure if I could understand your question right, try this:
<c:forEach items="${queslist}" var="ques">
<tr>
<td>${ques.quesText}</td>
<td>
<c:forEach items="${rightAnsList}" var="rightans" varStatus="status">
<c:if test="${rightans.questions.quesId == ques.quesId}">
${rightans.answer}
</c:if>
<c:if test="${not status.last}">,</c:if>
</c:forEach>
</td>
<td>
<c:forEach items="${wrongAnsList}" var="wrongans" varStatus="status">
<c:if test="${wrongans.questions.quesId == ques.quesId}">
${wrongans.answer}
</c:if>
<c:if test="${not status.last}">,</c:if>
</c:forEach>
</td>
</tr>
</c:forEach>
I have a 2-dimensional array, which can be done like this (working code):
<c:forEach items="${dataArray}" var="row">
Subject id: ${row[0]}
</c:forEach>
But I want to add a condition; when I try this it does not display anything:
<c:forEach items="${dataArray}" var="row">
<c:choose>
<c:when test="${fn:containsIgnoreCase(${row[0]}, 'condtion1')}">
Subject id: ${row[0]}
</c:when>
</c:choose>
</c:forEach>
Have I got something wring in the test attribute that makes it always false?
I am new to JSP and Spring. I want to insert a userID(U0005) using textbox in spring form but it is storing(,U0005). From where the "," is inserted?
The code I have written is:
/* In Register.jsp: */
<c:url var="addAction" value="/libUsr/add"></c:url>
<form:form action="${addAction}" commandName="libUsr">
<table>
<tr>
<td>
<form:label path="id">
<spring:message text="ID" />
</form:label>
</td>
<td><form:input path="id" required="true" /></td>
</tr>
</table>
</form:form>
/*
In UserController.java:
*/
#RequestMapping(value= "/libUsr/add", method = RequestMethod.POST)
public String addLibUsr(#ModelAttribute("libUsr") LibUsr libUsr){
libUsrDAO.saveOrUpdate(libUsr);
return "redirect:/register";
}
/*
In DAOImpl:
Saving the data through DAOs
*/
#Transactional
public void saveOrUpdate(LibUsr libusr) {
sessionFactory.getCurrentSession().saveOrUpdate(libusr);
}
Your problem is with this tag:
<form:label path="id">
As you are adding the attribute path to one label, which wont store any value, spring try to get the value and returns empty,yourID.
Change the path attribute of your label to:
<form:label for="id">
<spring:message text="ID" />
</form:label>
If you want to show the value of the id into that label use:
<label>${yourObject.id}</label>
or
<label th:value="${yourObject.id}"></label>
Path attribute is just for input, select, checkbox...not static values as label, span...
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 counter i been put in scope for me.
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>
get is not a jstl function.
<td>${stats[i.index].No}</td>
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>
I have searched through for this logic pretty much everywhere but am sure I haven't done a great job and hence this question.
I have an Arraylist and it needs to be iterated and displayed in the following format
User1 User2 User3
User4 User5 User6
User7 User8 User9
I am able to do this using normal JSP scriptlets but not using JSTL. I have tried using JSTL but it skips a few entries from the list. Here is my code
<tr>
<c:forEach var="party" items="${partiesList}">
<c:set var="ctr" value="${ctr+1}" scope="page"/>
<c:choose>
<c:when test="${ctr le 3}">
<td>
<figure>
<img src="images/${party.avatarUrl}" onclick="showHide('','${party.screenName}')" class="avaimg" alt="${party.screenName}">
<figcaption><spring:message code="user.${party.screenName}" text="${party.screenName}"/></figcaption>
</figure>
</td>
</c:when>
<c:otherwise>
</tr>
<c:set var="ctr" value="0" scope="page"/>
</c:otherwise>
</c:choose>
</c:forEach>
Any help would be appreciated.
Thanks in advance
You can do this by using the varStatus attribute and the modulo operator (%).
<table>
<c:forEach var="party" items="${partiesList}" varStatus="status">
<c:if test="${status.index % 3 == 0}"><tr></c:if>
<td>${status.index}</td>
<c:if test="${status.index % 3 == 2}"></tr></c:if>
</c:forEach>
</table>
varStatus will return a LoopTagStatus object, where index is
The index of the current round of the iteration. If iteration is being performed over a subset of an underlying array, java.lang.Collection, or other type, the index returned is absolute with respect to the underlying collection. Indices are 0-based.
You can use the modulo operator to check if you need to start or close a new row.
A different approach would be just looping through the list, output it as an unnumbered list and use CSS to style it the way you want. That's how I would have solved it. An example of how your CSS could look:
ul.my-class { overflow: hidden; width: 600px; }
ul.my-class li { float: left; width: 200px; }