How to split number in a jstl variable - jstl

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

Related

JSTL <c:if> tag to compare two variables

Why the block never reaches "conditionTrue"? I have printed both the variables
authorEmail and scase.authorEmail and they are same string. If I use literal like 'abc#yahoo.com' to test the condition , it works but not if both the c:if params are variables.
<c:forEach var="scase" items="${section.subSectionList.get(0).SCaseList}">
<c:set var="authorEmail" >
<sec:authentication property="principal.email" />
</c:set>
<c:if test = "${authorEmail == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
</c:forEach>
UPDATE - I got it to work by making these changes.
I commented out the entire c:set tag. Instead I added these line -
<sec:authentication var="principal" property="principal" />
My test condition was then :
<c:if test = "${principal.email == scase.authorEmail}" >
<option>conditionTrue</option>
</c:if>
And this worked. However, the reasoning behind it still baffles me. Any insight, much appreciated.

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.

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.

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>

JSTL: I need to access hashtable using a key

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.

Resources