springMVC注解用法:@modelattribute的用法
在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()。
springMVC注解用法:@modelattribute的用法的更多相关文章
- SpringMVC常用注解的规则(用法)
SpringMVC注解 @RequestMapping用法: a. 用在controller方法上: 标记url到请求方法的映射, 其实就是通过一段url地址, 找到对应需要执行的 ...
- SpringMVC +mybatis+spring 结合easyui用法及常见问题总结
SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...
- XStream中几个注解的含义和用法
转自:http://blog.csdn.net/robert_mm/article/details/8459879 XStream是个很强大的工具,能将java对象和xml之间相互转化.xstream ...
- xstream中几个注解的含义和用法(转)
XStream是个很强大的工具,能将Java对象和xml之间相互转化.xstream不在意java类中成员变量是私有还是公有,也不在乎是否有默认构造函数.它调用方式也非常简单:从xml对象转化为jav ...
- springMVC注解初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- springmvc注解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- springmvc注解@Controller和@RequestMapping
Spring从2.5版本引入注解,从而让开发者的工作变得非常的轻松 springmvc注解Controller org.springframework.stereotype.Controller注解类 ...
随机推荐
- DDR3内存技术原理
随着AMD AM2平台CPU的上市,目前两大处理器巨头均提供了对DDR2内存的支持.不过,DDR2远不是内存技术发展的终点,CPU和内存厂商已经在着手进行DDR3内存的相应准备.DDR2内存的好日子还 ...
- 服务器性能监控tips
一.tops 第一行 当前时间/已运行时间/登录用户数/最近 5 10 15分钟平均负载(平均进程数 cat /proc/loadavg) 除了前3个数字表示平均进程数量外,后面的1个分数,分母表示系 ...
- Mysql之索引的基本概念
一.索引是什么? 比如我们要在字典中找某一字,如何才能快速找到呢?那就是通过字典的目录. 对数据库来说,索引的作用就是给‘数据’加目录. 二.索引算法 设有N条随机记录,不用索引,平均查找N/2次,那 ...
- vue --- 脚手架初始化项目中配置文件webpack.base.conf.js代码含义
'use strict' //引入node path 中间件 可以获取到 path 路径的一些信息 const path = require('path') //引入utils工具模块 utils主要 ...
- Windows安全攻略:教你完全修复系统漏洞
Windows安全攻略:教你完全修复系统漏洞 首发:http://safe.it168.com/a2012/0709/1369/000001369740.shtml 目前互联网上的病毒集团越来越猖狂, ...
- Server.UrlEncode与HttpUtility.UrlEncode的区别
一.HttpUtility.UrlEncode 方法 1.public static string UrlEncode(byte[]) 将字节数组转换为已编码的 URL 字符串. 2.public s ...
- JAVA文件写入FileWriter
JAVA文件写入FileWriter 导包import java.io.FileWriter创建构造方法public FileWrite(String filename),参数是文件的路径及文件名(默 ...
- mysql not in用法
select * from zan where uid not in(select uid from zan where zhongjiang !=0) group by uid order by r ...
- spring set注入
上篇文章说到了构造器注入.可是有时候构造器注入并非非常好用,如今来看下set注入. 构造器注入博客地址:http://blog.csdn.net/luckey_zh/article/details/4 ...
- java一个月日历
项目须要,获取当天之后的30天.并提示星期几(周几),写了一个工具类 /** * 计算日期时间 * @author shijing * 2015年8月10日下午2:16:09 * @param dat ...