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>
相关推荐
Cyclo-6 小时前
PDFJS 在React中的引入 使用组件打开文件流PDF
前端·react.js·pdf
向着光芒的女孩8 小时前
【IDEA】关不了的Proxy Authentication弹框探索过程
java·ide·intellij-idea
Filotimo_8 小时前
Spring Boot 整合 JdbcTemplate(持久层)
java·spring boot·后端
李慕婉学姐9 小时前
【开题答辩过程】以《“饭否”食材搭配指南小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring·小程序
椒盐螺丝钉9 小时前
Vue Router应用:组件跳转
前端·javascript·vue.js
顾安r9 小时前
11.20 开源APP
服务器·前端·javascript·python·css3
敲敲了个代码9 小时前
CSS 像素≠物理像素:0.5px 效果的核心密码是什么?
前端·javascript·css·学习·面试
少云清9 小时前
【软件测试】5_基础知识 _CSS
前端·css·tensorflow
倔强青铜三10 小时前
AI编程革命:React + shadcn/ui 将终结前端框架之战
前端·人工智能·ai编程
abments10 小时前
pgsql timestamp without time zone > character varying解决方案
java