SpringMVC笔记7---@ModelAttribute的使用

@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> 


4 在src/main/resources/templates目录下新建test.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


  5.2 在浏览器中输入 http://localhost:8080/index/gongmin,此时浏览器显示结果为:

Hello, gongmin!

Code, this is code!


  同时Eclipse的console窗口显示:

getCode start
setModelAttribute start
getAccount start
indexController start:gongmin, 123, this is code, hello world





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值