8> Model Scope
(1) session scope
- @SessionAttribute(names={"key값", "key값"})
Model 을 저장할 때 위에서 설정한 key 값을 사용하면
session scope 에 Model 이 저장된다.
- 세션을 사용하고자하는 Controller 마다 매번 @SessionAttribute 을
설정해야 된다.
package com.exam;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.exam.controller;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes(names = {"session", "xxx"}) // 만들때도 사용
public class MainController {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
ServletContext application;
// Model 저장
@GetMapping("/set-model")
public String set_model(ModelMap m) {
// request scope 에 저장
m.addAttribute("request", "홍길동");
// session scope 에 저장
m.addAttribute("session", "홍길동2");
m.addAttribute("xxx", "홍길동3");
// application scope 에 저장
application.setAttribute("application", "홍길동4");
return "hello";
}
}
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
request: ${request}<br>
session: ${sessionScope.session}<br>
xxx: ${sessionScope.xxx}<br>
application: ${applicationScope.application}<br>
</body>
</html>
# application.properties
logging.level.org.springframework=info
# tomcat port 번호 변경
server.port=8090
# context 명 변경
server.servlet.context-path=/app
# jsp 의 경로와 확장자 지정
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exam</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2) application scope
- @Autowired
ServletContext ctx;
ctx.setAttribute(key, value);
# JSP
${requestScope.key}
${sessiontScope.key}
${applicationScope.key}
${key} <== request 에서 먼저찾고 없으면 session 찾고 또 없으면 application 에서 찾음.
package com.exam.controller;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes(names = {"session", "xxx"}) // 사용할때도 사용
public class MainController2 {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
ServletContext application;
// MainController 에서 저장된 Model 사용
@GetMapping("/get-model")
public String get_model(ModelMap m) {
// request scope 에서 가져옴
String request = (String)m.getAttribute("request");
logger.info("logger: request:{}", request);
// session scope 조회
String session = (String)m.getAttribute("session");
String xxx = (String)m.getAttribute("xxx");
logger.info("logger: session:{}", session);
logger.info("logger: xxx:{}", xxx);
// application scope 조회
String application2 = (String)application.getAttribute("application");
logger.info("logger: application:{}", application2);
return "hello";
}
}
'[study]이론정리 > Spring Boot' 카테고리의 다른 글
Spring MVC - redirect 와 forward & redirect-flashScope-forward (0) | 2024.07.02 |
---|---|
Spring MVC - Ajax + json 통신 (0) | 2024.07.02 |
Spring MVC - Model 생성 (0) | 2024.07.02 |
Spring MVC - 쿠키값 얻기 (0) | 2024.07.02 |
Spring MVC - 요청헤더값 얻기 (0) | 2024.07.02 |