해당 글은 intelliJ 에서 스프링 MVC 를 해보고자 하는 저같은 초보!! 들에게 필요한 내용을 위주로 담고 있습니다.
고로 좀더 나이스한 설정법과 기타 미려한 내용들은 다 빠지고 ...

그야 말로 우선 가장 작은 단위를 만들어서 돌려보는것이 의의를 가지고 있습니다.

이렇게 한번 돌린것을 기반으로 변경해나가시면 훌륭한 프레임웍을 구성하실 수 있을거라 믿습니다.

참고


-------------------------------------------------------------------------------

1. File --> new Project --> create project from scratch


2. 프로젝트 이름정하고 아래 모듈이름도 정한다음...

모듈은 Maven Module 로 선택하고 

다음을 누른뒤 화면에서 Create from archetype 를 체크하면 프로토타입이 나온다.

(안나오면 언체크 한뒤 다시 체크하면됨)


org.apache.maven.archetypes:maven-archetype-webapp

위의것을 선택하면 기본 구조가 나온다.




3. pom.xml에 

 <properties>

        <java-version>1.6</java-version>

        <org.springframework-version>3.0.6.RELEASE</org.springframework-version>

        <org.aspectj-version>1.6.9</org.aspectj-version>

        <org.slf4j-version>1.5.10</org.slf4j-version>

    </properties>

추가하고  (일종의 변수이다...변수선언.. 이걸 한다고 해서 해당 버전으로 고정되는게 아니라 저걸 정의하는곳에 이용할뿐이다)

dependencies 아래에

 <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-webmvc</artifactId>

      <version>${org.springframework-version}</version>

    </dependency>


스프링 소스 홈페이지에 보면 maven 설정을 위한 내용이 있다. http://www.springsource.org/spring-framework#maven   

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.1.1.RELEASE</version> 

</dependency> 

추가하고, 오른쪽 메이븐프로젝트 사이드바에서 reimport를 하든지 이벤트로그에서 

Maven projects need to be imported: Import Changes Enable Auto-Import 에서 Import Changes를 선택하면된다.




4. web.xml

우선 web-app 태그에 값으로

version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

를 넣는다.

<web-app

  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


 web-app 안에 서블릿 설정 추가


    <servlet>

        <servlet-name>example</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>


    <servlet-mapping>

        <servlet-name>example</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>


 


5. src->main 아래에 디렉토리 java 생성하고 F4를 눌러서 프로젝트 구조를 띄운다음

오른쪽에서 좀전에 만든 java를 선택하고 마우스오른쪽 -->make directory as --> Source를 선택하여 소스디렉토리로 사용


6. 해당 소스디렉토리 이후레 패키지 생성 ex. com.sib.test


그리고 클래스를 하나 만들자

package com.sib.test;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;


/**

 * Created by IntelliJ IDEA.

 * User: nexGen

 * Date: 12. 3. 27

 * Time: 오후 2:29

 * To change this template use File | Settings | File Templates.

 */


@Controller

public class HelloWorldController {


    @RequestMapping("/helloWorld")

    public ModelAndView helloWorld() {

        ModelAndView mav = new ModelAndView();

        mav.setViewName("helloWorld");

        mav.addObject("message","Hello World");

        return mav;

    }

}



그리고 view 를 만들어두자.위에서 ViewName을 helloWorld로 했으니..

WEB-INF 밑에 views 디렉토리를 만들고 helloWorld.jsp를 추가

hello<br/>


${message}~~~



간단하게 만들었다.



7. 이제 web.xml에 스프링관련 설정.xml 을 추가하자.

서블릿이름-servlet.xml 을 하든지 아래처럼 명시적으로

<servlet> 안에 

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>

</init-param>


관리하기 쉽도록 명시적으로 처리하자.

해당 디렉토리를 만들고 파일도 만들면 web.xml에 있는 에러내용이 없어질것이다.

해서 만들어진 결과 

최종 web.xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app

  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name>Archetype Created Web Application</display-name>


    <servlet>

        <servlet-name>example</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>


    <servlet-mapping>

        <servlet-name>example</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>


</web-app>






빈의 오토스캔을 위해 servlet-context.xml에

<context:component-scan base-package="com.sib.test"/>

를 넣어야 하는데... context 태그를 쓰기위해서는 beans 선언에 추가할것이 있다.


xmlns:context="http://www.springframework.org/schema/context"

를 하면 context 태그를 쓸 수 있다. 우선 아래를 참고해라


<?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="org.springframework.samples.petclinic.web"/>


    // ...


</beans>


결과 최종 servlet-context.xml

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

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:beans="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"


       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context-3.0.xsd

       http://www.springframework.org/schema/mvc

       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


       <context:component-scan base-package="com.sib.test"/>


    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <beans:property name="prefix" value="/WEB-INF/views/" />

        <beans:property name="suffix" value=".jsp" />

    </beans:bean>


</beans:beans>


 



tomcat에 소스를 deploy  해야한다.


그전에 maven 에서 소스를 package 해야한다. (war 생성)

compile 하고 package 해야하는지는.. 잘 모르겠고;


8. 우선 톰켓이 없으면 설정한다. 프로젝트 최상위에 마우스 선택후 Add Framework Support

--> application server --> tomcat


다음 상단에 play 바 왼쪽에 셀렉트 박스에서 톰켓을 선택하고 다시 선택하여 Edit Configurations 선택

--> deployement 탭을 선택하여 + 표시.. 선택 --> Artifact --> 나오는 war 선택


이제 서버기동!



3월 27, 2012 3:24:55 오후 org.springframework.web.servlet.FrameworkServlet initServletBean

정보: FrameworkServlet 'example': initialization started

3월 27, 2012 3:24:55 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh

정보: Refreshing WebApplicationContext for namespace 'example-servlet': startup date [Tue Mar 27 15:24:55 KST 2012]; root of context hierarchy

3월 27, 2012 3:24:55 오후 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

정보: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]

3월 27, 2012 3:24:56 오후 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@60bd76d7: defining beans [helloWorldController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy

3월 27, 2012 3:24:56 오후 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

정보: Mapped URL path [/helloWorld] onto handler 'helloWorldController'

3월 27, 2012 3:24:56 오후 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

정보: Mapped URL path [/helloWorld.*] onto handler 'helloWorldController'

3월 27, 2012 3:24:56 오후 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

정보: Mapped URL path [/helloWorld/] onto handler 'helloWorldController'


servlet 'example'을 기동했고 mapping 정보를 읽어왔다.




http://localhost:8080/helloWorld


해보자. 나온다~



+ Recent posts