spring mvc中的@ModelAttribute注解示例介绍

所属分类: 软件编程 / java 阅读数: 31
收藏 0 赞 0 分享

前言

本文介绍在spring mvc中非常重要的注解@ModelAttribute.这个注解可以用在方法参数上,或是方法声明上。这个注解的主要作用是绑定request或是form参数到模型对象。可以使用保存在request或session中的对象来组装模型对象。注意,被@ModelAttribute注解的方法会在controller方法(@RequestMapping注解的)之前执行。因为模型对象要先于controller方法之前创建。

请看下面的例子

  • ModelAttributeExampleController.java 是controller类,同时包含@ModelAttribute 方法。
  • UserDetails.java是本例中的模型对象
  • 最后是spring的配置文件
//ModelAttributeExampleController.java
package javabeat.net;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ModelAttributeExampleController {
 @Autowired
 private UserDetails userDetails;
 @RequestMapping(value="/modelexample")
 public String getMethod(@ModelAttribute UserDetails userDetails){
 System.out.println("User Name : " + userDetails.getUserName());
 System.out.println("Email Id : " + userDetails.getEmailId());
 return "example";
 }

 //This method is invoked before the above method
 @ModelAttribute
 public UserDetails getAccount(@RequestParam String user, @RequestParam String emailId){
 System.out.println("User Value from Request Parameter : " + user);
 userDetails.setUserName(user);
 userDetails.setEmailId(emailId);
 return userDetails;
 }
}
//UserDetails.java
package javabeat.net;

public class UserDetails {
private String userName;
private String emailId;
public String getUserName() {
 return userName;
}
public void setUserName(String userName) {
 this.userName = userName;
}
public String getEmailId() {
 return emailId;
}
public void setEmailId(String emailId) {
 this.emailId = emailId;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://www.springframework.org/schema/jms 
 
 http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">
 <context:component-scan base-package="org.spring.examples" />
 <bean id="userDetails" class="org.spring.examples.UserDetails"/>
</beans>

- 上面的例子,getAccount方法使用@ModelAttribute注解。这意味着方法会在controller的方法之前执行。这个方法会使用request的参数设置模型对象。这是一种在方法中设置值的途径。

- 另一种@ModelAttribute注解的使用方法,是用在方法的参数上。在调用方法的时候,模型的值会被注入。这在实际使用时非常简单。将表单属性映射到模型对象时,这个注解非常有用。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Java concurrency之锁_动力节点Java学院整理

这篇文章主要为大家详细介绍了Java concurrency之锁的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java8新特性之StampedLock_动力节点Java学院整理

本文从synchronized、Lock到Java8新增的StampedLock进行对比分析,对Java8新特性之StampedLock相关知识感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Java8新特性之lambda的作用_动力节点Java学院整理

我们期待了很久lambda为java带来闭包的概念,但是如果我们不在集合中使用它的话,就损失了很大价值。现有接口迁移成为lambda风格的问题已经通过default methods解决了,在这篇文章将深入解析Java集合里面的批量数据操作解开lambda最强作用的神秘面纱。
收藏 0 赞 0 分享

Java8新特性之Base64详解_动力节点Java学院整理

这篇文章主要为大家详细介绍了Java8新特性之Base64的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java8新特性之JavaFX 8_动力节点Java学院整理

这篇文章主要介绍了Java8新特性之JavaFX 8的相关知识,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

将本地jar包安装进入maven仓库(实现方法)

下面小编就为大家带来一篇将本地jar包安装进入maven仓库(实现方法)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈Java finally语句到底是在return之前还是之后执行(必看篇)

下面小编就为大家带来一篇浅谈Java finally语句到底是在return之前还是之后执行(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于Java并发容器ConcurrentHashMap#put方法解析

下面小编就为大家带来一篇基于Java并发容器ConcurrentHashMap#put方法解析。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解Spring Boot Profiles 配置和使用

本篇文章主要介绍了详解Spring Boot Profiles 配置和使用,具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

详解Spring Boot 属性配置和使用

本篇文章主要介绍了详解Spring Boot 属性配置和使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多