index**页面**:
<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>Title</title></head><body><h1>首页</h1><a th:href="@{/testApplication}">取出Application域共享数据</a><br></body></html>
controller:
package com.way.controller;
import com.way.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@Controller
public class MyController2 {
@RequestMapping(value = "/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("key1","Application1");
application.setAttribute("key2","Application2");
return "target";
}
}
target页面:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>target页面</h1>
<p th:text="${application.key1}"></p>
<p th:text="${application.key2}"></p>
</body>
</html>
浏览器输出结果:
