SessionScope
- Bean을 정의할 때 session scope로 정의하면 브라우저가 서버에 최초의 요청을 보낼 때 Bean객체가 주입
- 주입된 Bean은 주입만 이루어지는 것이므로 session영역에 저장되지는 않음
1) index.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href='test1'>test1</a><br/>
<a href='result1'>result1</a><br/>
</body>
</html>
2) TestController 생성
@Controller
public class TestController {
@GetMapping("/test1")
public String test1() {
return "test1";
}
@GetMapping("/result1")
public String result1(Model model) {
return "result1";
}
}
3) test1.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>test1</h1>
</body>
</html>
3) result1.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>result1</h1>
</body>
</html>
4) DataBean1,2 생성
public class DataBean1 {
private String data1;
private String data2;
public String getData1() {
return data1;
}
public void setData1(String data1) {
this.data1 = data1;
}
public String getData2() {
return data2;
}
public void setData2(String data2) {
this.data2 = data2;
}
}
public class DataBean2 {
private String data3;
private String data4;
public String getData3() {
return data3;
}
public void setData3(String data3) {
this.data3 = data3;
}
public String getData4() {
return data4;
}
public void setData4(String data4) {
this.data4 = data4;
}
}
5) Bean을 RootAppContext.java에 등록
// 프로젝트 작업시 사용할 bean을 정의하는 클래스
@Configuration
public class RootAppContext {
@Bean
@SessionScope
public DataBean1 dataBean1() {
return new DataBean1();
}
@Bean("sessionBean2")
//이름을 통해 주입
@SessionScope
public DataBean2 dataBean2() {
return new DataBean2();
}
}
6) TestController에서 Bean 주입받기
@Controller
public class TestController {
@Autowired
DataBean1 sessionBean1;
@Resource(name = "sessionBean2")
DataBean2 sessionBean2;
@GetMapping("/test1")
public String test1() {
sessionBean1.setData1("data1");
sessionBean1.setData2("data2");
sessionBean2.setData3("data3");
sessionBean2.setData4("data4");
return "test1";
}
@GetMapping("/result1")
public String result1(Model model) {
System.out.printf("sessionBean1.data1 : %s\n", sessionBean1.getData1());
System.out.printf("sessionBean1.data2 : %s\n", sessionBean1.getData2());
System.out.printf("sessionBean2.data3 : %s\n", sessionBean2.getData3());
System.out.printf("sessionBean2.data4 : %s\n", sessionBean2.getData4());
model.addAttribute("sessionBean1", sessionBean1);
model.addAttribute("sessionBean2", sessionBean2);
return "result1";
}
}
-Bean을 @SessionScope로 정의했기 때문에 브라우저가 최초의 요청을 할 때 주입된다.(한 번 주입된 객체를 계속 사용)
-requestScope와 마찬가지로 request에 저장하려면 Model을 주입받아 담아준다.
7) request에 저장됐는지 확인하기 위해 result1.jsp 변경
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>result1</h1>
<h3>sessionBean1.data1 : ${requestScope.sessionBean1.data1 }</h3>
<h3>sessionBean1.data2 : ${requestScope.sessionBean1.data2 }</h3>
<h3>sessionBean2.data3 : ${requestScope.sessionBean2.data3 }</h3>
<h3>sessionBean2.data4 : ${requestScope.sessionBean2.data4 }</h3>
</body>
</html>
'SPRING' 카테고리의 다른 글
[SPRING] SessionScope (0) | 2021.07.27 |
---|---|
[SPRING] 스프링 component-scan 개념 및 동작 과정 (0) | 2021.07.27 |
[SPRING] RequestScope Bean 주입 (0) | 2021.07.26 |
[SPRING] Request Scope (0) | 2021.07.25 |
[SPRING] Redirect와 Forward (0) | 2021.07.24 |
댓글