一、SpringMVC 作用域传值的几种方式

  1  使用原生Servlet

    1.1 在 HandlerMethod 参数中添加作用域对象

        1.1.1 ServletContext不能在方法参数中获取,只能在request中获取

@Controller
public class DemoController {
@RequestMapping("demo")
public String demo(HttpServletResponse resp,HttpServletRequest req,HttpSession session){
//Request
req.setAttribute("request", "这是HttpServletRequest");
//Session
session.setAttribute("session","这是HttpSession");
HttpSession session2=req.getSession();
session2.setAttribute("session2","这是HttpSession22");
//application
ServletContext context = req.getServletContext();
context.setAttribute("servletcontext","这是ServletContext");
return "/index.jsp";
}
}

  2  使用Map集合

    2.1 把map中内容放在 request 作用域中

    2.2 spring 会对map集合通过  spring中的 class org.springframework.validation.support.BindingAwareModelMap 进行实例化

        @RequestMapping("demo2")
public String demo2(Map<String,Object> map){
System.out.println(map.getClass());
map.put("map","天使");
return "/index.jsp";
}   index.jsp中
    map:::${requestScope.map} 进行获取    (可以不使用requestScope)

  3 使用SpringMVC 中的Model接口

    3.1 本质还是 request作用域进行传值,(Model存在的意义就是替换request)

    3.2 把内容最终放入到request作用域中 

        @RequestMapping("demo3")
public String demo3(Model model){
model.addAttribute("model","这是Model的值");
return "/index.jsp";
}   index.jsp 中
  model:::${requestScope.model} 获取

  4 使用SpringMVC中ModelAndView 类

        @RequestMapping("demo4")
public ModelAndView demo4(){
ModelAndView mac=new ModelAndView("/index.jsp");
mac.addObject("mav","天使的ModelAnd view");
return mac;
}
index.jsp中
modelandview::${requestScope.mav}

SpringMVC作用域传值几种方式的更多相关文章

  1. ios 页面传值4种方式(一) 之全局变量

    通用的是用代理的方式实现页面传值,但是有时候利用其它方式也可以很巧妙的解决问题,页面传值一共有4种方式: 1.使用全局变量, SharedApplication,定义一个变量来传递. 2.使用文件,或 ...

  2. Struts2网页面传值两种方式

    第一种方式: /** 列表 */ public String list() throws Exception { List<Role> roleList = roleService.fin ...

  3. 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】

    阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...

  4. springmvc参数类型转换三种方式

    SpringMVC绑定参数之类型转换有三种方式:     1. 实体类中加日期格式化注解      @DateTimeFormat(pattern="yyyy-MM-dd hh:MM&quo ...

  5. angular中路由跳转并传值四种方式

    一.路由传值 步骤1 路由传递参数 注意 一定是要传递 索引值 let key = index 这种情况是在浏览器中可以显示对应的参数 这种的是问号 localhost:8080/news?id=2& ...

  6. WebForm aspx页面传值---7种方式

    1.get方式 发送页 <form id="form1" runat="server">    <div>        <a h ...

  7. mvc model 传值两种方式区别

    1: controller中: public actionresult index() { M m=new M(); return view(m) } view中: @model.phone vs 中 ...

  8. Asp.Net MVC控制器获取视图传值几种方式

    前台表单(V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...

  9. ios 页面传值4种方式(四) 之通过delegate(代理)

    这是ios里最常用的设计模式了,简直贯穿了整个cocoa touch框架.废话不多说,直接上代码: 场景是: A--打开--B; B里输入数值,点击--返回--A; A里显示B输入的值; △在开始写之 ...

随机推荐

  1. poj 2553 缩点+染色+出度

    题目链接:https://vjudge.net/problem/POJ-2553 如果不会tarjan算法,推荐博客:https://blog.csdn.net/mengxiang000000/art ...

  2. ES6之扩展运算符

    一 将数组转换为用逗号分隔的参数序列 let turtles = ['Leo','Raph','Mikey','Don']; console.log(...turtles); 二 在解构赋值时使用 l ...

  3. 【python】入门指南:控制语句

    条件控制 if,if-else,if-elseif-else #!/bin/python a = 'test' if a == 'test': print('a is %s' %(a)) else: ...

  4. 宝塔Linux面板 概述

    安装要求: Python版本: 2.6/2.7(安装宝塔时会自动安装) 内存:128M以上,推荐512M以上(纯面板约占系统10M内存) 硬盘:100M以上可用硬盘空间(纯面板约占20M磁盘空间) 系 ...

  5. ScrollView嵌套ListView,禁止ListView的滚动,只让ScrollView可以滚动

    自定义ListView,xml布局文件中使用该自定义的ListView public class NoScrollListview extends ListView{ public NoScrollL ...

  6. 29-jsp中用js进行时间格式转化

    CST可以为如下4个不同的时区的缩写: 美国中部时间:Central Standard Time (USA) UT-6:00 澳大利亚中部时间:Central Standard Time (Austr ...

  7. Android沉浸式状态栏背景色以及字体颜色的修改

    在activity中设置透明状态栏 的思路:    1.让activity的布局全屏 此时布局会和状态栏重叠        2.让布局最上方预留出和状态栏高度一样的高度,将状态栏的背景色设置为透明 效 ...

  8. @__CheckForDebuggerJustMyCode@4

    最近在编译一个项目MiniFSWatcher,其中有一部分程序需要安装驱动minispy,编译过程中报错 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2019 无法解析的外部符号 @ ...

  9. Java并发-ConcurrentModificationException原因源码分析与解决办法

    一.异常原因与异常源码分析 对集合(List.Set.Map)迭代时对其进行修改就会出现java.util.ConcurrentModificationException异常.这里以ArrayList ...

  10. nvl 与 nvl2

    NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...