ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 01-9. CookieValue Annotation을 이용한 쿠키 Mapping, 파라미터 타입 정리
    프로그래밍/Spring 2015. 9. 14. 15:24
    반응형

    @CookieValue Annotation을 이용하면 쿠키 값을 파라미터로 전달받을 수 있다.


    @CookieValue Annotation은 해당 쿠키가 존재하지 않으면 기본적으로 500 에러를 발생시킨다. 따라서, 쿠키가 필수가 아닌 경우에는 required 속성의 값을 false로 지정해 주어야 한다. required 속성의 기본 값은 true이다.

    required 속성의 값을 false로 지정할 경우, 해당 쿠키가 존재하지 않으면 null을 값으로 전달받게 된다.




    @RequestParam Annotation과 마찬가지로 defalutValue 속성을 이용해서 기본 값을 설정할 수도 있다.


    [CookieController.java]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package spring.chap06.controller;
     
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletResponse;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.CookieValue;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    @Controller
    public class CookieController {
     
        @RequestMapping("/cookie/make.do")
        public String make(HttpServletResponse response) {
            response.addCookie(new Cookie("auth""1"));
            return "cookie/made";
        }
     
        @RequestMapping("/cookie/view.do")
        public String view(@CookieValue(value = "auth", defaultValue = "0"String auth) {
            System.out.println("auth 쿠키: " + auth);
            return "cookie/view";
        }
    }
    cs


    [/WEB-INF/dispatcher-servlet.xml]

    1
    <bean id="cookieController" class="madvirus.spring.chap06.controller.CookieController" />
    cs


    [/WEB-INF/view/cookie/made.jsp]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <title>쿠키</title>
        </head>
        <body>
        쿠키 생성함
        </body>
        </html>
     
    cs


    [/WEB-INF/view/cookie/view.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <title>쿠키</title>
        </head>
        <body>
        쿠키 확인
        </body>
        </html>
    cs


    서버 구동후 아래 주소로 실행

    http://localhost:8080/SpringMVC01/cookie/make.do

    http://localhost:8080/SpringMVC01/cookie/view.do


    * @RequestHeader Annotation을 이용한 헤더 Mapping




    @RequestHeader Annotation을 이용하면 HTTP 요청 헤더의 값을 메서드의 파라미터로 전달 받을 수 있다.

    @RequestHeader Annotation도 @RequestCookie Annotation과 마찬가지로 해당 해더가 존재하지 않으면 500 응답 에러 Code를 전송한다. 또한, required 속성과 defalutValue 속성을 이용해서 필수 여부와 기본 값을 설정할 수 있다.


    [HeaderController.java]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package spring.chap06.controller;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
     
    @Controller
    public class HeaderController {
     
        @RequestMapping("/header/check.do")
        public String check(@RequestHeader("Accept-Language"String languageHeader) {
            System.out.println(languageHeader);
            return "header/pass";
        }
    }
    cs


    [/WEB-INF/dispatcher-servlet.xml]

    1
    <bean class="madvirus.spring.chap06.controller.HeaderController" />
    cs


    [/WEB-INF/view/header/pass.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>헤더 검사 통과</title>
    </head>
    <body>헤더 검사 통과됨
    </body>
    </html>
    cs


    서버 구동후 아래 주소로 실행

    http://localhost:8080/SpringMVC01/header/check.do


    * Servlet API 직접 사용


    컨트롤러 Class의 @RequestMapping Annotation이 적용 메서드는 다음의 다섯 가지 타입의 파라미터를 전달받을 수 있다.

    - javax.servlet.http.HttpServletRequest / javax.servlet.ServletRequest

    - javax.servlet.http.HttpServletResponse / javax.servlet.ServletResponse

    - javax.servlet.http.HttpSession


    Servlet API를 사용할 필요 없이 SPRING MVC가 제공하는 Annotation을 이용해서 요청 파라미터, 헤더, 쿠키, 세션 등의 정보에 접근할 수 있기때문에, 직접적으로 Servlet API를 사용해야 하는 경우는 매우 드물다. 하지만 다음의 경우에는 Servlet API를 사용하는 것이 더 편리할 수 있다.

    - HttpSession의 생성을 직접 제어해야 하는 경우

    - 컨트롤러에서 쿠키를 생성해야 하는 경우

    - Servlet API 사용을 선호하는 경우


    HttpSession 타입의 파라미터를 가질 경우 세션이 생성된다는 점에 유의해야 한다. 즉, 기존에 세션이 존재한다면 해당 세션이 전달되고 그렇지 않다면 새로운 세션이 생성되고 관련 HttpSession 인스턴스가 파라미터에 전달된다. 따라서, HttpSession 타입의 파라미터는 항상 null이 아니다.

    반응형

    댓글

Designed by Tistory.