在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里。

如果把@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法

比如我们有一个Controller:TestController

@Controller
@RequestMapping(value="test")
public class PassportController { @ModelAttribute
public void preRun() {
System.out.println("Test Pre-Run");
} @RequestMapping(method=RequestMethod.GET)
public String index() {
return "login/index";
} @RequestMapping(value="login", method=RequestMethod.POST)
public ModelAndView login(@ModelAttribute @Valid Account account, BindingResult result)
:
:
} @RequestMapping(value="logout", method=RequestMethod.GET)
public String logout() {
:
:
} }

在调用所有方法之前,都会先执行preRun()方法。

我们可以把这个@ModelAttribute特性,应用在BaseController当中,所有的Controller继承BaseController,即可实现在调用Controller时,先执行@ModelAttribute方法。

比如权限的验证(也可以使用Interceptor)等

下面是一个设置request和response的方式(这个未测试,不知有没线和安全问题)

package com.my.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.ModelAttribute; public class BaseController { protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session; @ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
this.request = request;
this.response = response;
this.session = request.getSession();
} }

@ModelAttribute也可以做为Model输出到View时使用,比如:

测试例子

package com.my.controller;

import java.util.ArrayList;
import java.util.List; 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.RequestMethod; import com.my.controller.bean.Account; @Controller
@RequestMapping(value="attr")
public class TestModelAttributeController { private static List<Account> accounts = new ArrayList<Account>();
{
accounts.add(new Account());
accounts.add(new Account()); Account ac1 = accounts.get(0);
Account ac2 = accounts.get(1); ac1.setUserName("Robin");
ac1.setPassword("123123"); ac2.setUserName("Lucy");
ac2.setPassword("123456");
} @RequestMapping(method=RequestMethod.GET)
public String index() {
System.out.println("index");
return "TestModelAttribute/index";
} @ModelAttribute("accounts")
public List<Account> getAccounts() {
System.out.println("getAccounts");
return accounts;
} }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="st" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestModelAttribute</title>
</head>
<body>
<c:forEach items="${accounts}" var="item">
<c:out value="${item.userName}"></c:out><br/>
</c:forEach>
</body>
</html>

页面将输出:

在Console中输出为:

这里可以看到,运行的先后次序为:先调用getAccounts(),再调用index()。

最新文章

  1. Java开发代码性能优化总结
  2. R与Java
  3. spring data实现自定义的repository实现类,实现跟jpa联通
  4. 部署git服务器 gitServer 软件
  5. Asp.Net复习篇之Asp.Net生命周期与Asp.Net页的生命周期
  6. Custom Control
  7. IIS配置不正确可能导致“远程服务器返回错误: (404) 未找到&quot;错误一例。
  8. 马蜂窝搜索基于 Golang 并发代理的一次架构升级
  9. 1.6 Why only in China?
  10. sublime和vscode 格式化Json ——两步走
  11. Spark Streaming和Kafka整合保证数据零丢失
  12. Mooc总结——Linux内核分析
  13. .Net Core DES加密解密
  14. Cloud Foundry v2 部署及入门运维
  15. Javaweb学习(三):Servlet程序
  16. MySQL存储引擎对比
  17. 【转】ElasticSearch之定时删除数据
  18. 题目1001:A+B for Matrices
  19. 【刷题】洛谷 P3901 数列找不同
  20. [poj] 2549 Sumsets || 双向bfs

热门文章

  1. angular router-ui
  2. Yii 检查输入的数据是否已经存在
  3. python中列表的常用方法
  4. iPhone/iPad/Android UI尺寸规范
  5. Java成员初始化顺序
  6. css布局之一列布局
  7. iOS中FMDB的使用
  8. 拿到新机器,进行初始化和部署Nginx的过程
  9. 《Java中的不可变类》
  10. android技巧(四)数据库跨版本升级写法