I am pretty new with JSTL, so need your help with something.
I have a string named “specialties”, in which I’m checking if a particular string exists, and if it does, I check mark the input corresponding to it, and then replacing it with empty string.
<input type="checkbox" name="specialities" value="OB/Gyn"
<c:if test="${fn:containsIgnoreCase(asc_details.specialities, 'OB/Gyn')}">
<c:set var="specialities" value="${fn:replace(specialities,'OB/Gyn','')}"></c:set>
<c:out value="${'checked'}"/></c:if>
/> OB/Gyn
<input type="checkbox" name="specialities" value="Plastic/reconstructive"
<c:if test="${fn:containsIgnoreCase(specialities, 'Plastic/reconstructive')}">
<c:set var="specialities" value="${fn:replace(specialities,'Plastic/reconstructive','')}"></c:set>
<c:out value="${'checked'}"/></c:if>
/> Plastic/reconstructive
It works great for the first matching string “OB/Gyn” and replaces it with empty string, but it doesn’t replace “Plastic/reconstructive” with empty string. I’m not able to figure out why that is happening. Please help.
There’s a possible case sensitivity bug here. The
containsIgnoreCase()test is case insensitive, so it would pass for example “plastic”, “Plastic”, “PlAsTiC” and “PLASTIC”, however thereplace()is case sensitive and it would replace only “Plastic”.That it failed for you most likely means that the value didn’t exactly match the case. Perhaps it’s actually “Plastic/Reconstructive” or so?
You’d basically need to fix the casing (and replace the
containsIgnoreCase()bycontains()), or to perform a case insensitive replacement, however such a function is not available in JSTL. You’d most likely need to homegrow one.