public class User {
int id;
String username;
String password;
String realName;
String email;
String phone;
Set<Role> roles;
}
UserAction.java
<span style="white-space:pre"> </span>@RequestMapping(value = "list")
@ResponseBody
public List list(@RequestParam Map map) {
List<User> list = userService.list(map);
return list;
}
<span style="white-space:pre"> </span>@RequestMapping(value = "update")
<span style="white-space:pre"> </span>@ResponseBody
<span style="white-space:pre"> </span>public User update(User user) {
<span style="white-space:pre"> </span>userService.update(user);
<span style="white-space:pre"> </span>return user;
<span style="white-space:pre"> </span>}
UserService.java
<span style="white-space:pre"> </span>public List<User> list(Map map) {
List<User> list = userDAO.list(map);
for (User user : list) {
user.setRoles(null);
}
return list;
}
因为User类中包含Set成员,由于Hibernate的延迟加载,所以在业务层吧roles置为null了。
所以list方法的返回结果如下:
[{"id":1,"username":"admin","password":"admin","realName":"张三","email":"123123@qq.com","phone":"23423234","roles":null},{"id":2,"username":"erwt","password":"admin","realName":"张三","email":"123123@foxmail.com","phone":"3423432432","roles":null}]
前台用easyui的datagrid控件显示和编辑数据:
<table id="dg" title="用户列表" class="easyui-datagrid" style="width:100%;height:auto;margin:0 auto;"
data-options="" idField="id"
rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr >
<th field="id" width="60" data-options="align:'center', halign: 'center'">id</th>
<th field="username" width="100" editor="text" data-options="align:'left', halign: 'center'">用户名</th>
<!-- 如果field 值easyui datagrid找不到的话,它会使用formatter的规则来绑定数据; -->
<th field="password" width="100" editor="text" hidden="true">登录密码</th>
<th field="realName" width="100" editor="text" align="center">真实姓名</th>
<th field="email" width="160" align="center" editor="text">邮箱</th>
<th field="phone" data-options="align:'left', halign: 'center'"
width="100" editor="text" >联系电话</th>
</tr>
</thead>
</table>
这样编辑easyui的表格数据后保存,报异常
WARN DefaultHandlerExceptionResolver:189 - Handler execution resulted in exception: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'user' on field 'roles': rejected value []; codes [typeMismatch.user.roles,typeMismatch.roles,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.roles,roles]; arguments []; default message [roles]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'roles'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.pelin.webapp.ms.entity.Role] for property 'roles[0]': no matching editors or conversion strategy found]
解决方案:
之前一直以为是springMVC的参数绑定功能和easyui的表格数据编辑功能不兼容,于是就去看源码,希望找到根本原因,弄的头都大了,还是找到原因。后来把roles置null改为roles.clear().居然能正常更新编辑了。
UserService.java
public List<User> list(Map map) {
List<User> list = userDAO.list(map);
for (User user : list) {
user.getRoles().clear();
}
return list;
}
修改后list返回结果:
[{"id":1,"username":"admin","password":"admin","realName":"张三","email":"123123@qq.com","phone":"23423234","roles":[]},
{"id":2,"username":"erwt","password":"admin","realName":"张三","email":"123123@foxmail.com","phone":"3423432432","roles":[]}]