-
session scope웹페이지 제작을 위한 스프링공부 2020. 5. 8. 13:50
java방식과 xml방식 공통된 DataBean1,2 beans
package kr.co.softcampus.beans; 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; } }
package kr.co.softcampus.beans; public class DataBean2 { private String data3; 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; } private String data4; }
java방식
controller
package kr.co.softcampus.controller; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import kr.co.softcampus.beans.DataBean1; import kr.co.softcampus.beans.DataBean2; @Controller public class TestController { //브라우저가 최초의 요청이 발생하게되면 bean객체가 각각 주입됨 -> 한번 주입된 객체를 계속사용함 @Autowired//type을 통한 주입을 받게됨 DataBean1 sessionBean1;//정의된 bean중에서 DataBean1 타입으로 정의된 bean이 있는지 확인해봄 (bean이 있는곳에서) @Resource(name = "sessionBean2")//이름을 통한 주입에는 Resource DataBean2 sessionBean2;//정의된 bean중에서 DataBean2 타입으로 정의된 bean이 있는지 확인해봄 (bean이 있는곳에서) @GetMapping("/test1") public String test1(){ sessionBean1.setData1("data1"); sessionBean1.setData2("data2"); sessionBean2.setData3("data3"); sessionBean2.setData4("data4"); return "test1"; } @GetMapping("/result1") public String result1(){ System.out.println("sessionBean1.data1:"+sessionBean1.getData1()); System.out.println("sessionBean1.data2:"+sessionBean1.getData2()); System.out.println("sessionBean2.data3:"+sessionBean2.getData3()); System.out.println("sessionBean2.data4:"+sessionBean2.getData4()); return "result1"; } }
bean정의
package kr.co.softcampus.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.annotation.SessionScope; import kr.co.softcampus.beans.DataBean1; import kr.co.softcampus.beans.DataBean2; // 프로젝트 작업시 사용할 bean을 정의하는 클래스 @Configuration public class RootAppContext { @Bean @SessionScope//bean이 주입되는시점: 브로우저의 최초의 요청이 있을때 public DataBean1 databean1() { return new DataBean1(); } @Bean("sessionBean2") @SessionScope//bean이 주입되는시점: 브로우저의 최초의 요청이 있을때 public DataBean2 databean2() { return new DataBean2(); } }
session영역에 저장되있는지 확인
<%@ 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:${sessionScope.sessionBean1.data1 }</h3> <h3>sessionBean1.data2:${sessionScope.sessionBean1.data2 }</h3> <h3>sessionBean2.data3:${sessionScope.sessionBean2.data3 }</h3> <h3>sessionBean2.data4:${sessionScope.sessionBean2.data4 }</h3> </body> </html>
결과창
값이 출력되지않는다 즉 session에 데이터가 저장되지않는다
브라우저가 최초의 요청이 발생했을때 객체가 주입되는걸로 끝난다!
값을 넘기고싶을땐 model 객체 주입후 사용
@GetMapping("/result1") public String result1(Model model){ System.out.println("sessionBean1.data1:"+sessionBean1.getData1()); System.out.println("sessionBean1.data2:"+sessionBean1.getData2()); System.out.println("sessionBean2.data3:"+sessionBean2.getData3()); System.out.println("sessionBean2.data4:"+sessionBean2.getData4()); model.addAttribute("sessionBean1", sessionBean1); model.addAttribute("sessionBean2", sessionBean2); return "result1"; }
그후 model은 request영역에 담기기때문 requestScope를 사용
<%@ 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>
XML방식
controller
package kr.co.softcampus.controller; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import kr.co.softcampus.beans.DataBean1; import kr.co.softcampus.beans.DataBean2; @Controller public class TestController { //브라우저가 최초의 요청이 발생하게되면 bean객체가 각각 주입됨 -> 한번 주입된 객체를 계속사용함 @Autowired//xml의경우 서버가 동작시 무조건 주입하려고 하기때문에 lazy어노테이션 기입 @Lazy DataBean1 sessionBean1; @Resource(name = "sessionBean2") @Lazy 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.println("sessionBean1.data1:"+sessionBean1.getData1()); System.out.println("sessionBean1.data2:"+sessionBean1.getData2()); System.out.println("sessionBean2.data3:"+sessionBean2.getData3()); System.out.println("sessionBean2.data4:"+sessionBean2.getData4()); model.addAttribute("sessionBean1", sessionBean1); model.addAttribute("sessionBean2", sessionBean2); return "result1"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class='kr.co.softcampus.beans.DataBean1' scope='session'/> <bean id='sessionBean2' class='kr.co.softcampus.beans.DataBean2' scope='session'/> </beans>
<%@ 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:${sessionScope.sessionBean1.data1 }</h3> <h3>sessionBean1.data2:${sessionScope.sessionBean1.data2 }</h3> <h3>sessionBean2.data3:${sessionScope.sessionBean2.data3 }</h3> <h3>sessionBean2.data4:${sessionScope.sessionBean2.data4 }</h3> </body> </html>
xml에서도 먼저 session영역에 저장되있는지 확인
결과를 보니 이름을 가지고 주입을 받았던 (xml에서 bean을 셋팅하고 이름을 통해 주입받은)경우 session영역에 자동으로 저장됨
주입되지않은 sessionBean1은 model객체를 주입시킨다
@GetMapping("/result1") public String result1(Model model){ System.out.println("sessionBean1.data1:"+sessionBean1.getData1()); System.out.println("sessionBean1.data2:"+sessionBean1.getData2()); System.out.println("sessionBean2.data3:"+sessionBean2.getData3()); System.out.println("sessionBean2.data4:"+sessionBean2.getData4()); model.addAttribute("sessionBean1", sessionBean1); return "result1"; }
<%@ 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:${sessionScope.sessionBean2.data3 }</h3> <h3>sessionBean2.data4:${sessionScope.sessionBean2.data4 }</h3> </body> </html>
출력창
'웹페이지 제작을 위한 스프링공부' 카테고리의 다른 글
properties (0) 2020.05.09 application scope (0) 2020.05.08 Session (0) 2020.05.07 RequestScope 빈 주입 (0) 2020.05.06 requestScope (0) 2020.05.05