Spring mvc checkboxes tag : not sure what is the format of path attr

懵懂的女人 提交于 2020-01-03 05:11:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!