一、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. codeforces 722D Generating Sets 【优先队列】

    You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...

  2. Python compile() 函数

    Python compile() 函数  Python 内置函数 描述 compile() 函数将一个字符串编译为字节代码. 语法 以下是 compile() 方法的语法: compile(sourc ...

  3. MongoDB修改默认数据库

    在某些情况下,我们并不想把mongoDB的数据库放在c盘,这时候有两种方法可以切换数据库目录. 1.命令方式 首先创建数据路目录,例如:E:\data\db.然后运行命令 mongod -dbpath ...

  4. TOJ 5225: 玩转二叉树

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=5225 时间限制(普通/Java): ...

  5. 游戏行业的女性拥有强大的新盟友:Facebook

    据外媒 TheNextWeb 报道,Facebook 本周宣布其新的游戏行业女性倡议,致力于鼓励更多的女性加入游戏行业.这家社交媒体公司专注于提供榜样和成功故事,而这实际上是一种令人愉快的方式.虽然游 ...

  6. Balanced Numbers (数位DP)

    Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...

  7. 微信小程序开发——使用回调函数出现异常:TypeError: Cannot read property 'setData' of undefined

    关键技术点: 作用域问题——回调函数中的作用域已经脱离了调用函数了,因此需要在回调函数外边把this赋给一个新的变量才可以了. 业务需求: 微信小程序开发,业务逻辑需要,需要把获取手机号码的业务逻辑作 ...

  8. elastic5.4安装错误解决

    首先,我们从官网下载:(官网:https://www.elastic.co/downloads/elasticsearch)(推荐下载deb或者rpm包,否则坑很多) 启动 (需要依赖java环境) ...

  9. selenium无界面chromedriver

    chromeDriver下载地址:http://chromedriver.storage.googleapis.com/index.html 谷歌浏览器Chrome和驱动程序的对照表https://b ...

  10. 关于sortedlist 中值的添加,删除,索引测试.

    SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问. 排序列表是数组和哈希表的组合.它包含一个可使用键或索引访问各项的列表.如果您使用索引访问各项,则它是一个 ...