后台controller返回给前端页面值的方式Model、ModelAndView、redirect、ajax的json返回

1.Model

Model是接口,接口源码如下

public interface Model {

	
	Model addAttribute(String attributeName, Object attributeValue);

	
	Model addAttribute(Object attributeValue);

	
	Model addAllAttributes(Collection<?> attributeValues);

	
	Model addAllAttributes(Map<String, ?> attributes);

	
	Model mergeAttributes(Map<String, ?> attributes);

	
	boolean containsAttribute(String attributeName);

	
	Map<String, Object> asMap();

}

Model在页面的使用

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "/toVm")
    public String toVm(Model view){
        //Model格式key-value添加元素与值
        view.addAttribute("name","张三");
        view.addAttribute("age","李四");
        //通过页面地址返回页面
        return "ym/testVm";
    }
}

 


版权声明:本文为weixin_41926301原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。