springMVC中使用注解时出现嵌套对象空指针异常的解决方法

org.springframework.beans.NullValueInNestedPathException: Invalid property 'brand' of bean class [when.we.collide.Product]: Value of nested property 'style' is null

在使用annotation之前,使用springMVC提交表单之前都会执行formBackingObject(),使用注解之后,仍需要执行类似的方法,否则在绑定页面的值的时候,spring mvc根据反射得到getStyle()方法,然后invoke该方法,此时style为null,返回null,于是抛出异常,可以采用@ModelAttribute注解的方式解决:

	@ModelAttribute  
	public Product setupModelAttribute() {   
	    Product product = new Product();
	    Style style= new Style();
	    productInfo.setStyle(style);
	    return product;
	}   
	
 

猜你喜欢

转载自copperfield.iteye.com/blog/1017630