ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring 3.0 webmvc heloworld!!!
    개발노하우/spring.framework 2010. 10. 13. 21:00

    1. eclipse 에서 Maven Project 생성

       - archetype :

             Group Id : org.apache.maven.archetypes

            -  Id : maven-archetype-webapp

        - group Id : 

        - artifact Id : hellospring3

     

     

    2. maven dependency 에 spring-mvc 추가

        - 기본 웹 archetype 을 선택했기때문에 spring-webmvc 라이브러리를  줘야한다.

        - 그리고 기본적으로 메이븐 플러그인에서 이클립스  관리하도록 아래와 같이 버젼셋팅 플러그인을 추가한다.

        -  파일에 아래 추가

    <dependencies>
    ...
      <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>3.0.4.RELEASE</version>
      </dependency>
    </dependencies>
    <build>
    ...
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
         <target>1.6</target>
         <source>1.6</source>
        </configuration>
       </plugin>
      </plugins>
    </build>

        - Maven ->  Project Configuration 로 프로젝트 설정 업데이트

        -  설정을 하지않으면 기본적으로 1.5 이하버젼으로 셋팅되기때문에 어노테이션을 쓸 수가 없음으로 

     


    3. 프로젝트에 java 소스디렉토리 추가

       - 처음 만들면 java 소스 폴더가 없기때문에 만들어주자.

       - New > Source Folder

            Folder name : src/main/java

     

     

     톰켓 작동 테스트

       - Run As ->  build...

          - Goals : tomcat:run

     - http://localhost:8080/hellospring3/ 접속확인

     

     

    5.  파일수정

       - 톰켓으로 들어오는 요청을 spring-webmvc 로  위해서 셋팅을 해야한다.

       - *.do 로 들어오는 모든  spring 이 처리하도록 한다.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
     
     <display-name>Archetype Created Web Application</display-name>
     
     <servlet>
      <servlet-name>hello</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
     
     <servlet-mapping>
      <servlet-name>hello</servlet-name>
      <url-pattern>*.do</url-pattern>
     </servlet-mapping>
     
    </web-app>

        - 기본적인  파일은 jsp 버젼을 2.3 을 채택하고 있기때문에 위 처럼 버젼을  로 바꾸지 않으면 jsp 의 EL 표현식을 사용할 수 없으니 

     

     

    6. spring bean 설정파일 

        - spring bean 설정파일 이름은 [servlet-name]-servlet.xml  만들어야 한다.

        - WEB-INF/hello-servlet.xml 파일 생성해서 아래와  입력하자.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     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:component-scan base-package="net.cranix.web.hellospring3" />
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
     </bean>
     
    </beans>

        - context:component-scan 태그는 이름그대로 어노테이션 기반의 spring3-mvc 컴포넌트를 검색하도록 하기위한 기본 패키지를 지정한다. 여기에 지정된 패키지는 하위 패키지까지 자동으로 검색해서 컴포넌트를 등록해 준다.

        - spring-webmvc 는  가 viewResolver 인 bean 을 가지고 출력할 view 페이지의 경로를  낸다.

     

    7. hello jsp 만들기

       - WEB-INF/jsp/hello.jsp 생성 (jsp 디렉토리도 생성)

    <html>
    <body>
    <h2>hello ${message}</h2>
    </body>
    </html>


    8. hello controller 만들기

       -  -> Class

          - Package : net.cranix.web.hellospring3

          - Name : Hello

     

    package net.cranix.web.hellospring3;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller
    public class Hello {
     @RequestMapping("hello")
     public String hello(Model model) {
      model.addAttribute("message","spring3!!");
      return "hello";
     }
    }


    9. 실행하기

       - Run  -> Maven build...

          - Goals : 

       - http://localhost:8080/hellospring3/hello.do 접속확인

     

     

    10. 마무리

      - 스프링3.0 webmvc 모델은  지원을 강화했다. 거의 xml 편집을 하지않아도 될 정도이기 때문에 상당히  알 수 있다.


    출처 : http://cranix.net/336

     

Designed by Tistory.