springboot 数据模型


springboot 数据模型

 

 

************************

相关类及接口

 

ModelAndView

public class ModelAndView {
    @Nullable
    private Object view;
    @Nullable
    private ModelMap model;
    @Nullable
    private HttpStatus status;
    private boolean cleared = false;

********************
构造函数

    public ModelAndView() {
    public ModelAndView(String viewName) {
    public ModelAndView(View view) {
    public ModelAndView(String viewName, @Nullable Map model) {
    public ModelAndView(View view, @Nullable Map model) {
    public ModelAndView(String viewName, HttpStatus status) {
    public ModelAndView(@Nullable String viewName, @Nullable Map model, @Nullable HttpStatus status) {
    public ModelAndView(String viewName, String modelName, Object modelObject) {
    public ModelAndView(View view, String modelName, Object modelObject) {


********************
常用方法

    public void setViewName(@Nullable String viewName) {
    public String getViewName() {     //视图名称

    public void setView(@Nullable View view) {
    public View getView() {
    public boolean hasView() {         //视图解析器

    protected Map getModelInternal() {
    public ModelMap getModelMap() {
    public Map getModel() {  //获取属性


    public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
    public ModelAndView addObject(Object attributeValue) {
    public ModelAndView addAllObjects(@Nullable Map modelMap) {
                                             //添加属性

    public void clear() {
        this.view = null;
        this.model = null;
        this.cleared = true;
    }  //清空属性

 

ModelMap:modelAndView使用的存储数据的属性字段

public class ModelMap extends LinkedHashMap {

******************
构造方法

    public ModelMap() {
    public ModelMap(String attributeName, @Nullable Object attributeValue) {
    public ModelMap(Object attributeValue) {

******************
常用方法

    public ModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {

    public ModelMap addAttribute(Object attributeValue) {
        Assert.notNull(attributeValue, "Model object must not be null");
        return attributeValue instanceof Collection && ((Collection)attributeValue).isEmpty() ? this : this.addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
    }


    public ModelMap addAllAttributes(@Nullable Collection attributeValues) {

    public ModelMap addAllAttributes(@Nullable Map attributes) {
    public ModelMap mergeAttributes(@Nullable Map attributes) {
 
    public boolean containsAttribute(String attributeName) {
    public Object getAttribute(String attributeName) {

 

Model接口

public interface Model {

    Model addAttribute(String var1, @Nullable Object var2);
    Model addAttribute(Object var1);

    Model addAllAttributes(Collection var1);

    Model addAllAttributes(Map var1);
    Model mergeAttributes(Map var1);

    boolean containsAttribute(String var1);

    @Nullable
    Object getAttribute(String var1);

    Map asMap();
}

 

BindingAwareModelMap:实际创建的用来存储数据的对象

public class BindingAwareModelMap extends ExtendedModelMap {

**************
构造方法

    public BindingAwareModelMap() {
    }

**************
普通方法

    public Object put(String key, Object value) {
    public void putAll(Map map) {

    private void removeBindingResultIfNecessary(Object key, Object value) {

 

ExtendedModelMap

public class ExtendedModelMap extends ModelMap implements Model {

********************
构造函数

    public ExtendedModelMap() {
    public ExtendedModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
    public ExtendedModelMap addAttribute(Object attributeValue) {


********************
普通方法

    public ExtendedModelMap addAllAttributes(@Nullable Collection attributeValues) {
    public ExtendedModelMap addAllAttributes(@Nullable Map attributes) {
    public ExtendedModelMap mergeAttributes(@Nullable Map attributes) {

    public Map asMap() {

 

 

************************

示例

 

Hello2Controller

@RestController
public class Hello2Controller {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("name","瓜田李下");
        mv.addObject("hello world");

        return mv;
    }

    @RequestMapping("/hello2")
    public ModelAndView hello2(ModelMap modelMap){
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());

        modelMap.addAttribute("name","海贼王");
        modelMap.addAttribute("hello world2");

        return mv;
    }

    @RequestMapping("/hello3")
    public ModelAndView hello3(Model model){
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());

        model.addAttribute("name","火影忍者");
        model.addAttribute("hello world3");
        
        return mv;
    }
}

说明:model、modelMap会自动绑定到视图modelAndView上

 

*************************

使用测试

 

/hello

      

 

/hello2

      

 

/hello3

      

 

 

你可能感兴趣的:(springboot,springboot,数据模型)