rajputhch
rajputhch

Reputation: 627

Spring:bind with checkbox

In the code below testData is an HashMap. I am trying to bind checkbox with spring:bind tag. I am getting syntax errors on spring:bind. Could you tell me what is the issue in my code?

<c:forEach items="${testData}" var="test" varStatus="loopStatus"> 
     <spring:bind path="${testData[${loopStatus.index}]}.selected"> 
        <input type="hidden" name="_${status.expression}"> 
        <input type="checkbox" name="${status.expression}" value="true"> 
            <c:if test="${status.value}">checked</c:if> 
     </spring:bind> 
   </c:forEach> 

Upvotes: 0

Views: 7481

Answers (3)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

First, in Expression Language, ${ starts your expression and } ends it. You don't need to nest them or anything, so ${testData[${loopStatus.index}]} is your syntax error, and .selected is outside of your expression, so that should just be: ${testData[loopStatus.index].selected}.

Now, in forEach, your var attribute determines the name of the variable that holds the current item. So you rarely need to use varStatus. You can just have your test as ${test.selected}.

Finally, your checked attribute is outside of your checkbox input element!

So:

<c:forEach items="${testData}" var="test"> 
   <spring:bind path="${test.selected}"> 
      <input type="hidden" name="_${status.expression}"> 
      <input type="checkbox" name="${status.expression}" value="true" <c:if test="${status.value}">checked</c:if>>
   </spring:bind> 
</c:forEach>

(not sure you ever need the hidden field, we're not using one for any of our checkboxes).

Upvotes: 0

Javi
Javi

Reputation: 19799

I'm not sure if you have another syntax error but start by changing this:

<spring:bind path="${testData[${loopStatus.index}]}.selected">

for

<spring:bind path="${testData[loopStatus.index]}.selected">

Upvotes: 2

Dave Richardson
Dave Richardson

Reputation: 23

The path in your spring:bind tag is probably wrong. I can't tell you the correct path as you haven't described the object you're binding to.

You may want to use the form:checkbox tag instead of spring bind.

Upvotes: 0

Related Questions