SpringMvc如何向context域设置数据

目录

(1)控制层方法

(2)jsp页面


context作用域表示在整个应用范围都有效。在SpringMVC中对context作用域传值,只能使用ServletContext对象来实现。但是该对象不能直接注入到方法参数中,需要通过HttpSession对象获取。

(1)控制层方法

java 复制代码
@RequestMapping("/c2/h6")
    public String h6(HttpSession session){
        ServletContext servletContext=session.getServletContext();
        servletContext.setAttribute("age",999);
        return "test";
    }

(2)jsp页面

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>request=${requestScope.name}</h1>
<h2>session=${sessionScope.info}</h2>
<h3>context=${applicationScope.age}</h3>
</body>
</html>
相关推荐
Kuo-Teng2 分钟前
LeetCode 19: Remove Nth Node From End of List
java·数据结构·算法·leetcode·链表·职场和发展·list
P***25393 分钟前
前端构建工具缓存清理,npm cache与yarn cache
前端·缓存·npm
北i4 分钟前
TiDB 关联子查询去关联优化实战案例与原理深度解析
java·数据库·sql·tidb
Kuo-Teng5 分钟前
LeetCode 21: Merge Two Sorted Lists
java·算法·leetcode·链表·职场和发展
好奇的菜鸟5 分钟前
解决 npm 依赖版本冲突:从 “unable to resolve dependency tree“ 到依赖管理高手
前端·npm·node.js
我命由我1234511 分钟前
Java 开发 - 粘包处理器 - 基于消息头 + 消息体(魔数验证、长度验证)
java·网络·后端·网络协议·java-ee·intellij-idea·intellij idea
lcc18711 分钟前
Vue 内置指令
前端·vue.js
2301_8003997217 分钟前
stm32 printf重定向到USART
java·stm32·算法
bagadesu21 分钟前
15.<Spring Boot 日志>
java·后端
laplace012330 分钟前
Maven
java·maven