
session.name은 세션 객체의 name 속성을 의미합니다.
session.name을 설정한 후 다른 페이지로 redirect를 할 때 유지되지 않는 이유는 세션 객체가 request scope에 존재하기 때문입니다.
redirect를 할 때 request scope는 새로 생성되기 때문에 이전 request scope의 세션 객체는 사라집니다.
다른 페이지에서 session.name을 읽어올 수 있는 방법은 세션 객체를 request scope에서 session scope로 옮기는 것입니다.
이 방법을 사용하려면 session scope에 세션 객체를 저장해야 합니다.
예를 들어, spring mvc에서 session scope를 사용하는 경우 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
java
@RestController
public class MyController {
@Autowired
private Session session;
@GetMapping("/test")
public String test() {
session.setAttribute("name", "test");
return "redirect:/nextPage";
}
}
#hostingforum.kr
java
@RestController
@SessionScope
public class MyController {
@GetMapping("/nextPage")
public String nextPage() {
String name = (String) session.getAttribute("name");
return "name : " + name;
}
}
위 예제에서 session scope를 사용하여 session.name을 유지할 수 있습니다.
또한, session scope를 사용하여 세션 객체를 저장할 수 있습니다.
#hostingforum.kr
java
@RestController
@SessionScope
public class MyController {
@GetMapping("/test")
public String test() {
session.setAttribute("name", "test");
return "redirect:/nextPage";
}
@GetMapping("/nextPage")
public String nextPage() {
String name = (String) session.getAttribute("name");
return "name : " + name;
}
}
위 예제에서 session scope를 사용하여 session.name을 유지할 수 있습니다.
또한, 세션 객체를 request scope에서 session scope로 옮기는 방법도 있습니다.
#hostingforum.kr
java
@RestController
public class MyController {
@Autowired
private Session session;
@GetMapping("/test")
public String test(HttpServletRequest request) {
session.setAttribute("name", "test");
request.setAttribute("session", session);
return "redirect:/nextPage";
}
}
#hostingforum.kr
java
@RestController
public class MyController {
@GetMapping("/nextPage")
public String nextPage(HttpServletRequest request) {
Session session = (Session) request.getAttribute("session");
String name = (String) session.getAttribute("name");
return "name : " + name;
}
}
위 예제에서 request scope에서 session scope로 옮기는 방법을 사용하여 session.name을 유지할 수 있습니다.
이러한 방법을 사용하여 session.name을 유지할 수 있습니다.
2025-05-03 05:53