作用域范围

对象名称 作用范围
application 整个作用范围
session 在当前会话中有效
request 在当前请求中有效
page 在当前页面有效

request/session/application作用域传值

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map; @Controller
public class HttpRequestController {
@RequestMapping("/login")
public String showLogin(HttpServletRequest request) {
request.setAttribute("msg", "hello showlogin");
return "index.jsp";
} /*
SpringMVC 会为 Map 接口注入 BindingAwareModelMap 对象。
该对象是由 Spring 提供的一个实现了 Map 接口的对象。
SpringMVC会把该对象中的数据放入到 HttpServletRequest 对象中,
其目的是为了解除控制器与容器的耦合。
*/
@RequestMapping("/loginMap")
public String showLogin2(HttpServletRequest request, Map<String, String> map) {
map.put("msg", "hello showLogin2");
return "index.jsp";
} /*
在 SpringMVC 中提供了一个 Model 类型的接口,该接口定义了传递数据的基本行为。
如果在处理请求的方法中指定了Model类型的参数,
那么SpringMVC注入一个BindingAwareModelMap对象,
并通过该对象把数据放入到 HttpServletRequest 对象中。
*/
@RequestMapping("/loginModel")
public String showLogin3(HttpServletRequest request, Model model) {
model.addAttribute("msg", "loginModel");
return "index.jsp";
} @RequestMapping("/showsession")
public String showLogin4(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("msg", "hello session1");
return "index.jsp";
} @RequestMapping("/showsession2")
public String showLogin5(HttpSession session) {
session.setAttribute("msg", "hello session2");
return "index.jsp";
} @RequestMapping("/application")
public String showLogin6(HttpSession session) {
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("msg", "hello servletContext");
return "index.jsp";
}
}

最新文章

  1. linux的基本语法及一些设置
  2. asp.net 验证码(一)Session
  3. MyBatis源码分析(2)—— Plugin原理
  4. sharepoint custom web service
  5. Unity3d 防止内存修改工具的小方法
  6. Apache 配置 Basic 认证
  7. IOS9适配 MARK
  8. [设计模式]&lt;&lt;设计模式之禅&gt;&gt;关于接口隔离原则
  9. actionInvocation
  10. javascript 学习总结(三)Boolean对象
  11. 解决浏览器不支持HTML5和CSS3
  12. iOS 之 二维码生成与扫描(LBXScan)
  13. 我来说说XML文件中的xmlns、xmlns:xsi和xsi:schemaLocation的具体含义
  14. 【Android Developers Training】 95. 创建一个同步适配器
  15. css3渐变之径向渐变
  16. java学习之路--面试之多线程基础
  17. Testing - 软件测试知识梳理 - 软件性能测试
  18. linux/cmd中python路径导入ModuleNotFoundError: No module named &#39;xxx&#39;
  19. Laravel表单传值
  20. python 版本升级

热门文章

  1. [阿里云]Datahub测试使用记录
  2. 算法竞赛向 C++ Standard Library 使用速查
  3. flutter报错The type of the function literal can&#39;t be inferred because the literal has a block as its body.A value of type &#39;String?&#39; can&#39;t be assigned to a variable of type &#39;String&#39;.
  4. SQLSERVER 快照隔离级别 到底怎么理解?
  5. 动态代理-cglib分析
  6. 支付对接常用的加密方式介绍以及java代码实现
  7. ChatGPT与人工智能
  8. Springboot返回数据给前端-参数为null处理
  9. RocketMQ - 生产者消息发送流程
  10. clicknium-划时代的自动化