2015-07-28 65 views
0

我與男人同樣的問題從 this post@InitBinder爲Spring MVC的

@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) { 

的模範員工有一個變量從另一個模型與Hibernate

部,OneToOne連接在我選擇的形式該部門,但是當我提出我有一些錯誤...

<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] -- 

場「部門」:拒絕值1我■從下拉選擇的ID號

@Component 
public class DepartmentEditor extends PropertyEditorSupport{ 

    @Autowired 
    DepartmentService depService; 

    @Override 
    public String getAsText() { 
     return super.getAsText(); 
    } 

    @Override 
    public void setAsText(String value) throws IllegalArgumentException { 
     if(value.isEmpty()){ 
      try { 
       Department dep = depService.findById(Integer.parseInt(value)); 
       if (null!=dep) { 
        setValue(dep); 
       } else { 
        throw new IllegalArgumentException("Binding error. Cannot find userAccount with id ["+value+"]"); 
       } 
      } catch (NumberFormatException e) { 
       throw new IllegalArgumentException("Binding error. Invalid id: " + value); 
      } 
     } else { 
      setValue(null); 
     } 
    } 
} 
+0

不確定,但在你的setAsText方法中,你不需要找到你的部門。而不是Dep = depService.findById(Integer.parseInt(value));你可以試試這個:Department dep = new Department(); dep.setId(的Long.parseLong(文本));的setValue(DEP); –

+0

在日誌中我發現這 2015-07-28 15:54:08 TRACE BasicBinder:81 - 綁定參數[1]爲[VARCHAR] - [zzzzzz] 這意味着initbinder綁定錯誤的態度從請求...它需要一個輸入不是選擇....它必須採取一個數字從選擇 –

+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); 
    } 
}