Spring MVC中@ModelAttribute的目的和用法是什么?
当前回答
@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象
引用http://www.javabeat.net/modelattribute-spring-mvc/
其他回答
所以我会试着用更简单的方式来解释。让我们有:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
正如Spring MVC文档中所描述的那样——@ModelAttribute注释可以用于方法或方法参数。当然我们可以在一个控制器中同时使用这两种方法。
1.方法注释
@ModelAttribute("cities")
public List<String> checkOptions(){
return new Arrays.asList(new[]{"Sofia","Pleven","Ruse"});//and so on
}
这种方法的目的是在模型中添加属性。因此,在我们的案例中,cities key将拥有列表new Arrays.asList(new[]{"Sofia","Pleven","Ruse"})作为模型中的值(您可以将模型看作map(key:value))。在同一个控制器中,在@RequestMapping方法之前调用控制器中的@ModelAttribute方法。
在这里,我们希望向Model添加将在表单中用于显示给用户的公共信息。例如,它可以用来填充HTML选择:
2.方法参数
public String findPerson(@ModelAttriute(value="person") Person person) {
//..Some logic with person
return "person.jsp";
}
An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. So in this case we expect that we have in the Model person object as key and we want to get its value and put it to the method argument Person person. If such does not exists or (sometimes you misspell the (value="persson")) then Spring will not find it in the Model and will create empty Person object using its defaults. Then will take the request parameters and try to data bind them in the Person object using their names.
name="Dmitrij"&countries=Lesoto&sponsor.organization="SilkRoad"&authorizedFunds=&authorizedHours=&
我们有了name,它会使用setName(String name)绑定到person。name。因此,在
//..Some logic with person
我们可以访问这个值为“Dimitrij”的填充名称。
当然,Spring可以绑定更复杂的对象,比如List、map、List Of Sets Of Maps等等,但在幕后,它实现了数据绑定的魔力。
我们可以同时拥有带模型注释的方法和参数中带有@ModelAttribute的请求方法处理程序。那我们就得统一规则。 当然,我们有很多不同的情况——@ModelAttribute方法也可以在@ControllerAdvice中定义,等等……
我知道这是一个老帖子,但我想我把我的帽子扔在戒指里,看看我是否能把水弄浑一点:)
我发现我最初难以理解@ModelAttribute是因为Spring决定将几个注释合并为一个注释。当我把它分成几个更小的注释时,它变得更清晰了:
对于参数注释,可以将@ModelAttribute看作是@Autowired + @Qualifier的等量物,即它试图从Spring托管模型中检索具有给定名称的bean。如果没有找到命名bean,它不会抛出错误或返回null,而是隐式地扮演@Bean的角色,即使用默认构造函数创建一个新实例,并将bean添加到模型中。
对于方法注释,可以认为@ModelAttribute相当于@Bean + @Before,也就是说,它将由用户代码构建的bean放在模型中,并且总是在请求处理方法之前调用它。
比方说,我看到@ModelAttribute如下所示(请不要从字面上理解!!):
@Bean("person")
@Before
public Person createPerson(){
return new Person();
}
@RequestMapping(...)
public xxx handlePersonRequest( (@Autowired @Qualifier("person") | @Bean("person")) Person person, xxx){
...
}
如您所见,Spring做出了正确的决定,使@ModelAttribute成为一个包罗万象的注释;没有人想看到注释的大杂烩。
@ModelAttribute引用Model对象的属性(MVC中的M;) 假设我们有一个表单,它有一个名为Person的表单支持对象 然后你可以让Spring MVC通过使用@ModelAttribute注释将这个对象提供给Controller方法:
public String processForm(@ModelAttribute("person") Person person){
person.getStuff();
}
另一方面,注释用于定义应该成为Model一部分的对象。 所以如果你想在Model中引用Person对象,你可以使用下面的方法:
@ModelAttribute("person")
public Person getPerson(){
return new Person();
}
这个带注释的方法将允许访问视图中的Person对象,因为Spring会自动将它添加到模型中。
参见“使用@ModelAttribute”。
@ModelAttribute将使用您指定的名称(@ModelAttribute(“Testing”)Test Test)创建一个属性,在给定的示例中作为Testing,Test是bean测试,是bean的引用,而Testing将在模型中可用,以便您可以进一步在jsp页面上使用它来检索存储在ModelAttribute中的值。
@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象
引用http://www.javabeat.net/modelattribute-spring-mvc/
推荐文章
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?
- 在Java中什么时候使用可变参数?
- Mockito的argumentCaptor的例子
- 我如何告诉Spring Boot哪个主类用于可执行jar?
- 如何将Java8流的元素添加到现有的列表中
- 在Java 8中是否可以转换流?
- 不区分大小写的字符串作为HashMap键
- 什么是maven中的“pom”打包?
- 在Java中创建一个自定义事件
- 创建正则表达式匹配数组
- 我如何在Java中初始化一个全零的数组列表?
- 主体、使用者和主体之间的意义和区别是什么?
- 将字节转换为十六进制