复习第6点-6.SpringMVC作用域传值
2024-10-21 12:37:50
作用域范围
对象名称 | 作用范围 |
---|---|
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";
}
}
最新文章
- linux的基本语法及一些设置
- asp.net 验证码(一)Session
- MyBatis源码分析(2)—— Plugin原理
- sharepoint custom web service
- Unity3d 防止内存修改工具的小方法
- Apache 配置 Basic 认证
- IOS9适配 MARK
- [设计模式]<;<;设计模式之禅>;>;关于接口隔离原则
- actionInvocation
- javascript 学习总结(三)Boolean对象
- 解决浏览器不支持HTML5和CSS3
- iOS 之 二维码生成与扫描(LBXScan)
- 我来说说XML文件中的xmlns、xmlns:xsi和xsi:schemaLocation的具体含义
- 【Android Developers Training】 95. 创建一个同步适配器
- css3渐变之径向渐变
- java学习之路--面试之多线程基础
- Testing - 软件测试知识梳理 - 软件性能测试
- linux/cmd中python路径导入ModuleNotFoundError: No module named &#39;xxx&#39;
- Laravel表单传值
- python 版本升级
热门文章
- [阿里云]Datahub测试使用记录
- 算法竞赛向 C++ Standard Library 使用速查
- 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;.
- SQLSERVER 快照隔离级别 到底怎么理解?
- 动态代理-cglib分析
- 支付对接常用的加密方式介绍以及java代码实现
- ChatGPT与人工智能
- Springboot返回数据给前端-参数为null处理
- RocketMQ - 生产者消息发送流程
- clicknium-划时代的自动化