springMVC绑定参数时报错DefaultHandlerExceptionResolver解决办法

14 篇文章 0 订阅
9 篇文章 0 订阅
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":[]}]


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值