-
[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]
1234567891011121314151617181920212223242526272829303132333435363738package 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;}@Overridepublic String toString() {return "NewArticleCommand [content=" + content + ", parentId=" + parentId + ", title=" + title + "]";}}cs [ArticleService.java]
12345678package spring.chap06.service;public class ArticleService {public void writeArticle(NewArticleCommand command) {System.out.println("신규 게시글 등록: " + command);}}cs [NewArticleService.java]
123456789101112131415161718192021222324252627282930313233package 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 {@Autowiredprivate 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]
123456789101112131415161718192021222324<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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]
1234567891011121314151617<%@ 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" /> 제목: <inputtype="text" name="title" /><br /> 내용:<textarea name="content"></textarea><br /> <input type="submit" /></form></body></html>cs [/WEB-INF/view/article/newArticleSubmit.jsp]
1234567891011<%@ 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 반응형'프로그래밍 > Spring' 카테고리의 다른 글
[Spring] 01-7. HTML 폼과 커맨드 객체(JavaBean) (0) 2015.09.13 [Spring] 02-1. 뷰 영역 구현 (0) 2015.09.11 [Spring] 01-5. 컨트롤러 구현 및 설정 추가 (0) 2015.09.11 [Spring] 스프링을 시작하며 필요한 라이브러리 파일들 (0) 2015.09.10 [Spring] 01-4. DispatcherServlet 설정 및 SPRING 컨텍스트 설정 (0) 2015.09.10