<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>
配置就可以了
备忘