SpringMVC中的Model和ModelAndView详解

原文链接:

0.前言

1.Model是什么?

model是”模型“的意思,是MVC架构中的”M“部分,是用来传输数据的。

2.ModelAndView是什么?

如果翻译过来就是”模型和视图“,可以理解成MVC架构中的”M“和”V“,其中包含”Model“和”view“两部分,主要功能是:

设置转向地址
将底层获取的数据进行存储(或者封装)
最后将数据传递给View
区别?

1.Model只是用来传输数据的,并不会进行业务的寻址。ModelAndView 却是可以进行业务寻址的,就是设置对应的要请求的静态文件,这里的静态文件指的是类似jsp的文件。Model是每次请求中都存在的默认参数,利用其addAttribute()方法即可将服务器的值传递到jsp页面中;ModelAndView包含model和view两部分,使用时需要自己实例化,利用ModelMap用来传值,也可以设置view的名称。

2.Model是每一次请求可以自动创建,但是ModelAndView 是需要我们自己去new的。

1.model的使用

查看Model的源码发现,里面比较重要的就是前4个。

package org.springframework.ui;
 
import java.util.Collection;
import java.util.Map;
 
import org.springframework.lang.Nullable;
 
/**
 * Java-5-specific interface that defines a holder for model attributes.
 * Primarily designed for adding attributes to the model.
 * Allows for accessing the overall model as a {@code java.util.Map}.
 *
 * @author Juergen Hoeller
 * @since 2.5.1
 */
public interface Model {
 
    /**
     * Add the supplied attribute under the supplied name.
     * @param attributeName the name of the model attribute (never {@code null})
     * @param attributeValue the model attribute value (can be {@code null})
     */
    Model addAttribute(String attributeName, @Nullable Object attributeValue);
 
    /**
     * Add the supplied attribute to this {@code Map} using a
     * {@link org.springframework.core.Conventions#getVariableName generated name}.
     * <p><i>Note: Empty {@link java.util.Collection Collections} are not added to
     * the model when using this method because we cannot correctly determine
     * the true convention name. View code should check for {@code null} rather
     * than for empty collections as is already done by JSTL tags.</i>
     * @param attributeValue the model attribute value (never {@code null})
     */
    Model addAttribute(Object attributeValue);
 
    /**
     * Copy all attributes in the supplied {@code Collection} into this
     * {@code Map}, using attribute name generation for each element.
     * @see #addAttribute(Object)
     */
    Model addAllAttributes(Collection<?> attributeValues);
 
    /**
     * Copy all attributes in the supplied {@code Map} into this {@code Map}.
     * @see #addAttribute(String, Object)
     */
    Model addAllAttributes(Map<String, ?> attributes);
 
    /**
     * Copy all attributes in the supplied {@code Map} into this {@code Map},
     * with existing objects of the same name taking precedence (i.e. not getting
     * replaced).
     */
    Model mergeAttributes(Map<String, ?> attributes);
 
    /**
     * Does this model contain an attribute of the given name?
     * @param attributeName the name of the model attribute (never {@code null})
     * @return whether this model contains a corresponding attribute
     */
    boolean containsAttribute(String attributeName);
 
    /**
     * Return the attribute value for the given name, if any.
     * @param attributeName the name of the model attribute (never {@code null})
     * @return the corresponding attribute value, or {@code null} if none
     * @since 5.2
     */
    @Nullable
    Object getAttribute(String attributeName);
 
    /**
     * Return the current set of model attributes as a Map.
     */
    Map<String, Object> asMap();
 
}

Model addAttribute(String attributeName, @Nullable Object attributeValue)
Model addAttribute(Object attributeValue);
Model addAllAttributes(Collection<?> attributeValues);
Model addAllAttributes(Map<String, ?> attributes);
具体用法1:返回一个字符

Controller层的写法

package com.cat.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
 * controller 负责提供访问应用程序的行为,通常通过接口定义或者注解定义两种方法实现。
 * 控制器负责解析用户的请求并将其转换为一个模型。
 * */
@Controller  //代表这个类会被spring接管,被这个注解的类中所有方法,如果返回值是string,并且有具体的页面可以跳转,那么就会被视图解析器解析
public class IndexController {
     
    @RequestMapping("/hello")   //意为请求 localhost:8080/hello 
    public String hello(Model model){
        //封装数据(向模型中添加数据,可以jsp页面直接取出并渲染)
        model.addAttribute("name","张三");
        model.addAttribute("sex","男");
        model.addAttribute("age",23);
        System.out.println(model);
        //会被视图解析器处理
        return "hello";   //返回到哪个页面     
    }
}

jsp写法(注意和路径对应)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
hello.jsp页面
<p>姓名:${name}</p>
<p>性别:${sex}</p>
<p>年龄:${age}</p>
</body>
</html>

在这里插入图片描述
如果出现上面这种不正常的情况,请点击这里。

正常情况如下所示:
在这里插入图片描述
具体用法2:返回一个对象

model方法是可以返回一个对象的。我们创建一个对象在这里插入图片描述
Person实体类,至少要加上get方法。不然前端取不到数据

package com.cat.domain;
 
public class Person {
    public  String name;
    public  String sex;
    public  int age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

在这里插入图片描述
IndexController代码改成下面这样。

package com.cat.controller;
 
import com.cat.domain.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
@RequestMapping
public class IndexController {
    @RequestMapping("/hello")
    public String hello(Model model){
        Person person =new Person();
        person.name="张三";
        person.age=16;
        person.sex="男";
        System.out.println(person);
        model.addAttribute("person",person);
        return "hello";
    }
 
}

hello.jsp改成下面这样子。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
hello.jsp页面
<p>姓名:${person.name}</p>
<p>性别:${person.sex}</p>
<p>年龄:${person.age}</p>
</body>
</html>

页面重新请求后变成下面样子说明请求成功

在这里插入图片描述
返回map和collection类型暂时不做演示。

2. ModelAndView

ModelAndView有的方法和Model很类似,一共有下面这些方法。

构造方法:

ModelAndView()  //默认构造函数豆式的用法:填充bean的属性,而不是将在构造函数中的参数。
ModelAndView(String viewName)  //方便的构造时,有没有模型数据暴露。
ModelAndView(String viewName, Map model)  //给出创建一个视图名称和模型新的ModelAndView。
ModelAndView(String viewName, String modelName, Object modelObject)  //方便的构造采取单一的模式对象。
ModelAndView(View view)   //构造方便在没有模型数据暴露。
ModelAndView(View view, Map model)  //创建给定一个视图对象和模型,新的ModelAndView。
ModelAndView(View view, String modelName, Object modelObject)   //方便的构造采取单一的模式对象。

类方法

ModelAndView  addAllObjects(Map modelMap)  //添加包含在所提供的地图模型中的所有条目。
ModelAndView  addObject(Object modelObject)  //添加对象使用的参数名称生成模型。
ModelAndView  addObject(String modelName,ObjectmodelObject)  //对象添加到模型中。
void  clear()  //清除此ModelAndView对象的状态。
Map  getModel()  //返回的模型图。
protectedMap  getModelInternal()  //返回的模型图。
ModelMap  getModelMap()  //返回底层ModelMap实例(从不为null)。
View  getView()  //返回View对象,或者为null,如果我们使用的视图名称由通过一个ViewResolverDispatcherServlet会得到解决。
String  getViewName()  //返回视图名称由DispatcherServlet的解决,通过一个ViewResolver,或空,如果我们使用的视图对象。
boolean  hasView()  //指示此与否的ModelAndView有一个观点,无论是作为一个视图名称或作为直接查看实例。
boolean  isEmpty()  //返回此ModelAndView对象是否为空,即是否不持有任何意见,不包含模型。
boolean  isReference()  //返回,我们是否使用视图的参考,i.e.
void  setView(Viewview)  //设置此ModelAndView的视图对象。
void  setViewName(StringviewName)  //此ModelAndView的设置视图名称,由通过一个ViewResolverDispatcherServlet会得到解决。
String  toString()  //返回这个模型和视图的诊断信息。
boolean  wasCleared()??  //返回此ModelAndView对象是否为空的调用的结果,以清除(),即是否不持有任何意见,不包含模型。

在Controller中添加一个新的方法。

package com.cat.controller;
 
import com.cat.domain.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping
public class IndexController {
//    @RequestMapping("/hello")
//    public String hello(Model model){
//        Person person =new Person();
//        person.name="张三";
//        person.age=16;
//        person.sex="男";
//        System.out.println(person);
//        model.addAttribute("person",person);
//        return "hello";
//    }
        @RequestMapping("/hello2")
        public ModelAndView hello(){
 
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("hello");  //返回到那个文件
            modelAndView.addObject("name","派大星");
            modelAndView.addObject("sex","男");
            modelAndView.addObject("age",53);
            System.out.println(modelAndView);
            return modelAndView;
        }
}

hello.jsp改成

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
hello.jsp页面
<p>姓名:${name}</p>
<p>性别:${sex}</p>
<p>年龄:${age}</p>
</body>
</html>

请求新的地址后发现数据没有问题。

在这里插入图片描述
同理,ModelAndView也可以返回一个对象。这里就不做演示了。

