Spring MVC提供了以下几种途径输出模型数据:

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

2)Map及Model:处理方法入参为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.util.Map时,处理方法返回时,Map中的数据会自动被添加到模型中;

3)@SessionAttributes:将模型中的某个属性暂存到HttpSeession中,以便多个请求之间可以共享这个属性;

4)@ModelAttribute:方法入参标注该注解后,入参的对象就会放到数据模型中。

Map及Model

使用示例:

在TestModelData.java类中追加方法:

  1. @RequestMapping("/testMap")
  2. public String testMap(Map<String, Object> map) {
  3. map.put("mapTestKey", "mapTestValue");
  4. return SUCCESS;
  5. }

修改/WEB-INF/views/success.jsp页面,中

测试地址:http://localhost:8080/SpringMVC_02/testMap

返回页面打印信息:

SUCCESS PAGE 
current time: 
testMap mapTestKey:mapTestValue

查看此时入参Map实际什么类型,修改TestModelData.java代码:

  1. @RequestMapping("/testMap")
  2. public String testMap(Map<String, Object> map) {
  3. map.put("mapTestKey", "mapTestValue");
  4. System.out.println(map);
  5. System.out.println(map.getClass());
  6. return SUCCESS;
  7. }

打印结果:

  1. {mapTestKey=mapTestValue}
  2. class org.springframework.validation.support.BindingAwareModelMap

从打印结果发现,实际上入参Map是BildingAwareModelMap

  1. public class BindingAwareModelMap extends ExtendedModelMap
  1. public class ExtendedModelMap extends ModelMap implements Model

由于BildingAwareModelMap继承了ExtendedModelMap,而ExtendedModelMap又继承了ModeMap和实现了Model接口,因此,这里Map入参可以替换为ModelMap和Model。

调试:设置断点在“   map.put("mapTestKey", "mapTestValue");”行处

从调试跟踪发现,入参map最终被解析的类是MapMethodProcessor.java

MapMethodProcessor.java

  1. /*
  2. * Copyright 2002-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package org.springframework.web.method.annotation;
  18.  
  19. import java.util.Map;
  20.  
  21. import org.springframework.core.MethodParameter;
  22. import org.springframework.lang.Nullable;
  23. import org.springframework.util.Assert;
  24. import org.springframework.web.bind.support.WebDataBinderFactory;
  25. import org.springframework.web.context.request.NativeWebRequest;
  26. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  27. import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
  28. import org.springframework.web.method.support.ModelAndViewContainer;
  29.  
  30. /**
  31. * Resolves {@link Map} method arguments and handles {@link Map} return values.
  32. *
  33. * <p>A Map return value can be interpreted in more than one ways depending
  34. * on the presence of annotations like {@code @ModelAttribute} or
  35. * {@code @ResponseBody}. Therefore this handler should be configured after
  36. * the handlers that support these annotations.
  37. *
  38. * @author Rossen Stoyanchev
  39. * @since 3.1
  40. */
  41. public class MapMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
  42.  
  43. @Override
  44. public boolean supportsParameter(MethodParameter parameter) {
  45. return Map.class.isAssignableFrom(parameter.getParameterType());
  46. }
  47.  
  48. @Override
  49. @Nullable
  50. public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
  51. NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
  52.  
  53. Assert.state(mavContainer != null, "ModelAndViewContainer is required for model exposure");
  54. return mavContainer.getModel();
  55. }
  56.  
  57. @Override
  58. public boolean supportsReturnType(MethodParameter returnType) {
  59. return Map.class.isAssignableFrom(returnType.getParameterType());
  60. }
  61.  
  62. @Override
  63. @SuppressWarnings({ "unchecked", "rawtypes" })
  64. public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
  65. ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
  66.  
  67. if (returnValue instanceof Map){
  68. mavContainer.addAllAttributes((Map) returnValue);
  69. }
  70. else if (returnValue != null) {
  71. // should not happen
  72. throw new UnsupportedOperationException("Unexpected return type: " +
  73. returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
  74. }
  75. }
  76.  
  77. }

当页面返回时,该入参会被加载到Request请求域。

SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model的更多相关文章

  1. SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  2. SpringMVC(九):SpringMVC 处理输出模型数据之ModelAndView

    Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...

  3. SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  4. SpringMVC:学习笔记(4)——处理模型数据

    SpringMVC—处理模型数据 说明 SpringMVC 提供了以下几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添 ...

  5. SpringMVC 学习笔记(四) 处理模型数据

    Spring MVC 提供了下面几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体就可以通过该对象加入模型数据 – Map及Model: ...

  6. SpringMvc:处理模型数据

    SpringMvc提供了以下途径输出模型数据: -ModelAndView:处理方法返回值类型为ModelAndView,方法体即可通过该对象添加模型数据 -Map或Model:入参为org.spri ...

  7. 【SpringMVC】SpringMVC系列9之Model数据返回到View

    9.Model数据返回到View 9.1.概述     Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体 ...

  8. Spring MVC—模型数据,转发重定向,静态资源处理方式

    Spring MVC处理模型数据 添加模型数据的方法 ModelAndView Map及Model SessionAttribute ModelAttribute Spring MVC转发和重定向 S ...

  9. springmvc学习(五)——处理模型数据

    Spring MVC 提供了以下几种途径输出模型数据: ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据Map 及 Model: 入参 ...

随机推荐

  1. python web开发-flask中消息闪现flash的应用

    Flash中的消息闪现,在官方的解释是用来给用户做出反馈.不过实际上这个功能只是一个记录消息的方法,在某一个请求中记录消息,在下一个请求中获取消息,然后做相应的处理,也就是说flask只存在于两个相邻 ...

  2. 【漏洞】PHPCMS_V9.6.0 前台注册GETSHELL

    首先准备一台公网服务器,在上面新建一个一句话的txt文件.如下: 接着打开目标网站,点击注册,填写信息后点击提交,拦截该数据包. 将其中post提交的数据替换成我们的poc,poc如下: siteid ...

  3. 玩转log4j

    我的目标是授人以渔,而不是授人以鱼,我相信你仔细看完这篇文章,玩转log4j不成问题 先来一个log4j最简单的例子 public class MyApp { static Logger logger ...

  4. C语言操作符/表达式及其作用总结

    一.算术操作符:+ - * / % 1. 除了 %操作符之外,其他的 几个操作符可以作 用于整数和浮点数. 2. 对于"/"操作符如果两个操作数都为整数,执行整数除法.而只要有浮点 ...

  5. 基于node写了个工具,可以在线制作“sorry,为所欲为”的 GIF(开源)

    SnailDev.GifMaker 一个生成gif并添加自定义字幕的工具 client 微信小程序 server nodejs + express 欢迎 star&fork 如果您有好的com ...

  6. 去除input的自动填充色

    input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #ffffff inset !important; }

  7. div内文字显示两行,多出的文字用省略号显示

    用-webkit-私有属性,代码如下:text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;disp ...

  8. RPC原理解析

    1.RPC原理解析 1.1 什么是RPC RPC(Remote Procedure Call Protocol) --远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络 ...

  9. 听翁恺老师mooc笔记(14)--格式化的输入与输出

    关于C语言如何做文件和底层操作: 文件操作,从根本上说,和C语言无关.这部分的内容,是教你如何使用C语言的标准库所提供的一系列函数来操作文件,最基本的最原始的文件操作.你需要理解,我们在这部分所学习的 ...

  10. RxSwift(一)

    文/iOS_Deve(简书作者) 原文链接:http://www.jianshu.com/p/429b5160611f 著作权归作者所有,转载请联系作者获得授权,并标注"简书作者" ...