ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 01-5. 컨트롤러 구현 및 설정 추가
    프로그래밍/Spring 2015. 9. 11. 00:23
    반응형

    컨트롤러 구현시 먼저 @Controller Annotation을 클래스에 적용해야하며 @RequestMapping Annotation을 이용해서 클라이언트의 요청을 처리할 메서드를 지정한다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    package spring.chap06.controller;
     
    import java.util.Calendar;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
     
    @Controller
    public class HelloController {
     
        @RequestMapping("/hello.do")
        public ModelAndView hello() {
            ModelAndView mav = new ModelAndView();
            mav.setViewName("hello");
            mav.addObject("greeting", getGreeting());
            return mav;
        }
     
        private String getGreeting() {
            int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            if (hour >= 6 && hour <= 10) {
                return "좋은 아침입니다.";
            } else if (hour >= 12 && hour <= 15) {
                return "점심 식사는 하셨나요?";
            } else if (hour >= 18 && hour <= 22) {
                return "좋은 밤 되세요";
            }
            return "안녕하세요";
        }
     
    }

    cs



    @Controller Annotation은 해당 클래스가 스트링 MVC 컨트롤러를 구현한 클래스라는 것을 지정한다.

    그리고 @RequestMapping Annotation은 값으로 지정한 요청 경로를 처리할 메서드를 설정한다.




    SPRING MVC는 ModelAndView 뿐만 아니라 String이나 ModelMap, 또는 Map과 같은 타입을 이용해서 뷰 이름과 모델 정보를 설정할 수 있도록 하고 있다.


    DispatcherServlet은 SPRING Container에서 컨트롤러 객체를 검색하기 때문에 SPRING 설정 파일에 컨트롤러를 빈으로 등록해주어야 한다. 다음 코드는 설정 파일의 작성 예를 보여주고 있다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0" encoding="UTF-8"?>
     
    <beans xmlns="http://www.springframework.org/schema/beans"
        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">
     
        <bean id="helloController" class="spring.chap06.controller.HelloController" />
     
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
        </bean>
     
    </beans>
            
    cs




    뷰 코드 JSP 구현


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <%@ 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>
        인사말:
        <strong>${greeting}</strong>
    </body>
    </html>
    cs


    반응형

    댓글

Designed by Tistory.