JSP/EL String Concatenation
Is there an easy way to concatenate strings using EL in a JSP without using Javascript?
I need this to track persistent data on the JSP. Currently, there is a multiple select box, and upon submittal, the values are stored in a comma delimited string. The original statement went something like this...
// for loop
<c:when test="${fn:contains(status.value, node.id.orgId}">
// have this option be selected after posting
</c:when>
'status.value' is the persistent data
'node.id.orgId' is one of the individual options
Problem with this is that the values range from 1 - 18, so when a user selects 12 (or anything 10 - 18) from the list and submits, the 1, 2, and 12 options are now selected because 1 and 2 are also substrings of 12. So the simplest solution I have come up with is to do something like the following...
// for loop
<c:when test="${status.value == node.id.orgId || fn:contains(status.value, CONCAT(node.id.orgId, ','))}">
// have this option be selected after posting
</c:when>
Theoretically, if I somehow concatenate the 'orgId' and a comma (since the persistent data is comma delimited), then the test would work correctly. Well, unless it is the last option in the persistent string...Bah!
Suggestions please? Better solutions?
Thanx,
~ Jordan
First to answer the question, the easiest way to contatenate things is with the <c:set> tag
<c:set var="result" value="${node.id.orgId},"/>
No special function required.
However I don't think that this will help you.
You had a problem before that the response "12" matched "1", "2" and "12" since they are all substrings of 12.
Unfortunately adding a comma to the string won't help, unless you add one on both sides.
For instance consider "1, 2, 11, 12, 13"
Looking for "1," would match both 1 and 11.
To make it work you would have to look for ",1,"
There must be a better way.
1 - consider moving this sort of logic into java code - servlet/bean/function.
It is very easy to get an array from a comma separated string (string.split() method) You could then iterate through and find a match.
JSTL is more suited for presenting data rather than processing it.
2 -
<c:forEach var="val" items="${status.value}">
<c:if test="${val == nodeId.orgId}">
// have this option be selected.
</c:if>
</c:forEach>
3 - consider turning the logic around.
Instead of looping through all the possible values in the list, loop through all the items you have selected, and match it that way.
I don't know the context you are using this in, so not sure if it applies here, but sometimes it pays to take a step back, and look at another approach.
Hope this helps,
evnafets
Thanx! And good catch with the comma on both sides. :D
The comma solution may not be ideal, but it is the simplest for what has already been implemented and doesn't cost anything significant as far as reducing memory and time resources. If I were to do it again from scratch, maybe I'd design it a bit different so I wouldn't have to use this work-around. Here's the final solution, and I had to wrap the persistent string in commas as well...
<c:set var="selectedList" value=",${status.value},"/>
// multiple selectbox statement
// for loop
<c:set var="orgId" value=",${node.id.orgId},"/>
<c:choose>
<c:when test="${fn:contains(selectedList, orgId)}">
// select this option after posting
</c:when>
// otherwise
</c:choose>
// end for loop
// end multiple selectbox statement
Appreciate the help immensly,
~ Jordan