◆ 컨트롤러 클래스 구현 : @Controller 어노테이션 + @RequestMapping 어노테이션

  ● 컨트롤러 클래스에 @Controller 어노테이션을 적용

  ● 클라이언트의 요청을 처리할 메소드에 @RequestMapping 어노테이션을 적용

  ● 설정 파일에 컨트롤러 클래스를 빈으로 등록

 

HelloController.java

package sp.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

 

@Controller
public class HelloController {

 

 @RequestMapping(value="/hello.sp", method=RequestMethod.GET)
 public String helloGet(){
  System.out.println("----- GET -----");
  return "helloG";
 }
 
 @RequestMapping(value="/hello.sp", method=RequestMethod.POST)
 public String helloPost(){
  System.out.println("----- POST -----");
  return "helloP";
 }
}

 

/*

  클라이언트를 처리하는 메소드가 동일한 URI를 처리할 경우 @RequestMapping 어노테이션을 클래스에

  적용해서 해당 클래스가 처리할 기본 URI를 지정

*/

 

package sp.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

 

@Controller

@RequestMapping("/hello.sp")
public class HelloController {

 

 @RequestMapping(method=RequestMethod.GET)
 public String helloGet(){
  System.out.println("----- GET -----");
  return "helloG";
 }
 
 @RequestMapping(method=RequestMethod.POST)
 public String helloPost(){
  System.out.println("----- POST -----");
  return "helloP";
 }
}

● 리턴 타입이 String인 경우, 메소드의 리턴 값은 컨트롤러 처리 결과의 뷰 이름으로 사용 됨

● @RequestMapping 어노테이션의 method속성을 이용하여 메소드가 처리할 HTTP 메서드를 제한할 수 있음

 

 

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>

 

helloG.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>Insert title here</title>
</head>
<body>
 GET <br>
 
 <form action="/myTest/hello.sp" method="post">
  <input type="submit" name="submit" value="POST 전송"/>
 </form>
</body>
</html>

 

helloP.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>Insert title here</title>
</head>
<body>
 POST
</body>
</html>

 

결과

----- GET -----
----- POST -----

 

 

 

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

 

+ Recent posts