在spring boot的controller层里使用session
package com.springboot06.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @author SongXianYang
* @date 2020-07-26 13:01:01
**/
@Controller
@RequestMapping("session")
public class SessionTest {
@RequestMapping("test")
public String test() {
return "index/sessionTest";
}
@PostMapping("post")
@ResponseBody
public String mySession(HttpServletRequest request, @RequestParam("username") String username) {
//首先获取session
HttpSession session = request.getSession();
//往session中存入你想要的东西
session.setAttribute("username", username);
// ("password",passWord);
// ("userId",userId);
//然后就可以通过 来获取你存的东西
String username1 = (String) session.getAttribute("username");
System.out.println(username1);
return username1;
}
}
结果输出:
user