SpringMVC作用域传值几种方式
一、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作用域传值几种方式的更多相关文章
- ios 页面传值4种方式(一) 之全局变量
通用的是用代理的方式实现页面传值,但是有时候利用其它方式也可以很巧妙的解决问题,页面传值一共有4种方式: 1.使用全局变量, SharedApplication,定义一个变量来传递. 2.使用文件,或 ...
- Struts2网页面传值两种方式
第一种方式: /** 列表 */ public String list() throws Exception { List<Role> roleList = roleService.fin ...
- 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】
阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...
- springmvc参数类型转换三种方式
SpringMVC绑定参数之类型转换有三种方式: 1. 实体类中加日期格式化注解 @DateTimeFormat(pattern="yyyy-MM-dd hh:MM&quo ...
- angular中路由跳转并传值四种方式
一.路由传值 步骤1 路由传递参数 注意 一定是要传递 索引值 let key = index 这种情况是在浏览器中可以显示对应的参数 这种的是问号 localhost:8080/news?id=2& ...
- WebForm aspx页面传值---7种方式
1.get方式 发送页 <form id="form1" runat="server"> <div> <a h ...
- mvc model 传值两种方式区别
1: controller中: public actionresult index() { M m=new M(); return view(m) } view中: @model.phone vs 中 ...
- Asp.Net MVC控制器获取视图传值几种方式
前台表单(V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...
- ios 页面传值4种方式(四) 之通过delegate(代理)
这是ios里最常用的设计模式了,简直贯穿了整个cocoa touch框架.废话不多说,直接上代码: 场景是: A--打开--B; B里输入数值,点击--返回--A; A里显示B输入的值; △在开始写之 ...
随机推荐
- pta l3-20(至多删三个字符)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805046946938880 题意:给定一个长度<=106 ...
- WebSphere Application Server中manageprofiles的使用
转自 https://www.cnblogs.com/lgfeng/archive/2013/02/21/2921215.html ---------------------------------- ...
- typedef void (*Fun) (void) 的理解——函数指针——typedef函数指针
首先介绍大家比较熟悉的typedef int i;//定义一个整型变量i typedef myInt int: myInt j;//定义一个整型变量j 上面介绍得是我们常用的比较简单的typedef的 ...
- FZU-2150.FireGame.(BFS))
本题大意:给出一个n * m的地,‘#’ 代表草, ‘.’代表陆地,每次选择这片地里的两片草,可选相等的草,选择的两片草初始状态为被燃状态,每一分钟被点燃的草会将身边的四连块点.问你需要对于给定的这片 ...
- python常见的数据结构
https://www.cnblogs.com/5poi/p/7466760.html
- ubuntu下安装php
一.下载PHP7的最新版源码 php7.0.9 下载地址 http://php.net/get/php-7.0.9.tar.gz/from/a/mirror 二.解压 tar -zxf /tmp/ph ...
- Bootstrap(4) 表单和图片
1.表单 基本格式,实现基本的表单样式 <form class="form-horizontal"> <div class="form-group&qu ...
- 关于xshell连接阿里云服务器后报错的问题,git安装失败,找不到git包
1.如果安装git出现这样的错误的,在接下来键入这样一行命令 curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-c ...
- <context:annotation-config/>,<mvc:annotation-driven/>和<context:component-scan>之间的关系
首先看一下三个注解各自定义: ① <context:annotation-config/> 1.如果你想使用@Autowired注解,那么就必须事先在 spring 容器中声明 Autow ...
- netcore sqlserver linq contains生成的sql语句不是使用like而是charIndex
在ef中使用linq调用了contains,结果怎么都查不到值,打开sqlserver profiler 发现生成的sql语句不是使用like...而是CharIndex 参考文档:https://s ...