spring mvc 表单提交 报异常org.springframework.validation.BindException:

					<form id="personal" action="<%=basePath%>secure/details/personalImprove" name="personal" method="post">
					<input type="hidden" id="cpInstitutionId" name="cpInstitutionId" value="${details.cpInstitutionId}"/>
					<input type="hidden" id="cpEntitieId" name="cpEntitieId" value="${details.cpEntitieId}"/>
					</form>

以上为表单personal ,省略一些其他字段,对应pojo为以下,省略set/get方法和其他字段

public class CustomerInfo
{
	// 自增机构id
	private long cpInstitutionId;
	// 子机构id
	private long cpEntitieId;
}

对应controller为以下
	@RequestMapping(value = "personalImprove", method = RequestMethod.POST)
	public String personalImprove(CustomerInfo personal, HttpServletRequest request)
	{//省略}
cpInstitutionId
cpEntitieId
在新建的时候,form表单提交的这两个字段为""空字符串
此时,点击页面提交抛异常
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'customerInfo' on field 'cpEntitieId': rejected value []; codes [methodInvocation.customerInfo.cpEntitieId,methodInvocation.cpEntitieId,methodInvocation.long,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerInfo.cpEntitieId,cpEntitieId]; arguments []; default message [cpEntitieId]]; default message [Property 'cpEntitieId' threw exception; nested exception is java.lang.IllegalArgumentException]
Field error in object 'customerInfo' on field 'cpInstitutionId': rejected value []; codes [methodInvocation.customerInfo.cpInstitutionId,methodInvocation.cpInstitutionId,methodInvocation.long,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerInfo.cpInstitutionId,cpInstitutionId]; arguments []; default message [cpInstitutionId]]; default message [Property 'cpInstitutionId' threw exception; nested exception is java.lang.IllegalArgumentException]
解决办法
第一种:将
CustomerInfo实体的该两个字段改成基本类型对应引用类型即Long型 此时可以顺利提交
第二种:自定义类实现Converter接口
public class StringToLong implements Converter<String, Long>
{
	@Override
	public Long convert(String text)
	{
		if (text.isEmpty())
			return 0L;
		else
			return Long.parseLong(text);
	}

}

然后在spring配置文件中
	<bean id="conversionService"
		class="org.springframework.context.support.ConversionServiceFactoryBean">
		<property name="converters">
			<list>
				<bean class="org.mySpring.utils.StringToLong" />
			</list>
		</property>
	</bean>
配置就可以了
备忘

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值