@ModelAttribute注解有多种使用方法,具体可分为如下几种:
1 用来注解一个无返回值的方法;
2 用来注解一个有返回值的方法;
3 用来注解一个方法的属性;
4 用@RequestMapping注解同时使用 。
具体请看下面的示例:
1 新建Account类,内容如下:
package com.gm.springmvc_test;
public class Account {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2 新建一个IndexController类,内容如下:
package com.gm.springmvc_test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gm.springmvc_test.Account;
@Controller
public class IndexController {
/**
* ModelAttribute注解用于属性中
* @param name
* @param account
* @param code
* @param test
* @param model
* @return
*/
@RequestMapping(value = "/index/{name}")
public String indexController(@PathVariable("name") String name,
@ModelAttribute Account account, @ModelAttribute("code") String code,
@ModelAttribute("test") String test, Model model) {
System.out.println("indexController start:" + account.getUserName() +
", " + account.getPassword() + ", " + code + ", " + test);
model.addAttribute("name", name);
return "index";
}
/**
* ModelAttribute和RequestMapping同时注释一个方法上
* 此时方法返回的不再是一个视图名称,而是一个Model属性的值
* 视图名称由RequestToViewNameTranslator根据请求"/test"转为为逻辑视图test
* @return
*/
@ModelAttribute("test")
@RequestMapping(value = "/test")
public String testController() {
return "gongmin test";
}
/**
* 不指定属性名称时,这个方法的返回类型来生成属性名称
* 如这个方法的返回类型为Account,则返回值将设置到
* 属性名为account的model中
* @return
*/
@ModelAttribute
public Account getAccount() {
System.out.println("getAccount start");
Account account = new Account();
account.setPassword("123");
account.setUserName("gongmin");
return account;
}
/**
* 指定属性名称时,返回的结果将设置到指定属性名的model中
* @return
*/
@ModelAttribute("code")
public String getCode() {
System.out.println("getCode start");
return "this is code";
}
/**
* 另外一种用法,传递一个Model参数,直接向参数中添加属性
* @param model
*/
@ModelAttribute
public void setModelAttribute(Model model) {
System.out.println("setModelAttribute start");
model.addAttribute("test", "hello world");
}
}
3 在src/main/resources/templates目录下新建index.html文件,内容如下:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
<p th:text="'Code, ' + ${code} + '!'" />
</body>
</html>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="${test}" />
</body>
</html>
5 测试:
5.1在浏览其中输入 http://localhost:8080/test,此时浏览器显示结果为 :
gongmin test
Hello, gongmin!
Code, this is code!
getCode start
setModelAttribute start
getAccount start
indexController start:gongmin, 123, this is code, hello world