@CookieValue 어노테이션 : 쿠키 값을 파라미터로 전달 받을 수 있음.

  ● 쿠키가 존재하지 않으면 500에러

  ● required 속성 값을 이용해 필수여부 설정

  ● defaultValue 속성 값을 이용해 기본 값 설정

 

@RequestHeader 어노테이션 : HTTP 요청 헤더의 값을 메서드의 파라미터로 전달

  ● 헤더가 존재하지 않으면 500에러

  ● required 속성 값을 이용해 필수여부 설정

  ● defaultValue 속성 값을 이용해 기본 값 설정

 

 

ParamController.java

package sp.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

 

@Controller
public class ParamController {

 
 @RequestMapping("/cookie.sp")
 public void cookieInfo(@CookieValue(value="notice", required=true, defaultValue="0") String auth){
  System.out.println("----- ParamController.cookieInfo(...) -----");
  System.out.println("auth : " + auth);
 }
 
 @RequestMapping("/header.sp")
 public void headerInfo(

 @RequestHeader(value="Accept-Language", required=false, defaultValue="ko") String AccLang, 
 @RequestHeader(value="Cookie", required=false, defaultValue="ㅎㅎㅎ") String cookie){
  System.out.println("----- ParamController.headerInfo(...) -----");
  System.out.println("Accept-Language : " + AccLang);
  System.out.println("Cookie : " + cookie);
 }

 
}

 

main_config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>
 <context:component-scan base-package="sp.mvc.controller"/>
 
 <!-- ViewResolver 설정 -->
 <bean id="internalResource" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="jsp/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>


 <!-- 각종 bean 설정 -->

 
</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 
  <display-name>spring303</display-name>

 <filter>
  <filter-name>characterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>


 <!-- 공통 빈 설정
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
  
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 -->

 
 <servlet>
  <servlet-name>spring303</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/main_config.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>spring303</servlet-name>
  <url-pattern>*.sp</url-pattern>
 </servlet-mapping>

</web-app>

 

결과

요청 URL

   http://localhost:8090/spring303/cookie.sp

 

----- ParamController.cookieInfo(...) -----
auth : 0

요청 URL

   http://localhost:8090/spring303/header.sp

 

----- ParamController.headerInfo(...) -----
Accept-Language : ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Cookie : JSESSIONID=068217B91C3FDC686EF6B25BA40EDE45

 

 

 

[참고자료] Spring 3.0 프로그래밍-최범균

+ Recent posts