[!NOTE]
本教程是《SpringBoot系列基础教程》之一,教程目录:https://laisc7301.github.io/blog/2024/01/29/202401290001SpringBoot%E7%B3%BB%E5%88%97%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/
首先按照图示新建项目:
添加以下依赖:
找到application.properties文件,并写入下面内容:
整个项目的文件结构如下图所示:
贴出代码:
TestController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package org.laisc.example7.controller;
import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;
@Controller @RequestMapping("/test") public class TestController { @ResponseBody @RequestMapping("/set") public String set(HttpSession session) { session.setAttribute("name", "laisc"); return "成功设置!"; }
@ResponseBody @RequestMapping("/get") public String get(HttpSession session) { String name = (String) session.getAttribute("name"); return name; }
@ResponseBody @RequestMapping("/remove") public String remove(HttpSession session) { session.removeAttribute("name"); return "成功删除!"; }
@ResponseBody @RequestMapping("/reset") public String reset(HttpSession session) { session.invalidate(); return "已重置!"; } }
|
项目源代码下载:https://pan.baidu.com/s/1-Cl0aJVosdjTYPwUYfHR6Q?pwd=wpf4