处理模型数据

ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据。

@ModelAttribute:方法入参标注了该注解后,入参对象会放到数据模型中

Map、Model:入参为org.springframework.ui.Model/ModelMap或java.util.Map时,数据会自动添加到数据模型中

ModelAndView添加数据模型方法

addObject(String attributeName,ObjectattributeValue)

addAllObjects(Map<String,?> modelMap)

@ModelAttribute

可以定义在入参中也可以定义在方法上

Spring mvc在调用目标方法前,会先逐个调用方法级别上的标注了@ModelAttribute的方法,并将方法的返回值添加到模型中

public class UserController {

@ModelAttribute(“user”)

public User getUser(){

User user = new User();

user.setId(100);

return user;

}

@RequestMapping(value= "/handle62")

public String handle62(@ModelAttribute(“user”)Useruser) {

return "user/showUser";

}

}

访问UserController中任何一个请求处理方法前,spring mvc都会先执行getUser()方法,并将返回值以user为键添加到模型中,执行handle62后,模型数据填充覆盖user对象

@SessionAttributes

如果希望多个请求之间共享某个模型数据,可以在控制器类标注@SessionAttributes,spring mvc会将模型中对应的属性暂存到httpSession中

@SessionAttributes(“user”)

public class UserController {

@ModelAttribute(“user”)

publicUser getUser(){

Useruser = new User();

User.setUserId(“1001”);

returnuser;

}

@RequestMapping(“/handle71”)

public String handle71(@ModelAttribute(“user”)User user) {

user.setName(“John”);

return “redirect:/user/handle71.html”;

}

@RequestMapping(value= "/handle72")

public String handle72(ModelMapmodelMap,SessionStatus sessionStatus) {

User user = (User)modelMap.get(“user”);

If(user!=null){

user.setName(“jetty”);

sessionStatus.setComplete();

}

return "user/showUser";

}

}

Spring mvc在调用处理方法前,在请求线程中自动一个隐含的模型对象,调用所有在方法级别标注了@ModelAttribute的方法,并将方法返回值添加到隐含模型中,查看Session中是否存在@SessionAttributes(“xxx”)所指定的xxx属性,如果有,将其添加到隐含模型中,如果隐含模型已经存在xxx属性,该步骤会覆盖隐含模型中已有的属性值。

对应标注了@ModelAttribute(“xxx”)方法的入参按如下流程

如果隐含模型包含了xxx属性,将其赋给该入参,再用请求消息填充该入参对象直接返回,否则如果xxx是会话属性,即处理类定义出标注了@SessionAttributes(“xxx”),则尝试从该会话中获取该属性,并将其值赋给入参,然后再用请求填充该入参对象,如会话中找不到xxx属性,则抛出HttpSessionRequiredException,如果隐含模型不存在xxx属性,且xxx也不是会话属性,则创建入参的对象实例,在用请求填充该入参。

@SessionAttributes 允许指定多个属性。你可以通过字符串数组的方式指定多个属性,如 @SessionAttributes({“attr1”,”attr2”})。此外,@SessionAttributes 还可以通过属性类型指定要 session 化的 ModelMap 属性,如 @SessionAttributes(types = User.class),当然也可以指定多个类,如@SessionAttributes(types = {User.class,Dept.class}),还可以联合使用属性名和属性类型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。

@SessionAttributes需要清除时,使用SessionStatus.setComplete();来清除。注意,它只清除@SessionAttributes的session,不会清除HttpSession的数据

SpringMVC Model的更多相关文章

  1. springMVC Model ModelMap 和 ModelAndView的区别(转)

    原文地址:springMVC Model ModelMap 和 ModelAndView的区别 近来在看代码,发现controller里有不同的处理返回数据的方式,而自己一直在用ModelAndVie ...

  2. SpringMVC Model,ModelMap ModelAndView

    SpringMVC 调用方法之前会创一个隐含的模型对象(即Model,ModelMap ModelAndView) //@ModelAttribute 先于login方法执行 @ModelAttrib ...

  3. SpringMVC model 多余字段 忽略

    spring-mybaits的model中如何通过注解忽略非数据库字段?——CSDN问答频道https://ask.csdn.net/questions/643534 ObjectMapper忽略多余 ...

  4. springMVC model传对象数组 jq 获取

    这个问题网上没有什么解答,有两种可能性: 一.我使用的这种方法实在太蠢了正常人都不会去这个搞: 二.我太蠢了.... 以下解决方案 //后台代码如下 public String plant(Model ...

  5. 巩固一下:SpringMVC详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  6. [转]史上最全最强SpringMVC详细示例实战教程

    原文:http://www.cnblogs.com/sunniest/p/4555801.html?utm_source=tuicool&utm_medium=referral SpringM ...

  7. 史上最全最强SpringMVC详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  8. springmvc上传图片并显示图片--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

  9. SpringMVC基础入门

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

随机推荐

  1. 【转】缺少servlet-api.jar包

    转载地址:http://blog.sina.com.cn/s/blog_6cfb18070100n7pu.html 在Eclipse中缺省servlet-api.jar包,由于servlet-apbi ...

  2. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  3. myeclipse快捷键

    转: 当时我看到struts2讲解视频的时候,讲解员居然能一下子注释掉好几行代码,而且注释的很整齐,然我大吃一惊,上网搜了下Myeclipse的快捷键还真多选择你要注释的那一行或多行代码,按Ctrl+ ...

  4. OPTM-Optimal Marks-SPOJ839最小割

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...

  5. [JAVA] 面向对象编程OOP Note

    面向对象编程OOP Note OOP五个基本特性1. 万物皆对象.2. 程序是对象的集合,它们通过发送消息来告知彼此所要做的.3. 每个对象都有自己的由其他对象所构成的存储.4. 每个对象都拥有其类型 ...

  6. intel82599在centos6.5下编译安装

    .intel驱动下载地址:https://sourceforge.net/projects/e1000/files/ixgbe%20stable/ .编译安装步骤 yum install kernel ...

  7. 【leetcode❤python】 7. Reverse Integer

    #-*- coding: UTF-8 -*-#2147483648#在32位操作系统中,由于是二进制,#其能最大存储的数据是1111111111111111111111111111111.#正因为此, ...

  8. Write a script to check an interesting game 6174

    # -*- coding: utf-8 -*-#from ftplib import FTPimport osdef sort_reverse(x,y): if x>y: return -1 i ...

  9. Java开发中经典的小实例-(冒泡法)

    public class Test25 {    public static void main(String[] args) {        // 冒泡法        int[] array = ...

  10. QAbstractItemView::setRootIndex(const QModelIndex & index) 失效

    问题: 在逻辑中使用了, QAbstractItemView::setRootIndex(const QModelIndex & index), 第一次设置生效, view 进入了model ...