@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

版权声明:本文为qq_33326449原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。