[기초] Spring MVC 구성해 보기.

ITWeb/개발일반 2012. 3. 9. 14:24
기냥 기초 부터 다시 작성해 보려 한다.

1. Spring MVC 구성해 보기.
2. Spring MVC 에 MyBatis 적용해 보기.
3. Spring MVC 에 Spring Security 적용해 보기. 
4. Spring MVC 에 Hibernate 적용해 보기. 
5. 2+3번 적용해 보기.
6. 3+4번 적용해 보기. 



[준비물]

- SpringSourceTool Suite 에서 Spring Project Template 중 MVC 로 기본 프로젝트 생성.
- 괜히 노가다 한다고 시간 빼지 말자.
- Eclipse 또는 STS
- JDK 1.6
- Maven
- Tomcat 

 

[pom.xml]
- 내가 가지고 있던 sts 버전땜시 spring framework 버전을 수정함. (3.0.6 -> 3.1.0)

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>proto.ncms</groupId>

    <artifactId>board</artifactId>

    <name>board</name>

    <packaging>war</packaging>

    <version>1.0.0-BUILD-SNAPSHOT</version>

    <properties>

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

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

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

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

    </properties>

    <dependencies>

        <!-- Spring -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

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

            <exclusions>

                <!-- Exclude Commons Logging in favor of SLF4j -->

                <exclusion>

                    <groupId>commons-logging</groupId>

                    <artifactId>commons-logging</artifactId>

                 </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

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

        </dependency>


        <!-- AspectJ -->

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjrt</artifactId>

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

        </dependency>


        <!-- Logging -->

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-api</artifactId>

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

        </dependency>

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>jcl-over-slf4j</artifactId>

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

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-log4j12</artifactId>

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

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>1.2.15</version>

            <exclusions>

                <exclusion>

                    <groupId>javax.mail</groupId>

                    <artifactId>mail</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>javax.jms</groupId>

                    <artifactId>jms</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>com.sun.jdmk</groupId>

                    <artifactId>jmxtools</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>com.sun.jmx</groupId>

                    <artifactId>jmxri</artifactId>

                </exclusion>

            </exclusions>

            <scope>runtime</scope>

        </dependency>


        <!-- @Inject -->

        <dependency>

            <groupId>javax.inject</groupId>

            <artifactId>javax.inject</artifactId>

            <version>1</version>

        </dependency>


        <!-- Servlet -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>2.5</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>javax.servlet.jsp</groupId>

            <artifactId>jsp-api</artifactId>

            <version>2.1</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

            <version>1.2</version>

        </dependency>


        <!-- Test -->

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.7</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                    <source>${java-version}</source>

                    <target>${java-version}</target>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>

                <configuration>

                    <warName>abc</warName>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-dependency-plugin</artifactId>

                <executions>

                    <execution>

                        <id>install</id>

                        <phase>install</phase>

                        <goals>

                            <goal>sources</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-resources-plugin</artifactId>

                <version>2.5</version>

                <configuration>

                    <encoding>UTF-8</encoding>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>



[기타설정]

WEB-INF/web.xml
WEB-INF/spring/root-context.xml
WEB-INF/spring/appServlet/servlet-context.xml 

일단 다른 애덜이랑 연동할 일이 없으니까 그냥 default 설정으로 놔둬도 된다. 

 

※ 추가 작성할 패키지는 향후 MyBatis 와 연동 하기 위한 구조로 작성함.

proto.board
proto.board.web : Controller
proto.board.service : Biz logic
proto.board.domain : VO
proto.board.dao  : Data Access Object



[Controller 작성]
- 기본 HomeController.java 가 있으니 작성하기 전에 이게 잘 뜨는지 확인

[BoardContentViewController.java]

package proto.board.web;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

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


import proto.board.service.BoardContentViewService;


@Controller

public class BoardContentViewController {


    private static final Logger logger = LoggerFactory.getLogger(BoardContentViewController.class);


    @Autowired

    private BoardContentViewService boardContentViewService;


    @RequestMapping(value = "/boardContentView", method = RequestMethod.GET)

    public String boardContentView(Locale locale, Model model) {

        logger.info("Welcome home! the client locale is "+ locale.toString());

        logger.info("Autowired :  "+ boardContentViewService.getBoardContentVO());


        model.addAttribute("val", boardContentViewService.getBoardContentVO() );


        return "boardContentView";

    }

}


[boardContentView.jsp]

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page session="false" %>

<html>

<head>

    <title>BoardContentView</title>

</head>

<body>

<h1>

    BoardContentView

</h1>


<P>  The Request is ${val.content}. </P>

</body>

</html>  



[Domain 작성]

[BoardContentVO.java]

package proto.board.domain;


public class BoardContentVO {

    private int documentSrl;

    private String title;

    private String content;

    private String nickname;

    private String email;

    private String regdate;


    public int getDocumentSrl() {

        return documentSrl;

    }


    public void setDocumentSrl(int documentSrl) {

        this.documentSrl = documentSrl;

    }


    public String getTitle() {

        return title;

    }


    public void setTitle(String title) {

        this.title = title;

    }


    public String getContent() {

        return content;

    }


    public void setContent(String content) {

        this.content = content;

    }


    public String getNickname() {

        return nickname;

    }


    public void setNickname(String nickname) {

        this.nickname = nickname;

    }


    public String getEmail() {

        return email;

    }


    public void setEmail(String email) {

        this.email = email;

    }


    public String getRegdate() {

        return regdate;

    }


    public void setRegdate(String regdate) {

        this.regdate = regdate;

    }

} 



[Service 작성]

[BoardContentViewServiceImpl.java]

package proto.board.service;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import proto.board.dao.BoardContentViewDAO;

import proto.board.domain.BoardContentVO;


@Service

public class BoardContentViewServiceImpl implements BoardContentViewService {


    @Autowired

    private BoardContentViewDAO boardContentViewDAO;


    public BoardContentVO getBoardContentVO() {

        return (BoardContentVO)boardContentViewDAO.getBoardContentVO();

    }

}


[BoardContentViewService.java]

package proto.board.service;


import proto.board.domain.BoardContentVO;



public interface BoardContentViewService {

    BoardContentVO getBoardContentVO();

}



[DAO 작성]

[BoardContentViewDAOImpl.java]

package proto.board.dao;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;


import proto.board.domain.BoardContentVO;


@Repository

public class BoardContentViewDAOImpl implements BoardContentViewDAO {


    private BoardContentVO boardContentVO = new BoardContentVO();


    public BoardContentVO getBoardContentVO() {

        boardContentVO.setContent("CONTENT");


        return boardContentVO;

    }

}


[BoardContentViewDAO.java] 

package proto.board.dao;


import proto.board.domain.BoardContentVO;


public interface BoardContentViewDAO {

    BoardContentVO getBoardContentVO();

} 



※ 자 그럼 build 를 하고 실행을 하봅시다.
[Eclipse Console 로그확인]
- 아래 로그는 실제 servlet-context.xml 에서 component-scan 일 잘 되었는지 확인하기 위함 이다.
- 이게 정상적으로 나오지 않을 경우는 "not found url mapping" 어쩌구 에러가 발생 합니다.

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String proto.board.HomeController.home(java.util.Locale,org.springframework.ui.Model)

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/boardContentView],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String proto.board.web.BoardContentViewController.boardContentView(java.util.Locale,org.springframework.ui.Model)



[Tomcat 실행결과]



: