ํฐ์คํ ๋ฆฌ ๋ทฐ
Project/SpringMVC
[SpringMVC] ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๊ฒ์ํ ํ๋ฉด ์์ฒญ๊ณผ ๊ธฐ๋ฅ ์์ฒญ
yeahajeong 2019. 8. 9. 14:37๋ฐ์ํ
BoardController.java
๊ฒ์ํ ํ๋ฉด ์์ฒญ GET์์ฒญ
package com.spring.myapp.board.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.spring.myapp.board.model.BoardVO;
import com.spring.myapp.board.service.IBoardService;
//๊ฒ์ํ ์์ ์์ฒญ์ ์ฒ๋ฆฌํด ์ค ์ปจํธ๋กค๋ฌ์ด๋ค. -> ๊ทธ๋ฌ๊ธฐ ์ํด ๋น๋ฑ๋ก์ ํด์ฃผ์ด์ผํ๋ค.
@Controller //์ปจํธ๋กค๋ฌ ๋น๋ฑ๋กํด์ฃผ๋ ์ด๋
ธํ
์ด์
@RequestMapping("/board") //"/board"์์ฒญ์ด ๋ค์ด์ฌ ๋ ๋ค ์ฌ๊ธฐ์ ์ฒ๋ฆฌํ๊ฒ ๋ค ํ๋ ๊ณตํต URL์์ฑ!!
public class BoardController {
//๋ก๊ฑฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ก๊ฑฐ๊ฐ์ฒด ์ ์ธ
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
//์ปจํธ๋กค๋ฌ์ ์๋น์ค๊ฐ ์์กด๊ด๊ณ๊ฐ ์ค์ ๋์ด์์ผ๋ ์์กด์ฑ์ฃผ์
ํด์ค๋ค.
@Autowired
private IBoardService service;
///////////////////// ์ด๋ ์์ฒญ [GET] ////////////////////////
//๊ฒ์๊ธ ๋ชฉ๋ก ํ์ด์ง ์ด๋ ์์ฒญ(๋ชจ๋ ๊ฒ์๋ฌผ ์กฐํ) ์ฒ๋ฆฌ ๋ฉ์๋ -> ์ด ๋ฉ์๋์ ํธ๋ฆฌ๊ฑฐ๊ฐ ๋ฌด์์ธ๊ฐ๋ฅผ ์ ์ด์ค์ผํ๋ค.
// '/list'๋ผ๋ ์์ฒญ์ด์ค๋ฉด ์ด ๋ฉ์๋๊ฐ ์๋, GET๋ฐฉ์๋ง ๋ฐ๋๋ก ๋ฉ์๋์์ฑ๋ ์ ์ด์ค๋ค.
@RequestMapping(value="/list", method=RequestMethod.GET)
public String list (
/*
* ๊ฐ์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ
* 1. "HttpServletReqeust req" ์ ์ธ
* 2. "@RequestParam("aaa") String aaa, @RequestParam("bbb") String bbb" ์ ์ธ
* 3. ์ปค๋งจ๋ ๊ฐ์ฒด ์ด์ฉ : ๊ฐ์ ธ์ฌ ์ปค๋งจ๋ ๊ฐ์ฒด "Box b" ์ ์ธ,
* Model model ์ ์ธ (๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ค์ ๋ด์ ์ ์๋ ๊ณต๊ฐ์ผ๋ก viewํ๋ฉด์ ๋ฟ๋ ค์ฃผ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.)
* */
Model model
) throws Exception {
logger.info("/board/list : GET ์์ฒญ ๋ฐ์!");
System.out.println("๊ฒ์๊ธ ํ์ด์ง ์ด๋ ์์ฒญ!");
//1. HttpServletReqeust req ๋ฐฉ๋ฒ
// System.out.println("aaa : " + req.getParameter("aaa"));
// System.out.println("bbb : " + req.getParameter("bbb"));
//2. @RequestParam ๋ฐฉ๋ฒ
// System.out.println("aaa : " + aaa);
// System.out.println("bbb : " + bbb);
//3. ์ปค๋งจ๋ ๊ฐ์ฒด ๋ฐฉ๋ฒ
// System.out.println("aaa : " + b.getAaa());
// System.out.println("bbb : " + b.getBbb());
//๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ๊ณผ์ - view์์๋ ""์ ์ ์ ์ด๋ฆ์ ELํ๊ทธ๋ฅผ ์ด์ฉํด ์ฌ์ฉํ๋ค.
// model.addAttribute("aaaa", b.getAaa());
// model.addAttribute("bbbb", b.getBbb());
//์ฝ์์ฐฝ์ ๊ฒ์๊ธ ๋ชฉ๋ก ๋์ฐ๊ธฐ
List<BoardVO> articles = service.getAllArticles();
System.out.println("=====================");
for(BoardVO vo : articles)
System.out.println(vo);
System.out.println("=====================");
//DB์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ articles๋ผ๋ ์ด๋ฆ์ ๋ชจ๋ธ์ ๋ด๋๋ค.
model.addAttribute("articles", service.getAllArticles());
return "board/list";
}
//๊ฒ์๊ธ ์์ฑํ๋ฉด ์ด๋์์ฒญ
@RequestMapping(value="/write", method=RequestMethod.GET)
public String write() {
logger.info("/board/write : GET ์์ฒญ ๋ฐ์!");
return "board/write";
}
//์์ธ ํ์ด์ง ํ๋ฉด ์ด๋ ์์ฒญ
@RequestMapping(value="/content", method=RequestMethod.GET)
public String content(@RequestParam("boardNo") int boardNo, Model model) throws Exception {
//๊ฒ์๋ฌผ์ ๋ฒํธ๋ฅผ requestparam์ผ๋ก ๊ฐ์ ธ์จ๋ค
logger.info("/board/content : GET ์์ฒญ ๋ฐ์!");
//DB์์ ์ฐ๊ฒฐํด ๊ฐ์ ธ์จ ๊ฒ์๋ฌผ์ article์ด๋ผ๋ ์ด๋ฆ์ ๋ชจ๋ธ์ ๋ด๋๋ค.
model.addAttribute("article", service.getArticle(boardNo));
return "board/content";
}
//๊ฒ์๊ธ ์์ ํ๋ฉด ์ด๋ ์์ฒญ
@RequestMapping(value="/modify", method=RequestMethod.GET)
public String modify(@RequestParam("boardNo") int boardNo, Model model) throws Exception {
logger.info("/board/modify : GET ์์ฒญ ๋ฐ์!");
//requestParam์ผ๋ก ๊ธ๋ฒํธ๋ฅผ ๊ฐ์ ธ์จ๋ค ๊ธ๋ฒํธ๋ก ๊ฒ์๊ธ ์ ์ฒด๋ฅผ ์กฐํํ๊ณ article์ด๋ฆ์ ๋ชจ๋ธ์ ๋ด๋๋ค.
model.addAttribute("article", service.getArticle(boardNo));
return "board/modify";
}
๊ฒ์ํ ๊ธฐ๋ฅ ์์ฒญ POST์์ฒญ
/////////////////// ๊ฒ์ํ ๊ธฐ๋ฅ ์์ฒญ [POST] ///////////////////////
//๊ฒ์๋ฌผ ๋ฑ๋ก ๊ธฐ๋ฅ ๋ฉ์๋ ์์ฒญ (๊ฐ์ ์ด๋ฆ์ผ๋ก ํจ์๋ฅผ ๋ง๋ค์์๋ค. ์ค๋ฒ๋ก๋ฉ)
@RequestMapping(value="/write", method=RequestMethod.POST)
public String write(BoardVO article, RedirectAttributes redirectAttr) throws Exception {
logger.info("/board/write : POST ์์ฒญ !");
logger.info("๊ฐ์ ธ์จ ๊ฒ์๊ธ ํ์ธ : " + article.toString());
service.insert(article); //๊ฒ์๊ธ ๋ฑ๋ก ์์ฒญ
//๊ฒ์๊ธ ๋ฑ๋ก์ ํ๊ณ alert์ผ๋ก ์ํ๋ค~ ๋ผ๊ณ ์๋ ค์ฃผ๊ธฐ ์ํด ๋ฆฌ๋ค์ด๋ ํธ๋ฅผ ํ ๋ ์์ ๋ฐ์ดํฐ(๊ทธ ์๊ฐ๋ง)๋ฅผ ์๋ ๋ฐฉ๋ฒ
redirectAttr.addFlashAttribute("message", "regSuccess");
//list.jsp ์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ฌ์ฉ
/*
* ๊ฒ์๊ธ ๋ฑ๋ก์ด ๋๋๋ฉด ๋ค์ ๊ฒ์๊ธ ๋ชฉ๋ก์ด ๋์์ผํ๋ค. ์ด๋ ๊ทธ๋ฅ jspํ์ผ์ ๋ถ๋ฌ์ค๋ฉด ์๋๊ณ
* ๊ฒ์๊ธ ๋ชฉ๋ก์ ํญ์ DB๋ฅผ ๊ฑฐ์ณ์ ๋ชฉ๋ก์ ์๋ก ๊ฐ์ ธ์์ผํ๋ค. -> ๋ค์ ํ ๋ฒ ๋ชฉ๋ก ์ฌ์์ฒญ์ ํด์ผํจ
* ์ฌ์์ฒญ์ด ์์ ๋๋ redirect:/
*/
return "redirect:/board/list";
}
//๊ฒ์๋ฌผ ์์ ๊ธฐ๋ฅ ๋ฉ์๋ ์์ฒญ
@RequestMapping(value="/modify", method=RequestMethod.POST)
public String modify(BoardVO article, RedirectAttributes redirectAttr) throws Exception {
logger.info("/board/modify : POST ์์ฒญ !");
service.update(article); //๊ฒ์๊ธ ์์ ์์ฒญ
redirectAttr.addFlashAttribute("message", "modSuccess"); //์์ ์ฑ๊ณต ์ ์์๋ฐ์ดํฐ
return "redirect:/board/content?boardNo="+article.getBoardNo();
}
//๊ฒ์๋ฌผ ์ญ์ ๊ธฐ๋ฅ ๋ฉ์๋ ์์ฒญ
@RequestMapping(value="/delete", method=RequestMethod.POST)
public String delete(@RequestParam("boardNo") int boardNo, RedirectAttributes redirectAttr) throws Exception {
logger.info("/board/delete : POST ์์ฒญ !");
service.delete(boardNo);
redirectAttr.addFlashAttribute("message", "delSuccess"); //์ญ์ ์ฑ๊ณต ์ ์์ ๋ฐ์ดํฐ
return "redirect:/board/list";
}
viewํ๋ฉด - list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<jsp:include page="../include/static-head.jsp"/>
<body>
<div class="container-fluid">
<jsp:include page="../include/menu-bar.jsp"/>
<div class="container" style="margin-top: 70px;">
<div class="wrap_title">
<h2>๊ฒ์ํ<small>๋ชฉ๋กํ์ด์ง</small></h2>
<%-- ๊ฒ์๋ฌผ ๋ณด์ด๋ ๊ฐ์ ๋ฐ๊พธ๊ธฐ --%>
<span id="count-per-page" style="float: right;">
<i class="fa fa-list">๋ชฉ๋ก๋ณด๊ธฐ</i>
<input class="btn" type="button" value="10">
<input class="btn" type="button" value="20">
<input class="btn" type="button" value="30">
</span>
</div>
<%-- ๊ฒ์๋ฌผ ์์ ๋ถ๋ถ --%>
<div class="list_content">
<table class="table table-hover">
<thead>
<tr>
<th width="10%">๋ฒ ํธ </th>
<th width="50%">์ ๋ชฉ</th>
<th width="30%">๊ธ์ด์ด</th>
<th width="10%">์กฐํ</th>
</tr>
</thead>
<%-- ๊ฒ์๋ฌผ์ด ๋ค์ด๊ฐ ๊ณต๊ฐ:์ปจํธ๋กค๋ฌ์์ articles๋ผ๋ ์ด๋ฆ์ ๋ชจ๋ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์๊ธฐ ๋๋ฌธ์ elํ๊ทธ๋ก ์ฌ์ฉํ ์ ์๋ค. --%>
<c:if test="${articles.size() <=0 }">
<!-- ๋ชจ๋ธ์ ๋ด์ articles ํฌ๊ธฐ๊ฐ 0๋ณด๋ค ์์ผ๋ฉด ๋ํ๋๋ ๊ณณ -->
<td colspan="4" align="center"><strong>๋ฑ๋ก๋ ๊ฒ์๋ฌผ์ด ์์ต๋๋ค !</strong></td>
</c:if>
<c:if test="${articles.size() > 0 }">
<!-- ๊ฒ์๋ฌผ ์๊ฐ ์กด์ฌํ ๋ ๋ณด์ฌ์ง๋ ๊ณณ -->
<!-- articles(listํ์
)๋ฅผ ๊บผ๋ด์ article(BoardVOํ์
)์ ํ๋์ฉ ๋ด๋๋ค. -->
<c:forEach var="article" items="${articles }">
<tr>
<td>${article.boardNo }</td>
<!-- ๊ธ๋ฒํธ๋ก ๊ฒ์๊ธ์ ์กฐํํด์ผํ๋๊น ๊ธ๋ฒํธ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฌ์์ค๋ค. -->
<td><a href="<c:url value='/board/content?boardNo=${article.boardNo }' />"> ${article.title }</a></td>
<td>${article.writer }</td>
<td>${article.viewCnt }</td>
</tr>
</c:forEach>
</c:if>
</table>
</div>
<div class="list_paging">
</div>
<button type="button" class="btn btn-success btn-flat" id="writeBtn">
<i class="fa fa-pencil"></i> ๊ธ์ฐ๊ธฐ
</button>
</div>
<jsp:include page="../include/footer.jsp"/>
</div>
<script type="text/javascript">
const result = "${message}"; //์์์ฒ๋ฆฌ
if(result === "regSuccess") {//๋๋ฑ๋น๊ต ===์ฌ์ฉ
alert("๊ฒ์๊ธ ๋ฑ๋ก์ด ์๋ฃ๋์์ต๋๋ค.");
} else if (result === "delSuccess") {
alert("๊ฒ์๊ธ ์ญ์ ๊ฐ ์๋ฃ๋์์ต๋๋ค.");
}
$(document).ready(function() {
//๊ธ์ฐ๊ธฐ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
$("#writeBtn").on("click", function(){
self.location = "/mvc/board/write";
});
});
</script>
</body>
</html>
viewํ๋ฉด - write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<jsp:include page="../include/static-head.jsp"/>
<body>
<div class="container-fluid">
<jsp:include page="../include/menu-bar.jsp"/>
<div class="container" style="margin-top: 70px;">
<div class="wrap_title">
<h2>๊ฒ์ํ<small>์์ฑํ์ด์ง</small></h2>
</div>
<form id="writeForm" method="post" action="<c:url value='/board/write' />">
<div class="content_title form-group">
<label for="title">๊ธ์ ๋ชฉ</label>
<input class="form-control" id="title" name="title" placeholder="์ ๋ชฉ์ ์
๋ ฅํด์ฃผ์ธ์" value="">
</div>
<div class="content_content form-group">
<label for="content">๋ด์ฉ</label>
<textarea class="form-control" id="content" name="content" rows="10" placeholder="๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์" style="resize: none;">๊ธ๋ด์ฉ</textarea>
</div>
<div class="form-group">
<label for="writer">์์ฑ์</label>
<input class="form-control" id="writer" name="writer" value="์์ฑ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.">
</div>
<div class="list_paging">
</div>
<button type="button" class="btn btn-primary"><i class="fa fa-list"></i> ๋ชฉ๋ก</button>
<button type="reset" class="btn btn-warning"><i class="fa fa-reply"></i> ์ด๊ธฐํ</button>
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> ์
๋ ฅ</button>
</form>
</div>
<jsp:include page="../include/footer.jsp"/>
</div>
</body>
</html>
viewํ๋ฉด - content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<jsp:include page="../include/static-head.jsp"/>
<body>
<div class="container-fluid">
<jsp:include page="../include/menu-bar.jsp"/>
<div class="container" style="margin-top: 70px;">
<div class="wrap_title">
<h2>๊ฒ์ํ<small>์์ธํ์ด์ง</small></h2>
</div>
<div class="content_title">
<strong>${article.title }</strong>
<span>
<!-- fmt๋ผ์ด๋ธ๋ฌ๋ฆฌ : ๋ ์ง ํ์ ์ ๊ณต - ์์ชฝ์ ํ๊ทธ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ์ธํด์ผํ๋ค. -->
<fmt:formatDate pattern="yyyy-MM-dd a HH:mm" value="${article.regDate }"/>
</span>
</div>
<div class="content_content" style="height: 350px;">
${article.content }
</div>
<div>
<span class="username">${article.writer }</span>
</div>
<form role="form" method="post">
<input type="hidden" name="boardNo" value="${article.boardNo }">
</form>
<button class="btn btn-primary listBtn">
<i class="fa fa-list"></i> ๋ชฉ๋ก
</button>
<button class="btn btn-warning modBtn">
<i class="fa fa-edit"></i> ์์
</button>
<button class="btn btn-danger delBtn">
<i class="fa fa-trash"></i> ์ญ์
</button>
</div>
<jsp:include page="../include/footer.jsp"/>
</div>
<script type="text/javascript">
$(document).ready(function () {
const formObj = $("form[role='form']");
$(".listBtn").on("click", function() {
formObj.attr("method", "get");
formObj.attr("action", "list");
formObj.submit();
});
$(".modBtn").on("click", function() {
formObj.attr("method", "get");
formObj.attr("action", "modify");
formObj.submit();
});
$(".delBtn").on("click", function() {
formObj.attr("method", "post");
formObj.attr("action", "delete");
formObj.submit();
});
});
</script>
<script type="text/javascript">
const result = "${message}"; //์์์ฒ๋ฆฌ
if(result === "modSuccess") {//๋๋ฑ๋น๊ต ===์ฌ์ฉ
alert("๊ฒ์๊ธ ์์ ์ด ์๋ฃ๋์์ต๋๋ค.");
}
</script>
</body>
</html>
viewํ๋ฉด - modify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<jsp:include page="../include/static-head.jsp"/>
<body>
<div class="container-fluid">
<jsp:include page="../include/menu-bar.jsp"/>
<div class="container" style="margin-top: 70px;">
<div class="wrap_title">
<h2>๊ฒ์ํ<small>์์ ํ์ด์ง</small></h2>
</div>
<form role="form" id="modifyForm" method="post" action="<c:url value='/board/modify'/>">
<input type="hidden" id="boardNo" name="boardNo" value="${article.boardNo }">
<div class="content_title form-group">
<label for="title">๊ธ์ ๋ชฉ</label>
<input class="form-control" id="title" name="title" placeholder="์ ๋ชฉ์ ์
๋ ฅํด์ฃผ์ธ์" value="${article.title}">
</div>
<div class="content_content form-group">
<label for="content">๋ด์ฉ</label>
<textarea class="form-control" id="content" name="content" rows="10" placeholder="๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์" style="resize: none;">${article.content }</textarea>
</div>
<div class="form-group">
<label for="writer">์์ฑ์</label>
<input class="form-control" id="writer" name="writer" value="${article.writer}" readonly>
</div>
<div class="list_paging">
</div>
<button type="button" class="btn btn-primary listBtn"><i class="fa fa-list"></i> ๋ชฉ๋ก</button>
<button type="button" class="btn btn-warning cancelBtn"><i class="fa fa-trash"></i> ์ทจ์</button>
<button type="submit" class="btn btn-success modBtn"><i class="fa fa-save"></i> ์์ ์ ์ฅ</button>
</form>
</div>
<jsp:include page="../include/footer.jsp"/>
</div>
<script>
$(document).ready(function () {
const formObj = $("form[role='form']");
$(".modBtn").on("click", function () {
formObj.submit();
});
$(".cancelBtn").on("click", function () {
history.back();
});
$(".listBtn").on("click", function () {
self.location = "list";
});
});
</script>
</body>
</html>
๋ฐ์ํ
'Project > SpringMVC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๊ธ
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- ๊ฒ์ํ ์ญ์
- Algorithm
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- ์๋ฐ
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ๊ฒ์๋ฌผ ์ญ์
- mysql์ค์น
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ์จ๋ฆฌ์์ค
- Java
- typeAliases
- ๊ฐ๋ฐ
- ๋ณ๋ช ์ฒ๋ฆฌ
- java ํ๊ฒฝ๋ณ์
- ์๊ณ ๋ฆฌ์ฆ
- ์๋ฃ๊ตฌ์กฐ
- ์ดํด๋ฆฝ์ค ์ค์น
- ๊ฒ์ํ ์กฐํ
- tomcat์ค์น
- java jdk ์ค์น
- ๊ฒ์๋ฌผ์กฐํ
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ๋ถํธ ์๋์์ฑ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
๊ธ ๋ณด๊ดํจ