기본 application의 설정

1. 스프링 프레임워크를 다운받아서 임의의 위치에 압축해제한다.

2. web.xml을 수정한다.

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         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" >

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>

</web-app>

3. WEB-INF 하위에 springapp-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

  <bean name="/hello.htm" class="springapp.web.HelloController"/>

</beans>

4. WEB-INF 하위에 lib 디렉토리를 생성하고 아래의 파일들을 복사한다.

spring-framework-2.5/dist/spring.jar
spring-framework-2.5/dist/modules/spring-webmvc.jar
spring-framework-2.5/lib/jakarta-commons/commons-logging.jar

5. 테스트하기 위한 controller를 생성한다.

src/springapp/web/HelloController.java

package springapp.web;

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

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("Returning hello view");

        return new ModelAndView("hello.jsp");
    }

}

6. controller 테스트를 위한 TestCase를 작성한다.

test/springapp/web/HelloControllerTests.java

package springapp.web;

import org.springframework.web.servlet.ModelAndView;

import springapp.web.HelloController;

import junit.framework.TestCase;

public class HelloControllerTests extends TestCase {

    public void testHandleRequestView() throws Exception{  
        HelloController controller = new HelloController();
        ModelAndView modelAndView = controller.handleRequest(null, null);  
        assertEquals("hello.jsp", modelAndView.getViewName());
    }
}

7. build.xml에 테스트에 관한 task를 추가한다.

<property name="test.dir" value="test"/>
       
    <target name="buildtests" description="Compile test tree java files">
        <mkdir dir="${build.dir}"/>
        <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${test.dir}"/>
            <classpath refid="master-classpath"/>
        </javac>
    </target>
   
    <target name="tests" depends="build, buildtests" description="Run tests">
        <junit printsummary="on"
            fork="false"
            haltonfailure="false"
            failureproperty="tests.failed"
            showoutput="true">
            <classpath refid="master-classpath"/>
            <formatter type="brief" usefile="false"/>
           
            <batchtest>
                <fileset dir="${build.dir}">
                    <include name="**/*Tests.*"/>
                </fileset>
            </batchtest>
           
        </junit>
       
        <fail if="tests.failed">
            tests.failed=${tests.failed}
            ***********************************************************
            ***********************************************************
            ****  One or more tests failed!  Check the output ...  ****
            ***********************************************************
            ***********************************************************
        </fail>
    </target>

8. tests 태스크를 실행하여 통과여부를 확인한다.

9. war 디렉토리 하위에 hello.jsp를 생성한다.

<html>
  <head><title>Hello :: Spring Application</title></head>
  <body>
    <h1>Hello - Spring Application</h1>
    <p>Greetings.</p>
  </body>
</html>

10. build.xml의 deploy 태스크를 실행한다.

11. 아래 주소를 실행하여 화면이 정상적으로 나오는것을 확인한다.

http://localhost:8080/springapp/hello.htm




 

'개발 > 스프링' 카테고리의 다른 글

스프링 tutorial (6)  (0) 2008.07.26
스프링 tutorial (5)  (0) 2008.07.25
스프링 tutorial (4)  (0) 2008.07.17
스프링 tutorial (3)  (0) 2008.07.17
스프링 tutorial (1)  (0) 2008.07.16
Posted by 무혹
,