Spring mvc 弹簧型号-”的;模型对象不能为空";
我的一个Spring控制器类中的一个方法Spring mvc 弹簧型号-”的;模型对象不能为空";,spring-mvc,spring-4,Spring Mvc,Spring 4,我的一个Spring控制器类中的一个方法 @RequestMapping(value = "/products/{productId}/specifications", method = RequestMethod.GET) public String setup(@PathVariable("productId") Integer pid, Model m) { //... m.addAttribute(foo); <-- error return "my-page";
@RequestMapping(value = "/products/{productId}/specifications", method = RequestMethod.GET)
public String setup(@PathVariable("productId") Integer pid, Model m) {
//...
m.addAttribute(foo); <-- error
return "my-page";
}
@RequestMapping(value=“/products/{productId}/specifications”,method=RequestMethod.GET)
公共字符串设置(@PathVariable(“productId”)整数pid,型号m){
//...
m、 addAttribute(foo);尽管您没有提供显示foo
引用所指向内容的代码,但可以安全地假设它是空引用
我查看了Github上的项目代码,很清楚这里发生了什么
该方法委托给该方法,该方法使用您的问题所询问的确切消息断言所提供的对象不为null
ModelAndView
方法:
public ModelAndView addObject(Object attributeValue) {
getModelMap().addAttribute(attributeValue);
return this;
}
public ModelMap addAttribute(Object attributeValue) {
Assert.notNull(attributeValue, "Model object must not be null");
if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {
return this;
}
return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
}
ModelMap
方法:
public ModelAndView addObject(Object attributeValue) {
getModelMap().addAttribute(attributeValue);
return this;
}
public ModelMap addAttribute(Object attributeValue) {
Assert.notNull(attributeValue, "Model object must not be null");
if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {
return this;
}
return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
}
公共模型映射addAttribute(对象属性值){
notNull(attributeValue,“模型对象不能为null”);
if(集合的attributeValue实例&((集合)attributeValue.isEmpty()){
归还这个;
}
返回addAttribute(Conventions.getVariableName(attributeValue),attributeValue);
}
引用的foo
是否为空引用?您的代码片段不会显示foo
引用指向的内容。