ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 01-8. 컨트롤러 메서드의 파라미터 타입
    프로그래밍/Spring 2015. 9. 14. 14:52
    반응형

    컨트롤러의 @RequestMapping Annotation이 적용된 메서드는 커맨드 Class뿐만 아니라 HttpServletRequest, HttpSession, Locale 등 WEB Application과 관련된 다양한 타입의 파라미터를 가질 수 있는데, 전달 가능한 파라미터 타입은 아래와 같다.


    * @RequestParam Annotation을 이용한 파라미터 Mapping

    컨트롤러를 구현하면서 가장 많이 사용되는 Annotation이 바로 @RequestParam Annotation이다.

    @RequestParam Annotation은 HTTP 요청 파라미터를 메서드의 파라미터로 전달받을 때 사용된다.


    @RequestParam Annotation이 적용된 파라미터는 기본적으로 필수 파라미터이다. 따라서 @RequestParam Annotation에 명시한 HTTP 요청 파라미터가 존재하지 않을 경우 SPRING MVC는 잘못된 요청(Bad Request)을 의미하는 400 응답 Code를 WEB 브라우저에 전송한다.




    필수가 아닌 파라미터인 경우 required 속성 값을 false로 지정해 주면 된다.


    [SearchController.java]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package spring.chap06.controller;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
     
    @Controller
    public class SearchController {
     
        @RequestMapping("/search/internal.do")
        public ModelAndView searchInternal(@RequestParam("query"String query,
                @RequestParam(value = "p", defaultValue = "1"int pageNumber) {
            System.out.println("query=" + query + ",pageNumber=" + pageNumber);
            return new ModelAndView("search/internal");
        }
     
        @RequestMapping("/search/external.do")
        public ModelAndView searchExternal(@RequestParam(value = "query", required = falseString query,
                @RequestParam(value = "p", defaultValue = "1"int pageNumber) {
            System.out.println("query=" + query + ",pageNumber=" + pageNumber);
            return new ModelAndView("search/external");
        }
    }
    cs


    [/WEB-INF/dispatcher-servlet.xml]

    1
    <bean id="searchController" class="madvirus.spring.chap06.controller.SearchController" />
    cs


    [/WEB-INF/view/search/internal.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
           <html>
           <head>
           <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
           <title>내부 검색</title>
           </head>
           <body>
           내부 검색
           </body>
           </html>
    cs


    [/WEB-INF/view/search/external.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
           <html>
           <head>
           <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
           <title>외부 검색</title>
           </head>
           <body>
           외부 검색
           </body>
           </html>
    cs




    서버 구동후 아래 주소로 접속

    http://localhost:8080/Spring07/search/internal.do?query=q&p=2

    http://localhost:8080/Spring07/search/internal.do?query=q

    http://localhost:8080/Spring07/search/internal.do

    http://localhost:8080/Spring07/search/external.do?query=p&p=22

    http://localhost:8080/Spring07/search/external.do?query=p

    http://localhost:8080/Spring07/search/external.do

    반응형

    댓글

Designed by Tistory.