利用SpringMVC进行添加操作时,因为Employee类里面有个Department类型的属性,所以页面有一句
<form:select path="department.id" items="${departments}" itemValue="id" itemLabel="name"/>
结果这一句提示了IllegalArgumentException:Bean object must not be null,这个异常,此时controller里面的对应的方法为:
@RequestMapping(value="/add",method=RequestMethod.GET)
public String addEmplyee(Model model,Emplyee emplyee){
model.addAttribute("departments",departmentService.getAllDepartments());
model.addAttribute("emplyee",new Emplyee());
return "/add";
}
我以为是因为department属性初始为null导致的这个异常,便在Employee类的构造方法中加入了一句
department = new Department();
结果还是报这个错,然后我便去上网上搜关于SpringMVC表单的select标签的写法,在确认写法没有问题之后,我又去搜别人controller里面方法的写法,也是类似。
折腾了一个小时左右,我便怀疑是不是${departments}为空。
利用Junit测试了一下,果然为空,想起来因为我昨晚改过department表的列名,而我使用的mybatis,没有修改BaseResultMap,导致查询结果其实确实为空,当然这个问题,可能只是我个人导致的,写这儿只是为了记录下。
之所以修改department表的列名,是因为列名id、name和employee表列名id、name重复了,在mybatis的mapper.xml中,配置级联时,因为列名重复,导致mybatis无法区分属性,会将employee的id和name属性值赋给department,所以我才修改了department表的列名,应该把mapper.xml中的sql语句都相应修改下的,谨记!!