Domanda

All -

I'm using stripes to do some form input for a problem I'm working on and I'm stuck on how best to submit a a pair of data using stripes and checkboxes.. for example my page looks like the following:

I have a list of options where users can enable a selection by clicking the box, and also supply some input for that item by entering data into the text field next to it:

<tr>
<td><stripes:checkbox name="item.enable" value="${item.id}"/></td>
<td><stripes:text name="item.value" value="${item.value}"/></td>
</tr>
.....
next item...

When the form is submitted I'd expect my Collection<Item> to be populated yet that's not the case..

How can I best submit a pair of items using the check box fields.

Thanks in advance.

..Chris

È stato utile?

Soluzione

Read the documentation on indexed properties. You need to tell Stripes that you have multiple items, by naming them items[0], items[1], etc.:

<tr>
  <td><stripes:checkbox name="items[0].enable" value="${item.id}"/></td>
  <td><stripes:text name="items[0].value" value="${item.value}"/></td>
</tr>
<tr>
  <td><stripes:checkbox name="items[1].enable" value="${item.id}"/></td>
  <td><stripes:text name="items[1].value" value="${item.value}"/></td>
</tr>

This supposes that you action bean has a setItems(List<Item> items) method, that the Item class has a public no-arg constructor, and has a setEnable(String itemId) and a setValue(String value) method.

Altri suggerimenti

I would wrap this in a JSTL 'forEach' tag and I would put the items in a List. Similar to what JB Nizet said, you also need public setters in the action bean. If you are using Collection<Item> with some implementation other than List<Item> the below snippet won't work.

<c:forEach var='itemIndex' begin='0' end='2'>
    <c:set scope='page' var='item' value='${items[itemIndex]}'>
    <tr>
      <td><stripes:checkbox name="items[${itemIndex}].enable" value="${item.id}"/></td>
      <td><stripes:text name="items[${itemIndex}].value" value="${item.value}"/></td>
    </tr>
</c:forEach>

There is another case when you don't want the list to default to 3 items. The one I'm thinking of is when the list is already populated. If that is the case I would change the 'end' attribute of the <c:forEach> to be: ${fn:length(actionBean.items) == 0 ? 3 : fn:length(actionBean.items)-1}

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top