jstl access a second array value with foreach index - jstl

i have two arrays (strings delimited with commas) and i made a foreach loop on one of this vars
i need to be able to access to the other string with the foreach index like
<c:set var="name" value="Zara,nuha,roshy" />
<c:set var="name2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${name}" delims="," var="name" varstatus="i">
<c:out value="${name}"/><br>
</c:forEach>
i need to access name2 values, in the name foreach, is it possible without doing another foeach?

The varstatus variable you are using contains a value "index" that you can use.
But, you can't operate on a string like that (not that I know of at least).
First, you need to convert name2 to a proper array or list. Then you can access it inside the for loop:
${name2list[i.index]}
Now, how to convert it to an array? How about the split function?
<%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="name2list" value="${fn:split(name2, ',')}"/>

This can be done, contrary to some previous comments.
See the following example based on the question:
<c:set var="names" value="Zara,nuha,roshy" />
<c:set var="names2" value="Zara2,nuha2,roshy2" />
<c:forEach items="${names.split(',')}" varStatus="i" var="name" >
${name} : ${names2.split(',')[i.index]}<br/>
</c:forEach>
Essentially we're using a string split function with expression language to get a string array from list comma separated values. Within the loop we the get the second value from the names2 array by using the varStatus index. I believe this accomplishes the task.

Related

Concatenate two variables to check null condition using JSTL tags

I am trying to send image of each user separately in ModelMap
model.addAttribute("profileImage"+i, sb); // I have added attribute in loop
Variable 'i' is an index for each image which I am iterating for each user.
Now I am Having profileImage0,profileImage1,profileImage2 and so on.
While Accessing I am putting some condition
<c:choose>
<c:when test="${profileImage!=null}">
<center><img src="${profileImage}" id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar">
</c:when>
<c:otherwise>
<center><img src="${pageContext.request.contextPath}/resources/images/defaultuserprofile.png" id="${profileImage}" class="rounded-circle img-circle img-responsive" style="height:90px;width:90px;" alt="Avatar"></center>
</c:otherwise>
</c:choose>
So in the Above Code I need to append some indexing to a variable which will first initialize the value of a variable then use it properly instead using like this one.
like profileImage0,profileImage1 and so on....
You can make use of the <c:forEach> tag to iterate over your variables. You may want to pass in the maximum number of values you have in your controller so you know how many values you need to pick up from your request:
<c:forEach var = "i" begin = "1" end = "${max}">
<c:set var = "variableName" scope = "session" value = "profileName + ${i}"/>
<c:set var = "profileImage" scope = "session" value = "${variableName}"/>
</c:forEach>
(or something like that...)

How to split number in a jstl variable

I have this value
<c:set var="string1" value="SS4444"/>
What I am try to achieve is to get the numbers only (4444) from the above variable. I can use substring, but the index value might change, ie it can be 'SS4444', 'S4444', 'SSS444', so cant rely on static index.
Thanks in advance
Split it with regex.. (substringAfter is not proper for this..):
<c:forEach var="string1" items="${fn:split(yourstring, '^\w*')}">
<c:set var="string2" value="${string1}"/>
</c:forEach>
You can try with replace function as:
<c:set var="strng" value="SS4444"/>
<c:set var="strng1" value="${fn:replace(strng,'S','')}"/>

How do I concatenate a list of values in JSTL into a single JSTL var?

I’m using JBoss 7.1.3.Final and Spring 3.1.1.RELEASE. On my JSP page, how do I get all the ids of an array into a comma separated string? I have tried this:
<c:forEach var="subject" items="${category.subjects}" varStatus="status">
<c:if test="${status.index == 0}">
<c:set var="cateogrySubjects" value="${subject.id}"/>
</c:if>
<c:if test="${status.index > 0}">
<c:set var="categorySubjects" value="${subject.id},${categorySubjects}"/>
</c:if>
</c:forEach>
Unfortunately the last statement printed is always “subj1,subj1” in the array even if the intermediate values are “subj1”, “subj2”, “subj3”, etc. Any help on concatenating things would be great.

set value of jquery variable equal to a jstl variable

I have a jstl value,
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/></c:forEach>
I want to set the variable in jquery equal to the var = "c".
How could I do that?
<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
You might need to JS escape it, quote it, etc. depending on what's actually in the variable, which you don't mention. It's also not clear what "a variable in jQuery" is.
The bottom line is that if the JavaScript lives in a JSP, just use the value: the JS isn't evaluated until the response has been emitted, so mix-and-match, but be aware that it's pretty easy to create invalid JavaScript unless you take care to properly escape whatever value types you're emitting.
As an example, consider a Java String that contains a single quote: if you single-quote the Java value in JS you'd have invalid JS because you didn't JS-escape the string value.
the method given in the below code might be correct but the variable whatever would have only one value that also only during the last count of the for each loop would exist
<script>
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
var whatever = ${c};
</c:forEach>
<script>
for instance if the loop for the variable detail is of list type that consists of string type objects in it like "A" , "B" ,"C" then in the case the value of the var whatever will be "C", this is because jstl is compile time language.
So the above code won't work in for each loop
the following code might help you out or atleast provide you a idea for same.
<script>
var whatever = new Array;
<c:forEach items="${detail}" var="u">
<c:set value="${u.content}" var="c"/>
for (var i=0;i<whatever.length;i++)
{
whatever[i]=${c};
}
</c:forEach>
<script>

EL define array to output object properties

In PHP, I commonly would do something like this:
foreach(array('street','town','county','postcode') as $e) {
echo $address[$e] . '<br/>';
}
It's concise and easy to work with. Is there any way of doing this in EL? I'm having trouble finding a good way to do it cleanly.
There's nothing like that in standard JSTL/EL, but you could use JSTL fn:split() to split a single delimited string to an array:
<c:forEach items="${fn:split('street,town,county,postcode', ',')}" var="e">
${address[e]}<br/>
</c:forEach>
(provided that ${address} points to a Map with the given values as keys or a Javabean with the given properties)
Or if the ${address} is indeed a Map which contains only those keys already, you could also just loop over the Map itself:
<c:forEach items="${address}" var="entry">
${entry.value}<br/>
</c:forEach>
(the map key can in the above example be printed by ${entry.key}; also note that you need LinkedHashMap in order to maintain insertion order)
Typically you would populate a map or list server side, then output them on your JSP using JSTL for each loop something like below:
<c:forEach items="${formBean.myMap}" varStatus="itm">
<tr>
<td>${itm.key.propertyName}</td>
<td>${itm.value.propertyName}</td> <!--which is same as below ... -->
<td>${formBean.myMap[itm.key].propertyName}</td>
</tr>
</c:forEach>

Resources