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输入的值; △在开始写之 ...
随机推荐
- NumPy 算术函数
NumPy 算术函数 NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide(). 需要注意的是数组必须具有相同的形状或符合数组广播规则. ...
- Disruptor底层实现讲解与RingBuffer数据结构讲解
Disruptor术语 RingBuffer:被看作Disruptor最主要的组件,然而从2.0开始RingBuffer仅仅负责存储和更新在Disruptor中流通的数据.对一些特殊的使用场景能够被用 ...
- 32位Server2008添加IIS
- thinkphp 查找表并返回结果
使用 M('devices')->where($data)-> execute($resql); 返回的只是int值,表示成功与否,并没有返回search 到的 record . $res ...
- nginx安装扩展 sub_filter&http_ssl_module
nginx之所以轻,因为默认没有安装各种各样的扩展: nginx安装扩展插件: 下面列出两个可能会用到的插件 一.sub_filter内容过滤器,这个在nginx做http转发的时候会很常用 1.下载 ...
- Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead.解决方法
今天在eclipse里报这个错误: Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead. Please ...
- #define INVSQRT2 0.707106781 平方根倒数速算法
转自 http://www.cnblogs.com/pkuoliver/archive/2010/10/06/1844725.html 源码下载地址:http://diducoder.com/sotr ...
- netcore webapi 用户 'IIS APPPOOL\无托管代码' 登录失败
配置在iis上,除了环境配置错误的原因还有一种可能是连接字符串的问题,iis要求使用sql server的sa或者其他登录用户. ps:连接字符串: "Default": &qu ...
- C#设计模式-2工厂方法模式(Factory Method)
什么是工厂模式?类比生活中的概念,当我们需要打电话的时候,我们需要一部手机,我们通常会选择直接去卖手机的实体店买.但在程序设计中,当我们需要调用一个类(PhoneA或PhoneB)的方法的时候,我们往 ...
- Python 官方文件
7.2. 文件读写 函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode). >>> f = open('workfile', 'w') ...