今天遇到个问题,controller跳转index页面的时候,页面内容为空,浏览器console里也没有报错信息,查了半天错发现问题是:
controller层:
ModelAndView mva = new ModelAndView();
mva.setViewName("pages/index");
TargetModel tModel = new TargetModel();
tModel.setAreaCode(areaCode);
tModel.setAreaName(areaName);
tModel.setBegDate(begDate);
tModel.setEndDate(endDate);
tModel.setDateZones(begDate.substring(5)+"至"+endDate.substring(5));
mva.addObject("tModel",tModel);
return mva; --这里没问题
JS页面里这个地方:
areaId= [[${tModel.areaId}]];
controller层的tModel未创建过areaId这个对象,所以前端JS找不到这个对象,导致页面为空白页,并且console中不报错,
而且这段JS即使注释,也还是空白页,改成areaId= [[${tModel.areaCode}]]; 这个后 ,页面才正常加载,
‘通过百度查找’:https://blog.youkuaiyun.com/sinat_33151213/article/details/78788461
当在页面中需要引用JAVA端传递的对象时,可以进行空值判断处理,areaId= [[${tModel?.areaCode}]],这样应该就不会出现空白页的情况了
本文详细解析了在使用ModelAndView跳转页面时,由于对象属性未完全创建导致JS页面显示空白的问题。通过调整JS对对象属性的引用方式,避免了因属性不存在而引发的空白页现象。
247

被折叠的 条评论
为什么被折叠?



