ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 01-6. 컨트롤러 구현 및 전체적인 실행과정
    프로그래밍/Spring 2015. 9. 11. 11:24
    반응형


    SPRING 3.0버전부터는 @Controller Annotation을 이용해서 컨트롤러 Class를 구현하도록 권장하고 있으며 기존의 Controller Interface와 AbstractController Class, AbstractCommandController Class 등은 이제 고전 컨트롤러 (ClassicController)로 불리고 있다.


    @ModelAttribute Annotation을 이용해서 커맨드 객체의 모델 이름을 지정했다면, 뷰 Code에서는 다음과 같이 해당 모델 이름을 사용해서 커맨드 객체에 접근할 수 있다.

    제목 : ${command.title}




    [NewArticleCommand.java]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package spring.chap06.service;
     
    public class NewArticleCommand {
     
        private String title;
        private String content;
        private int parentId;
     
        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 int getParentId() {
            return parentId;
        }
     
        public void setParentId(int parentId) {
            this.parentId = parentId;
        }
     
        @Override
        public String toString() {
            return "NewArticleCommand [content=" + content + ", parentId=" + parentId + ", title=" + title + "]";
        }
     
    }
    cs


    [ArticleService.java]

    1
    2
    3
    4
    5
    6
    7
    8
    package spring.chap06.service;
     
    public class ArticleService {
     
        public void writeArticle(NewArticleCommand command) {
            System.out.println("신규 게시글 등록: " + command);
        }
    }
    cs

    [NewArticleService.java]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package spring.chap06.controller;
     
    import spring.chap06.service.ArticleService;
    import spring.chap06.service.NewArticleCommand;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
     
    @Controller
    @RequestMapping("/article/newArticle.do")
    public class NewArticleController {
        @Autowired
        private ArticleService articleService;
     
        @RequestMapping(method = RequestMethod.GET)
        public String form() {
            return "article/newArticleForm";
        }
     
        @RequestMapping(method = RequestMethod.POST)
        public String submit(@ModelAttribute("command") NewArticleCommand command) {
            articleService.writeArticle(command);
            return "article/newArticleSubmitted";
        }
     
        public void setArticleService(ArticleService articleService) {
            this.articleService = articleService;
        }
     
    }
    cs




    [/WEB-INF/dispatcher-servlet.xml]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?xml version="1.0" encoding="UTF-8"?>
     
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <bean id="helloController" class="spring.chap06.controller.HelloController" />
     
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
        </bean>
     
     
        <bean id="newArticleController" class="spring.chap06.controller.NewArticleController"
            p:articleService-ref="articleService" />
     
        <bean id="articleService" class="spring.chap06.service.ArticleService" />
     
    </beans>
    cs


    [/WEB-INF/view/article/newArticleForm.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>게시글 쓰기</title>
    </head>
    <body>
        게시글 쓰기 입력 폼:
        <form method="post">
            <input type="hidden" name="parentId" value="0" /> 제목: <input
                type="text" name="title" /><br /> 내용:
            <textarea name="content"></textarea>
            <br /> <input type="submit" />
        </form>
    </body>
    </html>
     
    cs


    [/WEB-INF/view/article/newArticleSubmit.jsp]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <%@ page language="java" contentType="text/html; charset=EUC-KR"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <title>게시글 쓰기</title>
    </head>
    <body>
        게시글 등록됨:
        <br /> 제목: ${command.title}
    </body>
    </html>
    cs


    반응형

    댓글

Designed by Tistory.