[Spring MVC] - @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()。
[Spring MVC] - @ModelAttribute使用的更多相关文章
- Spring MVC @ModelAttribute详解
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...
- Spring MVC @ModelAttribute注解
在一个Controller内,被@ModelAttribute标注的方法会在此controller的每个handler方法执行前被执行. 被@ModelAttribute标注的方法的参数绑定规则和普通 ...
- Spring MVC @ModelAttribute
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...
- spring mvc @ModelAttribute 每次执行requestmapping前自动执行
在不少应用场景中,我们希望在每次执行requestmapping前自动执行一些操作,比如把某些数据(比如数据字典.系统配置.标准错误号,这在企业应用系统中极为常见)塞到model中供view访问,因为 ...
- Spring MVC @ModelAttribute 详解
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...
- Spring MVC 对于@ModelAttribute 、@SessionAttributes 的详细处理流程
初学 Spring MVC , 感觉对于 @ModelAttribute 和 @SessionAttributes 是如何被Spring MVC处理的,这一流程不是很清楚, 经过Google资料,有了 ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- spring mvc:@RequestParam与@ModelAttribute异同
关于spring mvc中的两个注解:@RequestParam.@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别. 1.@ ...
- spring mvc:注解@ModelAttribute妙用
在Spring mvc中,注解@ModelAttribute是一个非常常用的注解,其功能主要在两方面: 运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入Model ...
随机推荐
- python--安装PIL
PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux ...
- RLP编码
RLP(Recursive Length Prefix, 递归长度前缀编码),是Ethereum中对象序列化的一个主要的编码方式,其目的是对任意嵌套的二进制数据的序列进行编码. RLP的目的仅仅是编码 ...
- Java中常用的运算符
运算符是一种“功能”符号,用以通知 Java 进行相关的运算,Java 语言中常用的运算符可分为如下几种: 算数运算符.赋值运算符.比较运算符.逻辑运算符.条件运算符. 一.算数运算符 Java 中常 ...
- absolute和fixed
共同点: 改变行内元素的呈现方式,display设置为block:让元素脱离文档流,不占据空间:默认会覆盖到非定位元素上. 不同点: absolute的根元素是相对于static定位以外的第一个父元素 ...
- div被object覆盖的解决办法
代码: <div id="contextmenu" style="width: 120px; height:120px;DISPLAY: none; top: 26 ...
- Linux设备驱动中的并发控制
1.并发是指多个执行单元同时.并行的执行.并发的执行单元对共享资源的访问很容易导致竞态. 在 Linux 内核中,主要的竞态发生于如下几种情况: ①对称多处理器(SMP)的多个 CPU ②单CPU内进 ...
- 利用ClouderaManager启动HBase时,出现 master.TableNamespaceManager: Namespace table not found. Creating...
1.错误描述: 出现上述这个错误的原因是我之前已经安装了Cloudera Manager中的CDH,其中添加了所有的服务,当然也包含HBase.然后重新安装的时候,就会出现如下错误: Failed t ...
- 2014年4月份第2周51Aspx源码发布详情
HFC代码转化工具源码 2014-4-8 [VS2010]源码描述:HFC代码转化工具源码 1.主要实现HTML代码转化为C#或者JS代码,为我们平时编码节省时间. 2.把代码复制到面板上,通过右键 ...
- <Java中的继承和组合之间的联系和区别>
//Java中的继承和组合之间的联系和区别 //本例是继承 class Animal { private void beat() { System.out.println("心胀跳动...& ...
- Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分 (转载)
本文参考了谷歌开发者文档:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#provisional 如果 ...