@ModelAttribute详解

@ModelAttribute简介

@ModelAttribute是Spring MVC最重要的注释之一。

1、此注释可作为方法参数或方法声明之前使用。

2、此注释的主要目的是将请求参数或表单字段绑定到模型对象。模型对象可以使用请求参数(如示例中所示)或已经存储在会话对象中来生成。

3、注意,这个@ModelAttribute方法是在调用@RequestMapping的控制器方法之前调用的。序列背后的逻辑是,在控制器方法内部开始任何处理之前,必须创建模型对象

下面是@ModelAttribute的源码,可以看得更直观一些

/**
 * Annotation that binds a method parameter or method return value
 * to a named model attribute, exposed to a web view. Supported
 * for controller classes with {@link RequestMapping @RequestMapping}
 * methods.    #简要理解就是可以作为方法参数和方法声明之前使用
 *
 * <p>Can be used to expose command objects to a web view, using
 * specific attribute names, through annotating corresponding
 * parameters of an {@link RequestMapping @RequestMapping} method.
 *
 * <p>Can also be used to expose reference data to a web view
 * through annotating accessor methods in a controller class with
 * {@link RequestMapping @RequestMapping} methods. Such accessor
 * methods are allowed to have any arguments that
 * {@link RequestMapping @RequestMapping} methods support, returning
 * the model attribute value to expose.
 *
 * <p>Note however that reference data and all other model content is
 * not available to web views when request processing results in an
 * {@code Exception} since the exception could be raised at any time
 * making the content of the model unreliable. For this reason
 * {@link ExceptionHandler @ExceptionHandler} methods do not provide
 * access to a {@link Model} argument.
 *
 * @author Juergen Hoeller
 * @author Rossen Stoyanchev
 * @since 2.5
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelAttribute {

	/**
	 * Alias for {@link #name}.
	 */
	@AliasFor("name")
	String value() default "";
            .
            .后面还有,这是截取了一部分

Example

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;
	}
}

参考资料:

相关资料参考1

相关资料参考2

reloated posts

  1. @PathVariable – URI Template Patterns in Spring MVC
  2. @RequestHeader in Spring MVC
  3. Spring MVC Annotations
  4. How to get current username in Spring Security?
  5. Exception Handling With @ControllerAdvice in Spring 3.2
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独步秋风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值