뷰 이름을 명시적으로 지정하려면 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

<?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>

 

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 프로그래밍-최범균

+ Recent posts