Spring MVC在post方法上使用视图模型绑定多选

时间:2015-04-20 06:20:17

标签: spring spring-mvc

如何在post方法上将多项选择绑定到视图模型? 这是视图模型:

public class AssignEvaluationViewModel {
   private String evaluationType;
   private String milestone;
   private List<AssigneesViewModel> optionsList;
   //getters and setters
}

public class AssigneesViewModel {

   private int evaluatorId;
   private int evaluatedId;
   private String evaluatorName;
   private String evalueatedName;
}

控制器

@RequestMapping(value="addAssignment", method = RequestMethod.GET)
public String addAssignment(Model model){
    // load the list of evaluation type
    List<DropDownListItem> items = new ArrayList<DropDownListItem>();
    items.add(new DropDownListItem("1", "Peer evaluation"));
    items.add(new DropDownListItem("2", "Team member evaluation"));
    items.add(new DropDownListItem("3", "Team evaluation"));

    model.addAttribute("items", items);
    // load the list of milestones
    List<DropDownListItem> milestones = new ArrayList<DropDownListItem>();
    List<MilestoneDTO> dtos = milestoneService.getAll();
    for (MilestoneDTO m : dtos) {
        milestones.add(new DropDownListItem(String.valueOf(m.getId()), m
                .getMilestoneName()));
    }
    model.addAttribute("milestones", milestones);

    model.addAttribute("addAssignment", new AssignEvaluationViewModel());

    return "addAssignment";
}

@RequestMapping(value="addAssignment", method = RequestMethod.POST)
public String addAssignmentPOST(@ModelAttribute("addAssignment") AssignEvaluationViewModel viewModel){

    //save the assignment       
    return "redirect:assignEvaluationForms";

}

问题在于jsp。

<form:form commandName="" modelAttribute="addAssignment" id="addAssignment">
    //..................
    <div class="selectAssignees">
                            <p>Assignees:</p>
                            <form:select multiple="multiple" class="assigneesOptions" path="optionsList" id="assignees">
                            </form:select>
                        </div>
   //..............
</form:form>

如何在AssignEvaluationViewModel的select with optionsList中绑定用户添加的选项?

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您想在Spring中显示选项,可以使用form:options

<form:form commandName="" modelAttribute="addAssignment" id="addAssignment">
    <div class="selectAssignees">
                            <p>Assignees:</p>
                            <form:select multiple="multiple" class="assigneesOptions" path="optionsList" id="assignees">
                            <form:option value="NONE" label="--- Select ---"/>
                            <form:options items="${optionsList}" />
                            </form:select>
                        </div>
</form:form>