我和那个男人有类似的问题 this post
android:name=".PrometheusApplication"
模型Employee有一个变量来自另一个模型Department,OneToOne连接与hibernate
在表单中我选择了部门但是当我提交时我有一些错误......
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
// we configure custom PropertyEditor for the given type and property
binder.registerCustomEditor(Department.class, new DepartmentEditor());
}
@RequestMapping(value = { "/new" }, method = RequestMethod.POST)
public String saveEmployee(
@ModelAttribute @Valid Employee employee,
BindingResult result,
ModelMap model ) {
字段“部门”:被拒绝的值1是下拉列表选择中的ID号
<form:select path="department" id="department">
<form:options items="${departments}" itemValue="id" itemLabel="name"></form:options>
</form:select>
message : Field error in object 'employee' on field 'department': rejected value [1]; codes [methodInvocation.employee.department,methodInvocation.department,methodInvocation.com.websystique.springmvc.model.Department,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.department,department]; arguments []; default message [department]]; default message [Property 'department' threw exception; nested exception is java.lang.NullPointerException] --
答案 0 :(得分:0)
AbstractDao的
public void persist(T entity) {
getSession().saveOrUpdate(entity);
}
控制器
@Autowired
DepartmentService depService;
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
LOGGER.info("@InitBinder " + request);
binder.registerCustomEditor(Department.class, new DepartmentEditor(depService));
}
DepartmentEditor
@Override
public void setAsText(String value) throws IllegalArgumentException {
LOGGER.info("value = " + value);
if(!value.isEmpty()){
Department department;
try {
LOGGER.info("value_2 = " + value);
department = depService.findById(new Integer(value));
LOGGER.info("value_3 = " + department);
if (null!=department) {
setValue(department);
} else {
setValue(department);
LOGGER.info("Error ", new IllegalArgumentException("Binding error. Cannot find userAccount with id ["+value+"]"));
}
} catch (NumberFormatException e) {
LOGGER.info("This is Error message ", e);
throw new IllegalArgumentException("Binding error. Invalid id: " + value);
}
} else {
setValue(null);
}
}