@RequestParam 어노테이션 : HTTP 요청 파라미터를 메서드의 파라미터로 전달받을 때 사용

  ● @RequestParam 어노테이션이 적용된 파라미터가 String이 아닐 경우 자동으로 타입 변환

  ● @RequestParam 어노테이션이 적용된 파라미터는 기본적으로 필수 파라미터(required 속성 값 이용)

    => 필수 파라미터에 대한 요청 파라미터가 없을 경우 400에러 발생

  ● defaultValue 속성을 이용하여 파라미터가 없을 경우 기본값을 설정할 수 있음

 

 

ParamController.java 

package sp.mvc.controller;

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

 

@Controller
public class ParamController {

 

 @RequestMapping("/param.sp")
 public void requestInfo(@RequestParam(value="flag", required=true) String flag, 
   @RequestParam(value="pageNo", required=false, defaultValue="1") int pageNo){
  
  System.out.println("----- ParamController.requestInfo(...) -----");
  System.out.println("flag : " + flag);
  System.out.println("pageNo : " + pageNo);
 }
 
}

 

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>

 

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>

 

 

결과

요청 URL(모든 파라미터 존재)

   http://localhost:8090/spring303/param.sp?flag=list&pageNo=10

 

----- ParamController.requestInfo(...) -----
flag : list
pageNo : 10

요청 URL(필수 파라미터 존재, 기본 값 설정)

   http://localhost:8090/spring303/param.sp?flag=list

 

----- ParamController.requestInfo(...) -----
flag : list
pageNo : 1

요청 URL(필수 파라미터 미존재)

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

 

400에러

 

 

 

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

+ Recent posts