问题
I am using Spring form checkbox tag. My modelAttribute is bookmarkMapping which in turn contains a list of folderMapping objects based on which I am trying to build checkboxes.
folderMapping object has property isMapping which decides the checked property of checkbox.
I am trying this
<form:checkboxes items="${bookmarkMapping.folderMapping}" path="isMapped" itemValue="isMapped" itemLabel="folderName" />
But it keeps saying isMapped is not a property of bookMarkMapping which is true. But in what way should I give the path attribute so that it can point to a property withing bookmarkMapping.folderMapping list.
Please help.
回答1:
There are two lists involved in the form:checkboxes tag: the list of selected items, and the list available items to select.
The list of selected items is stored in the path
attribute. The path
attribute points to a array or collection of Strings in the model.
The items
attribute points to the list of available items. This list is an attribute in the request. It is usually an Array or a List of Strings representing the labels.
Here is some sample code. The CheckboxesModel contains a List of selectedItems. The request contains the List of availableItems.
Model
public class CheckboxesModel {
List selectedItems;public List getSelectedItems() { return selectedItems; }
public void setSelectedItems(List selectedItems) { this.selectedItems = selectedItems; }
}
Controller
@RequestMapping("/index.do")
public String showCheckboxes(HttpServletRequest request, HttpServletResponse response, Model model) {
CheckboxesModel checkboxesModel = new CheckboxesModel(); List<String> selectedItems = new ArrayList<String>(); checkboxesModel.setSelectedItems(selectedItems); model.addAttribute("checkboxesModel",checkboxesModel); List<String> availableItems = new ArrayList<String>(); availableItems.add("One"); availableItems.add("Two"); availableItems.add("Three"); request.setAttribute("availableItems",availableItems); return "index";
}
View
<form:form action="checkBoxes.do" commandName="checkBoxesModel" method="POST">
<form:checkboxes items="${availableItems}" path="selectedItems"/>
<input type="submit" value="Submit"/>
</form:form>
回答2:
You talked about property named isMapping but you set in the path attribute: isMapped #confused
The path is relative to the modelAttribute. You don't have isMapped property in bookmarkMapping but you have a folderMapping property.
So your path should be folderMapping.isMapped
来源:https://stackoverflow.com/questions/14185879/spring-mvc-checkboxes-tag-not-sure-what-is-the-format-of-path-attr