gpt4 book ai didi

java - Controller 上的@ModelAttribute注释有什么用

转载 作者:行者123 更新时间:2023-12-01 06:22:08 27 4
gpt4 key购买 nike

我有一个如下所示的 Controller

@RequestMapping("/getList")
public ModelAndView getNetAllList(@ModelAttribute MYObject myObj) {

//my code here

}

我通过表单提交将此 Controller 称为

<form name="myForm"  th:action="@{/getList}" th:object="${myObjVO}" method="post"

现在我的问题是,即使我删除 @ModelAttribute 注释,此代码也可以正常工作。

这是否意味着新的 spring 不再需要 @ModelAttribute 了。如果不是,Spring 的哪些功能负责表单和模型之间的映射。我正在使用 Spring Boot 2.0.0 和 thymeleaf。

最佳答案

首先,我认为您的代码不起作用。仅当您选择以下任一解决方案时,它才有效。

解决方案1。

@RequestMapping("/getList")
public ModelAndView getNetAllList(@ModelAttribute("myObjVO") MYObject myObj) {

//my code here

}

<form name="myForm" th:action="@{/getList}" th:object="${myObjVO}" method="post">

解决方案2:

@RequestMapping("/getList")
public ModelAndView getNetAllList( MYObject myObj) {

//my code here

}

<form name="myForm" th:action="@{/getList}" th:object="${myObject}" method="post">

如果您仔细研究这些解决方案,您就会知道何时需要 @ModelAttribute

现在回到你的问题

您不需要 @ModelAttribute 只是使用 bean 作为参数。 ModelAttribute 用于各种其他目的。阅读 this文档并检查 stackoverflow 答案中的以下两个。

What is @ModelAttribute in Spring MVC?

@ModelAttribute annotation, when to use it?

关于java - Controller 上的@ModelAttribute注释有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49494832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com