首先交代背景:
实体关联,客户流失实体CustomerLoss(1),客户流失暂缓措施实体CustomerReprieve(多),在实体里面做关联:
然后在mapper文件做类关联
客户流失暂缓措施controller查询、修改方法:
/**
* 查询客户流失暂缓措施列表
* @param cusId
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/list")
public String list(@RequestParam(value="lossId",required=false)String lossId,HttpServletResponse response)throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
map.put("lossId", lossId);
List<CustomerReprieve> list = customerReprieveService.find(map);
JSONObject result = new JSONObject();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]{"customerLoss"});//排除掉customerLoss,否则会报错
----------
"JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig);"//注意这段代码
----------
result.put("rows", jsonArray);
ResponseUtil.write(response, result);
return null;
}
/**
* 保存或修改客户流失暂缓措施信息
* @param customerReprieve
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(CustomerReprieve customerReprieve,HttpServletResponse response)throws Exception{
int resultTotal = 0;
if(customerReprieve.getId()==null){
resultTotal = customerReprieveService.add(customerReprieve);
}else{
resultTotal = customerReprieveService.update(customerReprieve);
}
JSONObject result = new JSONObject();
if(resultTotal>0){
result.put("success",true);
}else{
result.put("success",false);
}
ResponseUtil.write(response, result);
return null;
}
前台调用:
saveUrl:'${pageContext.request.contextPath}/customerReprieve/save.do?customerLoss.id=${param.lossId}',
updateUrl:'${pageContext.request.contextPath}/customerReprieve/save.do',
注意横线中间的绿色代码,在代码这样写的时候就会报错:
JSONArray jsonArray = JSONArray.fromObject(list);//不使用javaConfig排除关联类
在调用save的时候没出现问题,但是在调用update的时候却报错了,错误日志如下:
Servlet.service() for servlet [springMVC] in context with path [/CRMStudy] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'customerLoss[confirmLossTime]' of bean class [com.wwr.entity.CustomerReprieve]: Property referenced in indexed property path 'customerLoss[confirmLossTime]' is neither an array nor a List nor a Map; returned value was []] with root cause
org.springframework.beans.InvalidPropertyException: Invalid property 'customerLoss[confirmLossTime]' of bean class [com.wwr.entity.CustomerReprieve]: Property referenced in indexed property path 'customerLoss[confirmLossTime]' is neither an array nor a List nor a Map; returned value was []
我的想法是这样:
本身我在类之间做了关联,当我在修改客户流失暂缓措施实体(CustomerReprieve)的时候,他会先去级联客户流失实体(CustomerLoss),相当于把客户流失表(CustomerLoss)的字段当成客户流失暂缓措施表(CustomerReprieve)的字段,因为报错信息中的”confirmLossTime”这个字段就是客户流失表(CustomerLoss)的,所以我们必须在CustomerReprieveController的list中利用javaConfig将CustomerLoss的信息过滤掉,才不会报错。
但其中的原理性问题并不是那么清楚,大家如有高见,劳驾留言,帮忙解答,谢谢!