tags:

views:

323

answers:

2

Hi All, I have a model with this method:

public List<String> getPriviledgeNames()

I'm trying to display a list of checkbox in my jstl page, and I'd like to checked only the ones which their name is in the list returned from my model. So I have something like:

<li>
    <input type="checkbox" name ="priviledge" id="CheckBox2" class="checkbox" value="clickthrough" />
    <label for="CheckBox2">Clickthrough</label>
</li>
<li>
    <input type="checkbox" name ="priviledge" id="CheckBox3" class="checkbox" value="details" />
    <label for="CheckBox3">Details</label>
</li>

I'd like to add the checked="checked" only their name is in the list provided by my model's method. Is there a way to do this WITHOUT using scriptlet? I'd like to have no java code inside my pages...

Thanks for any helps! Roberto

A: 
<c:when test = "${fn:contains(list,value)}">

-according to the docs it's actually doing a string/string comparison, so I assume it's actually checking whether the string value is a substring of list.toString().

You could also do a manual test on each value in the list with a forEach tag but that would be messier.

Steve B.
yes the for loop will be a mess, I'll definitely try this solution. I didn't try because I didn't think about the toString() method to be called.
Roberto
this is not working. get an error message but, even if it does, it won't be accurate. I mean if I have a list of these elements:firstElementsecondElementElementand I'm looking for thirdElement the result will be true even if I don't have that value in the list. Maybe the only solution is to use a java script inside the page?
Roberto
A: 

By using a map instead of a list could do something like this:

 <c:if test="${yourModel.priviledgeNames['clickthrough'] ne null}">
checked="checked"</c:if>

Or just this: ${yourModel.priviledgeNames['clickthrough']} by mapping your checkbox name to checked="checked"

svachon