具体的逻辑和上篇博客差不多,这里就直接贴代码了:
向session域共享数据:
@RequestMapping(value = "testSession") public String testSession(HttpSession httpSession){ httpSession.setAttribute("testSession","hello"); return "success"; }
index.html:
<a th:href="@{/testSession}">测试Session</a>
success.html
<p th:text="${session.testSession}"></p>
运行:
向application域中共享数据:
@RequestMapping(value = "testApplication") public String testApplication(HttpSession session){ ServletContext application = session.getServletContext(); application.setAttribute("testApplication","hello"); return "success"; }
index.html:
<a th:href="@{/testApplication}">测试Application</a>
success.html:
<p th:text="${application.testApplication}"></p>
运行: