Spring MVC中@ModelAttribute的目的和用法是什么?


当前回答

ModelAttribute注释被用作Spring MVC Web应用程序的一部分,可以在两种场景中使用。

首先,它可以用于将数据注入到预jsp加载模型中。这在确保需要JSP来显示所有数据本身时特别有用。注入是通过将一个方法连接到模型来获得的。

其次,它可以用于从现有模型中读取数据,并将其分配给教练方法的参数。

具有一定https://dzone.com/articles/using-spring-mvc%E2%80%99s

其他回答

首先,模型在MVC Spring (MVC =模型,视图,控制器)中使用。也就是说,模型与“视图”一起使用。

这些观点是什么?视图可以被认为是“由我们的后端框架(在我们的例子中是Spring)在html页面的某些部分生成的一些可变数据的html页面”。

所以我们有了模型,它是一个包含数据的实体,被“注入”到视图中。

您可以使用Spring使用几个“视图”库:其中包括JSP、Thymeleaf、Mustache等。

例如,让我们假设我们正在使用Thymeleaf(它们都是相似的。更重要的是,除了JSP, Spring甚至不知道他正在使用哪些视图库。所有模型都是通过Spring的Servlet提供服务的。这意味着对于所有这些视图库,Spring代码都是相同的。你唯一需要改变的是这些html页面的语法,它们位于resources/static/templates中)

resources/static/templates   //All our view web pages are here

控制器负责路由。例如,我们的站点托管在localhost:8080上。我们需要一个显示学生的路径(URL)。让我们假设这在localhost:8080/students上可用。做这个的控制器是StudentController:

@Controller  //Not @RestController
   public class StudentController {

   @GetMapping(/students)
   public String getStudentView() {
       return "student";
   }
}

这段代码的作用是说,如果我们要

localhost: 8080 /学生

然后调用getStudentView()方法。但是注意它应该返回一个字符串。然而,当使用视图库时,控制器被@Controller(而不是@RestController)注释,spring所做的是寻找一个带有方法返回的String名称的html视图页面,在我们的例子中,它将在

/ /静态/模板/ student.html资源

这对于没有数据的静态页面来说已经足够好了。然而,如果我们需要一个带有一些数据的动态页面,Spring提供了另一个很大的优势:getStudentView()上面的方法,也会在底层传递一个模型给我们的视图“student.html”。我们的模型将包含可以使用视图库中的特定语法在“student.html”文件中访问的数据。例如,用百里香叶:

<div th:text="${attribute1}"> </div>

这将访问模型的属性“attribute1”。 我们可以通过模型传递不同的数据。这是通过给它分配各种属性来实现的。有不同的方式分配属性,使用@ModelAttribute:

@Controller  //Not @RestController
   public class StudentController {

   @ModelAttribute(name = "attribute1")
   public int assignAttribute1() {
      return 123454321
   }   // Think it as "model.attribute1 = 123454321"

   @GetMapping(/students)
   public String getStudentView() {
       return "student";
   }
}

上面的代码将为模型(在底层创建)分配一个名为“attribute1”的属性(将其视为一个键),值为12354321。比如“模特”。Attribute1 = 123454321”。

最后,当我们访问url时,模型被传递给视图

localhost: 8080 /学生

注意:所有用@ModelAttribute标注的方法都是在视图返回之前调用的。一旦创建了所有属性,模型就会传递给我们的视图。简单地说,在getStudentView()方法被调用之后,所有带有@ModelAttribute的方法都被调用。

也就是说,上面写的html代码将从浏览器中被视为:

<div> 123454321 </div> // th:text is a command of 
//THymeleaf, and says to substitute the text
// between the tags with the attribute "attribute1" 
// of our model passed to this view.

这是@ModelAttribute的基本用法。

还有另一个重要的用例:

模型可能需要在相反的方向:即,从视图到控制器。在上面描述的情况下,模型从控制器传递到视图。但是,假设用户从我们的html页面发回一些数据。我们可以用模型属性@ModelAttribute捕获它。这已经被其他人描述过了

ModelAttribute注释被用作Spring MVC Web应用程序的一部分,可以在两种场景中使用。

首先,它可以用于将数据注入到预jsp加载模型中。这在确保需要JSP来显示所有数据本身时特别有用。注入是通过将一个方法连接到模型来获得的。

其次,它可以用于从现有模型中读取数据,并将其分配给教练方法的参数。

具有一定https://dzone.com/articles/using-spring-mvc%E2%80%99s

将方法参数或方法返回值绑定到已命名模型属性的注释,暴露在web视图中。

public String add(@ModelAttribute("specified") Model model) {
    ...
}

@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象

引用http://www.javabeat.net/modelattribute-spring-mvc/

我知道我来晚了,但我要引用他们的话, “迟到总比不到好”。让我们开始吧, 每个人都有自己解释事情的方式,让我试着总结一下,用一个例子用几个步骤为你简单解释一下; 假设你有一个简单的表单form.jsp:

<form:form action="processForm" modelAttribute="student">
  First Name : <form:input path="firstName" /> 
  <br/><br/>
  Last Name : <form:input path="lastName" />
  <br/><br/>
  <input type="submit" value="submit"/>
</form:form>

<form:input path="firstName" /> <form:input path="lastName" />这些是Student类中的字段/属性。当表单被调用/初始化时,它们的getter被调用。在表单提交时,它们的setter被调用,它们的值在表单标记中用modelAttribute="student"指示的bean中传输。

我们有StudentController,它包含以下方法:

@RequestMapping("/showForm")
// `Model` is used to pass data between controllers and views
public String showForm(Model theModel) {
    // attribute name, value
    theModel.addAttribute("student", new Student());
    return "form";
}
   
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("student") Student theStudent) {
    System.out.println("theStudent :"+ theStudent.getLastName());
    return "form-details";
}

//@ModelAttribute("student") Student theStudent
//Spring automatically populates the object data with form data 
//all behind the scenes

现在我们终于有了一个form-details.jsp:

<b>Student Information</b>
${student.firstName}
${student.lastName}

回到这个问题,Spring MVC中的@ModelAttribute是什么? 一个来自源代码的示例定义, http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation @ModelAttribute是一个注释,它将一个方法参数或方法返回值绑定到一个命名的模型属性,然后将它公开给一个web视图。

实际发生的是,它获取表单的所有值那些由它提交的值,然后保存它们供你绑定或赋值给对象。它的工作方式类似于@RequestParameter,我们只获得一个参数并将值分配给某个方法参数。 区别在于@ModelAttribute保存所有表单数据,而不是单个参数。它为您创建一个bean,其中包含表单中提交的数据。

概括一下整件事:

步骤1: 请求被发送,我们的方法showForm()运行,一个模型,一个临时bean,被设置为student并转发给表单: 这个模型。addAttribute("student", new student ());

步骤2: 表单属性modelAttribute="student"定义了在表单提交时,模型将更新学生并保存表单的所有参数。

步骤3: 在表单提交时,使用参数@ModelAttribute("student")调用processForm()方法:使用modelAttribute="student"的表单中保存的值被提取并分配给student对象中的字段。

步骤4: 然后我们使用它,因为我们出价,就像在页面上显示它等,就像我做的

我希望它能帮助你理解这个概念。谢谢