※ 뷰 이름을 명시적으로 지정하려면 ModelAndView나 String을 리턴
◆ ModelAndView : 클래스의 생성자 혹은 setViewName() 메소드를 이용
◆ String : 문자열 값이 뷰 이름
ParamController.java
package sp.mvc.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
@Controller public class ParamController {
@RequestMapping("/cookie.sp") public ModelAndView cookieInfo(@CookieValue(value="notice", required=true, defaultValue="0") String auth){ System.out.println("----- ParamController.cookieInfo(...) -----"); System.out.println("auth : " + auth); //ModelAndView modelAndView = new ModelAndView(); //modelAndView.setViewName("moveTest"); ModelAndView modelAndView = new ModelAndView("moveTest"); return modelAndView; }
@RequestMapping("/header.sp") public String 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); return "moveTest"; } } |
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> |
moveTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>View...</title> </head> <body> view... test... </body> </html> |
로그 결과
----- ParamController.cookieInfo(...) ----- auth : 0
----- ParamController.headerInfo(...) ----- Accept-Language : ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4 Cookie : JSESSIONID=983A53215DE5E42DD158EADD9D36F00B
|
[참고자료] Spring 3.0 프로그래밍-최범균