一、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. PAT1066(AVL树)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  2. 微信小程序开发——活动规则类文案文件读取及自动转换为小程序排版代码

    前言: 最近做的小程序活动规则内容比较多,且一直处于修改中.由于小程序并不支持类似Html5中的预排版,所以,活动规则内容修改较大的时候,仍需要对新的内容用小程序的<text>组件做下排版 ...

  3. AtCoder Regular Contest 092 C - 2D Plane 2N Points(二分图匹配)

    Problem Statement On a two-dimensional plane, there are N red points and N blue points. The coordina ...

  4. Linux 线程】线程同步《四》

    1.信号量 (1)概念 信号量和互斥锁(mutex)的区别:互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区. 不多做解释,要使用信号量同步,需要包含头文件semaphore.h. ...

  5. importlib的用法

    这个模块可以通过字符串导入模块 比如我们有下面的例子 需要导入的模块的代码 在test_import目录下有一个test1的py文件 name = "中国是个大傻逼" class ...

  6. 再遇ibatisNet

    11年在Mr刘的带领下第一次接触ibatisnet ,当时Mr刘很详细的很讲了xml里的写法还有配置文件之类的,但是随着时间越来越久远.很多东西都开始淡忘了. 如今,再次和它相遇,依然觉得很亲切,虽然 ...

  7. swift - 解析三方 - ObjectMapper

    // // JYQueryBespeakModel.swift // rtb // // Created by chen on 2018/3/30 // 查询预约信息 import UIKit imp ...

  8. HttpClient--使用HttpClient进行Get Post请求访问

    在java后台开发中,我们有时候需要调用其他网站的接口进行数据的获取操作,我们一般会采用 1.java net 包中的URL 类,进行网络的数据获取 2.使用apache提供的HttpClient进行 ...

  9. 7-OKHttp使用详解,步骤挺详细的,适合初学者使用!

    OKHttp使用详解,步骤挺详细的,适合初学者使用! 一,OKHttp介绍 okhttp是一个第三方类库,用于android中请求网络. 这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Squ ...

  10. ubuntu启动进程笔记

    --防止程序随着web终端一起被关闭方法: 1.Screen -ls 查看当前进程2.Screen -S XX XX是自定义的进程名回车完 进入这个进程,在这个进程里面 可以启动 程序 3.正常返回: ...