java - 将所选属性传递到表单中的post请求

  显示原文与译文双语对照的内容

我有一个带有下拉列表的窗体:


<div class="form-group">
 <form:label path="departments">Dept. Code</form:label>
 <form:select path="departments" items="${departmentMap}" multiple="true"/>

departmentMap 来自控制器方法:


@RequestMapping(value ="/officeForm", method=RequestMethod.GET) 
public ModelAndView showOfficeForm() {
 ModelAndView result = new ModelAndView("officeForm","command", new Office());
 List<Department> departmentsToDisplay = departmentServiceImpl.findAll();
 Map<Department, String> departmentMap = new HashMap<Department, String>();
 for (Department d : departmentsToDisplay) {
 departmentMap.put(d, d.getDepartmentName());
 }

 result.addObject("departmentMap", departmentMap);
 return result;
}

Post方法:


 @RequestMapping(value ="/addOffice", method = RequestMethod.POST)
 public ModelAndView updateOffice(@ModelAttribute("office") Office office, BindingResult result) {

 System.out.println("Office Name:" + office.getOfficeName());
 System.out.println("Departments:" + office.getDepartments());


 return new ModelAndView("result","command", office);
 }

来自 Office.java的摘录:


@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy ="offices")
 private List<Department> departments;

来自 Department.java的摘录:


@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
 @JoinTable(name="OFF_DEPT_T",
 joinColumns={@JoinColumn(name="DEPT_ID", referencedColumnName="ID")},
 inverseJoinColumns={@JoinColumn(name="OFF_ID", referencedColumnName="ID")}
 )
 private List<Office> offices = new ArrayList<Office>();

如果我打印 response.getAllErrors(),我得到:


Field error in object 'office' on field 'departments': rejected value [package.domain.Department@5597e5cf,package.domain.Department@2d14d0a7]; codes [typeMismatch.office.departments,typeMismatch.departments,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [office.departments,departments]; arguments []; default message [departments]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'departments'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [package.domain.Department] for property 'departments[0]': no matching editors or conversion strategy found]

谁能告诉我如何传递对象,但显示字符串? 谢谢。

时间: 作者:

问题 1:

因为你缺少列表 departments 初始化。 在将它的放入模型中之前,应先将它的初始化,然后替换:


 ModelAndView result = new ModelAndView("officeForm","command", new Office());

使用:


 Office office = new Office():
 office.setDepartments(new ArrayList<Department>()):
 ModelAndView result = new ModelAndView("officeForm","command", office);

或者,如果不想在控制器中初始化,可以在创建 Office 对象时初始化它,如下所示:


 private List<Department> departments = new ArrayList<Department>();

问题 2:

你需要在 select 路径中绑定自定义对象( 部门) 列表,你需要向数据绑定器提供一个自定义属性编辑器,如下所示:

首先创建一个属性编辑器类,类似于:


public class DepartmentEditor extends PropertyEditorSupport {

 @Override
 public void setAsText(String text) throws IllegalArgumentException {
 Department department = new department();
 department.setName(text);
 setValue(department);
 }
}

然后通过注册属性编辑器。 你可以通过简单地将 initBinder 方法放在如下所示的控制器类中,从而可以 register:


 @InitBinder
 public void initBinder(WebDataBinder binder) {
 binder.registerCustomEditor(Department.class, new DepartmentEditor());
 }

现在,你的代码应该能正常工作。

作者:

我将命令绑定到一个新的Office 属性。


 @RequestMapping(value ="/officeSearch", method=RequestMethod.GET) 
public String showOfficesSearch(Model model) {
 model.addAttribute("command", new Office());
 return"officeSearch";
}

作者:
...