@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
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> |
결과
[참고자료] Spring 3.0 프로그래밍-최범균