  • 75
    点赞
  • 337
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
### 回答1: ModelAndViewSpring MVC的一个类,用于封装视图和模型数据。它包含了一个视图名称和一个模型对象,可以将模型数据传递给视图进行渲染。在Controller,我们可以通过返回一个ModelAndView对象来指定要渲染的视图和需要传递给视图的模型数据。同时,ModelAndView还提供了一些方法,如addObject()和setViewName()等,用于设置模型数据和视图名称。 ### 回答2: Spring MVC ModelAndView 是一个用于返回渲染视图和存储模型数据的组件。 在 Spring MVC ,Controller 处理请求并返回 ModelAndView 对象,它包含两个主要部分:模型(Model)和视图(View)。其 Model 是一个存储数据的容器,View 是负责呈现模型数据的视图。 ModelAndView 可以通过构造函数初始化或通过方法设置。如下所示: ```java //通过构造函数初始化 ModelAndView modelAndView = new ModelAndView("viewName", "modelAttributeName", modelAttributeValue); //通过方法设置 modelAndView.setViewName("viewName"); //设置视图名称 modelAndView.addObject("attributeName", attributeValue); //添加模型数据 ``` ModelAndView 对象的构造函数需要提供以下两个参数: 1. View Name:视图名称,用于将模型数据呈现到客户端 2. Model Attribute:要在视图呈现的模型属性名称 例如,以下代码演示了如何创建一个 ModelAndView 对象: ```java //创建ModelAndView对象 ModelAndView modelAndView = new ModelAndView("welcome"); //设置模型数据 modelAndView.addObject("message", "欢迎使用Spring MVC"); ``` 这个 ModelAndView 将视图名设置为 "welcome",它将呈现在客户端。还添加了一个名为 "message" 的模型属性,用于在视图显示欢迎消息。 在视图访问模型属性很简单,只需要使用 EL 表达式或使用 JSTL 标签库即可。 例如,以下代码演示了如何在jsp页面使用模型属性: ```jsp <h1>Welcome to Spring MVC</h1> <p>${message}</p> ``` 使用 ModelAndView 可以更灵活地处理模型和视图。它提供了一种简单的方法来组织和呈现模型数据,并将数据传递给视图,让它们呈现在客户端。因此,ModelAndViewSpring MVC 是非常重要的组件之一。 ### 回答3: Spring是一种Java开发框架,它提供了许多实用的工具和类,可以帮助开发人员快速开发Java应用程序。Spring MVC是Spring框架的一个子框架,它采用了MVC(Model-View-Controller)架构模式,用于构建Web应用程序。 在Spring MVCModelAndView是一个常用的对象,用于封装ModelView,以向客户端提供响应。ModelAndView包含两个部分:ModelViewModel用于在Controller和View之间传递数据,它可以是任何Java对象。Controller可以将数据添加到ModelView可以从Model获取数据。当Controller调用视图时,Model数据将自动传递给ViewView用于渲染Model的数据,生成HTML或其他类型的响应。View可以是JSP或其他模板引擎。Spring支持许多视图技术,包括JSP,Thymeleaf和Freemarker。 ModelAndView的使用非常简单。在Controller,您可以使用ModelAndView对象来设置ModelView。如下所示: ``` @RequestMapping("/hello") public ModelAndView hello() { ModelAndView modelAndView = new ModelAndView("hello"); modelAndView.addObject("message", "Hello, World!"); return modelAndView; } ``` 上面的代码创建了一个ModelAndView对象,将View设置为“hello”,将消息添加到Model。这意味着在View,您可以使用以下代码来访问消息: ``` <h1>${message}</h1> ``` 在此示例,“hello”可以是JSP视图或其他模板引擎视图。ModelAndView还支持其他方法,例如设置重定向或转发,设置异常处理等。 总之,ModelAndViewSpring MVC常用的对象,用于将ModelView组合在一起以向客户端提供响应。它是Web应用程序的构建块之一,开发人员应该熟悉其用法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值