我有一个带下拉列表的表单:
<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]
有人能告诉我如何传递对象但显示字符串吗?感谢。
答案 0 :(得分:1)
问题1:
因为您缺少List 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
路径中绑定自定义对象(部门)列表,则需要为数据绑定器提供自定义属性编辑器,如下所示:
首先创建一个Property Editor类,如下所示:
public class DepartmentEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Department department = new department();
department.setName(text);
setValue(department);
}
}
然后注册属性编辑器。您只需在控制器类中添加initBinder
方法就可以注册它,如下所示:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Department.class, new DepartmentEditor());
}
现在,您的代码应该可以正常工作。
答案 1 :(得分:0)
我将命令绑定到新的办公室属性。
@RequestMapping(value = "/officeSearch", method=RequestMethod.GET)
public String showOfficesSearch(Model model) {
model.addAttribute("command", new Office());
return "officeSearch";
}