1. 설정 파일
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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>
<context:component-scan base-package="spring.mvc.ch01" />
<!-- <beans:bean id="newArticleController" class="spring.mvc.chap02_controller.NewArticleController" /> -->
<!-- <beans:bean id="article" class="spring.mvc.chap02_service.ArticleService" /> -->
<context:component-scan base-package="spring.mvc.ch02" />
<!-- 위의 두 줄을 이렇게 한 줄로 쓸 수 있다. -->
</beans:beans>
2. 컨트롤러
package spring.mvc.ch02.controller;
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;
import spring.mvc.ch02.model.NewArticleDTO;
import spring.mvc.ch02.service.ArticleService;
@Controller
public class NewArticleController {
@Autowired
private ArticleService articleService;
/*public void setArticleService(ArticleService articleService){
this.articleService = articleService;
}*/
// 특정 클라이언트의 요청에 form 메서드를 연결시키는 방법
@RequestMapping(value = "/newArticle.do", method = RequestMethod.GET)
public String form(){
return "article/newArticleForm";
// .do를 리턴 String타입으로 newArticleForm.jsp를 연결시킨다.
}
@RequestMapping(value="/newArticle.do", method=RequestMethod.POST)
public String submit(@ModelAttribute("dto") NewArticleDTO dto){
articleService.writeArticle(dto);
return "article/newArticleResult";
} // get 리퀘스트 객체 필요없이 위와 같이 ModelAttribute로 dto 설정만 해주면 된다.
}
2-1. 컨트롤러
리퀘스트 매핑 중복 밸류값 축약한 경우
package spring.mvc.ch02.controller;
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;
import spring.mvc.ch02.model.NewArticleDTO;
import spring.mvc.ch02.service.ArticleService;
@Controller
@RequestMapping("/newArticle.do") // 이하의 RequestMapping Value값을 이렇게 하나로 축약 가능
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("dto") NewArticleDTO dto){
articleService.writeArticle(dto);
return "article/newArticleResult";
}
}
3. DTO
package spring.mvc.ch02.model;
import org.springframework.stereotype.Component;
@Component
public class NewArticleDTO {
private int no;
private String title;
private String content;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
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;
}
@Override
public String toString() {
return "NewArticleDTO [no="+no+", title="+title+", content="+content+"]";
}
}
4. 서비스
package spring.mvc.ch02.service;
import org.springframework.stereotype.Service;
import spring.mvc.ch02.model.NewArticleDTO;
@Service
public class ArticleService {
public void writeArticle(NewArticleDTO dto) {
System.out.println("게시판 글 DB에 등록 "+dto);
}
}
6. index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="<%= request.getContextPath() %>/hello.do">ch01_HelloController</a><br>
<a href="<%= request.getContextPath() %>/newArticle.do" >ch02_NewArticleController</a><br>
</body>
</html>
7. 위의 입력폼에서 전달받은 값을 DB에 입력한 후의 결과를 가상으로 출력하는 jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력 결과</title>
</head>
<body>
<h2>게시글 등록 완료</h2>
<ul>
<li>번호: ${dto.no}</li>
<li>제목: ${dto.title}</li>
<li>내용: ${dto.content}</li>
</ul>
<br />
<a href="<%= request.getContextPath()%>/index.jsp">index.jsp로 돌아가기</a>
</body>
</html>
게시글 등록 완료
- 번호: 0
- 제목: 게시물 제목 테스트
- 내용: 게시물 내용 테스트
' Spring Framework' 카테고리의 다른 글
스프링 MVC로 게시판 구현: 리스트 화면 (방법1: BeanNameUrlHandlerMapping) (0) | 2016.04.07 |
---|---|
스프링 MVC 패턴 간단하게 구현하기 (어노테이션 활용) (0) | 2016.04.07 |
브라우저의 실행 경로 간단하게 변경하기 (0) | 2016.04.07 |
Spring MVC 실습 (0) | 2016.04.06 |
Spring MVC 세팅 (0) | 2016.04.05 |