JSTL: I need to access hashtable using a key - jstl

I know this works: <c:out value="${model.testhash['A']}"/>
but I need something like:
<c:out value="${model.testhash[${model.testkey}]}"/>
Is this possible?

Have you tried
${model.testhash[model.testkey]}
In general the ${ } only delineates the JSTL expression, you don't need to escape the lookup for the model.testkey lookup as well, so it is also possible to do:
${model.testhash[model.condition ? 'A' : 'B']}
.. just as an example.

Related

Finding the xpath of a class name with \n and spaces

This may be an easy question, I'm new to this.
I'm trying to get the data within this div
<div class="search-results-listings
" vocab="http://schema.org/" typeof="SearchResultsPage">
response.xpath("//div[#class='search-results-listings\n']")
and
response.xpath("//div[#class='search-results-listings\n ']")
are returning empty arrays
You can use XPath's contains:
response.xpath("//div[contains(#class, 'search-results-listings')]")

Jstl equals doesn't work

The jstl equals method doesn't work for me for some reason. The code is
<span>${fromerror}</span>
<span>${fromerror eq 'mandatoryCriteria.criteria.from'}</span>
yet the result is like this
mandatoryCriteria.criteria.from
false
I'm using jstl 1.2
More specifically I'd need it in if statement but the result is the same
<c:if test="${not empty fromerror and fromerror eq 'mandatoryCriteria.criteria.from'}">
I solved it. The problem was that fromerror variable actually contained something like this
<span id="from.errors">mandatoryCriteria.criteria.from</span>
Which browser evaluated as "mandatoryCriteria.criteria.from". And that's why eq didn't work

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','')}"/>

jstl access a second array value with foreach index

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.

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