스프링 서비스 세션 - seupeuling seobiseu sesyeon

1. 세션에 Data 저장

session.setAttribute("저장하고자 하는 변수명", 저장변수값);

<Controller>

@RequestMapping(value = "/test.do") public String test(HttpServletRequest request) throws Exception { HttpSession session = request.getSession(); String name = "홍길동"; session.setAttribute("sessionId", name); return "test/test"; }

<View   ex.Thymeleaf>

<body> <h2 th:text="${sessionId}"></h2> <!-- 출력값: 홍길동 --> </body>

2. 세션에 저장된 Data 가져오기

session.getAttribute("저장한 변수명");

<Controller>

@RequestMapping(value = "/test2.do") public String test2(HttpServletRequest request) throws Exception { HttpSession session = request.getSession(); String name = (String) session.getAttribute("sessionId"); System.out.println("=============================="); System.out.println("세션에 저장 되 있는 변수 : "+name); // 홍길동 출력 System.out.println("=============================="); name = "유재석"; session.setAttribute("sessionId", name); return "test/test"; }

<View  ex.Thymeleaf>

<body> <h2 th:text="${sessionId}"></h2> <!-- 출력값: 유재석 --> </body>

3. 세션 초기화 하기

session.invalidate();

1. 문제상황

컨트롤러에서 Session객체를 가져오는 것은 쉽다. 하지만 SERVICE / DAO / UTIL 등 기타 클래스에서 Session을 가져오는 것은 어떤가? 이러한 문제 상황이 닥쳤을때의 해결방법을 알아보자.

2. 해결방법

간단하다 아래의 클래스를 만들고 사용하면 된다.

package com.sample.vue.common.utils; import javax.servlet.http.HttpSession; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.sample.vue.user.model.UserEntity; public class AuthUtil { public static boolean isAdmin() { ServletRequestAttributes servletRequestAttribute = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpSession httpSession = servletRequestAttribute.getRequest().getSession(true); if( ((UserEntity)httpSession.getAttribute("userEntity")).getType().equals("01")) { return true; }else { return false; } } public static UserEntity getCurrentUserEntitiy() { ServletRequestAttributes servletRequestAttribute = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpSession httpSession = servletRequestAttribute.getRequest().getSession(true); return (UserEntity)httpSession.getAttribute("userEntity"); } public static String getCurrentUserAccount() { ServletRequestAttributes servletRequestAttribute = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpSession httpSession = servletRequestAttribute.getRequest().getSession(true); return( (UserEntity)httpSession.getAttribute("userEntity")).getAccount(); } }

아래 처럼 사용하면 된다.

param.put("account", AuthUtil.getCurrentUserAccount());

스프링에서 세션 사용하기

1. 세션에 Data 저장하기

session.setAttribute("저장 하고자 하는 변수이름", 저장변수값);

Java Controller

1

2

3

4

5

6

7

8

9

10

11

12

@RequestMapping(value = "/test.do")

public ModelAndView test(HttpServletRequest request) throws Exception {

HttpSession session = request.getSession();

String name = "세션저장하기";

session.setAttribute("ssVar", name);

ModelAndView mv = new ModelAndView();

mv.setViewName("/test/test");

return mv;

}

JSP

<body>

<input type="text" value="${ssVar }"/>

</body>

2. 세션에 저장된 Data 가져오기

session.getAttribute("저장한 변수 이름");

Java Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@RequestMapping(value = "/test2.do")

public ModelAndView test2(HttpServletRequest request) throws Exception {

ModelAndView mv = new ModelAndView();

HttpSession session = request.getSession();

String name = (String) session.getAttribute("ssVar");

System.out.println("==============================");

System.out.println("세션에 저장 되 있는 변수 : "+name);

System.out.println("==============================");

name = "세션값 변경";

session.setAttribute("ssVar", name);

mv.setViewName("/test/test");

return mv;

}

Java Console 확인

JSP

<body>

<input type="text" value="${ssVar }"/>

</body>

 3. 세션 초기화 하기

session.invalidate();

Toplist

최신 우편물

태그