by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

3. Hello World 예제




3. Hello World 예제

3.1 Controller 서블릿 작성

  • AbstractController 를 상속하여 Controller를 구현한다.
HelloWorldController.java
package net.gurubee.web.sample.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, 
         HttpServletResponse response) throws Exception {

        ModelAndView mv = new ModelAndView();
        
        //view 지정
        mv.setViewName("sample/hello");
        
        //result obejct 지정
        mv.addObject("msg", "Hello World!");

        return mv;

    }
}

3.2 mvc-dispatcher-servlet.xml 설정

  • viewResolver 설정
  • HelloWorldController bean 등록
mvc-dispatcher-servlet.xml
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean name="/sample/hello.do" class="net.gurubee.web.sample.controller.HelloWorldController"  />

3.3 jsp 작성

  • /WEB-INF/pages/sample/hello.jsp 파일을 작성한다.
hello.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Spring MVC Hello World Example</h1>
 
<h2>${msg}</h2>
 
</body>
</html>
 

3.4 테스트

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.