티스토리 뷰
| 게시판
게시판 제작에 필요한 간단한 구조를 정리해보자.
|| Dto
/src/main/java/com/cristoval/web/dto/NoticeDto.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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class NoticeDto { private int no; private String id; private String title; private String content; private String regtime; public NoticeDto() {}; public NoticeDto(int no, String id, String title, String content, String regtime) { this.no = no; this.id = id; this.title = title; this.content = content; this.regtime = regtime; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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 getRegtime() { return regtime; } public void setRegtime(String regtime) { this.regtime = regtime; } @Override public String toString() { return "NoticeDto [no=" + no + ", id=" + id + ", title=" + title + ", content=" + content + ", regtime=" + regtime + "]"; } } | cs |
|| Controller
/src/main/java/com/cristoval/web/notice/controller/NoticeController.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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | @Controller @RequestMapping("/notice") public class NoticeController { @Autowired private NoticeService noticeService; // 공지사항 목록 @RequestMapping(value = "/list", method = RequestMethod.GET) public String goSearchAll(Model model, @RequestParam Map<String, String> map) { String spp = map.get("spp"); map.put("spp", spp != null ? spp : "10");// sizePerPage try { List<NoticeDto> list = noticeService.list(map); PageNavigation pageNavigation = noticeService.makePageNavigation(map); model.addAttribute("list", list); model.addAttribute("navigation", pageNavigation); } catch (Exception e) { e.printStackTrace(); System.out.println("에러발생"); } return "notice/list"; } // 글작성 페이지로 이동 @RequestMapping(value = "/mvwrite", method = RequestMethod.GET) public String goWrite() { return "notice/write"; } // 글작성 @RequestMapping(value = "/write", method = RequestMethod.POST) public String doWrite(Model model, HttpServletRequest request) { HttpSession session = request.getSession(); UserDto memberDto = (UserDto) session.getAttribute("userDto"); NoticeDto noticeDto = new NoticeDto(); if (memberDto != null) { noticeDto.setId(request.getParameter("id")); noticeDto.setTitle(request.getParameter("title")); noticeDto.setContent(request.getParameter("content")); System.out.println(noticeDto); try { noticeService.write(noticeDto); } catch (Exception e) { e.printStackTrace(); System.out.println("에러발생"); } } else { System.out.println("에러발생"); } return "/notice/writesuccess"; } // 글 보기 @RequestMapping(value = "/show", method = RequestMethod.GET) public String show(Model model, HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); HttpSession session = request.getSession(); NoticeDto noticeDto; try { noticeDto = noticeService.show(no); session.setAttribute("list", noticeDto); model.addAttribute("list", noticeDto); } catch (Exception e) { e.printStackTrace(); System.out.println("처리중 에러가 발생"); return "error/error"; } return "/notice/show"; } // 글수정 페이지로 이동 @RequestMapping(value = "/mvmodify", method = RequestMethod.GET) public String moveModify(HttpServletRequest request) { HttpSession session = request.getSession(); UserDto memberDto = (UserDto) session.getAttribute("userDto"); session.setAttribute("userDto", memberDto); return "/notice/modify"; } // 글 수정 @RequestMapping(value = "/modify", method = RequestMethod.POST) public String modifyInfo(HttpServletRequest request) { HttpSession session = request.getSession(); NoticeDto noticeDto = new NoticeDto(); UserDto memberDto = (UserDto) session.getAttribute("userDto"); noticeDto.setId(memberDto.getId()); noticeDto.setTitle(request.getParameter("title")); noticeDto.setContent(request.getParameter("content")); noticeDto.setNo(Integer.parseInt(request.getParameter("no"))); noticeDto.setRegtime(request.getParameter("regtime")); System.out.println(noticeDto); try { noticeService.modifyInfo(noticeDto); noticeDto = noticeService.getInfo(noticeDto.getNo()); session.setAttribute("list", noticeDto); } catch (Exception e) { e.printStackTrace(); request.setAttribute("msg", "회원정보 수정 중 문제가 발생했습니다."); return "error/error"; } return "notice/show"; } // 글 삭제 @RequestMapping(value = "/delete", method = RequestMethod.GET) public String delete(HttpServletRequest request) { int no = Integer.parseInt(request.getParameter("no")); try { noticeService.delete(no); HttpSession session = request.getSession(); session.removeAttribute("list"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("msg", "공지사항 삭제 중 문제가 발생했습니다."); } return "notice/deletesuccess"; } } | cs |
|| Mapper(Repo)
/src/main/java/com/cristoval/web/repository/NoticeRepository.java
1 2 3 4 5 6 7 8 | public interface NoticeRepository { public List<NoticeDto> list(Map<String, Object> param) throws SQLException; public int getTotalCount(Map<String, String> map) throws SQLException; public void write(NoticeDto noticeDto) throws SQLException; public void modifyInfo(NoticeDto noticeDto) throws SQLException; public void delete(int no) throws SQLException; public NoticeDto show(int no) throws SQLException; } | cs |
/src/main/resources/mapper/notice.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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cristoval.web.repository.NoticeRepository"> <select id="list" parameterType="map" resultType="NoticeDto"> select no, id, title, content, regtime from notice order by no desc limit #{start}, #{spp} </select> <select id="show" parameterType="int" resultType="NoticeDto"> select no, id, title, content, regtime from notice where no = #{no} </select> <insert id="write" parameterType="NoticeDto"> insert into notice (id, title, content, regtime) values (#{id}, #{title}, #{content}, now()) </insert> <update id="modifyInfo" parameterType="NoticeDto"> update notice set title = #{title}, content = #{content}, regtime = now() where no = #{no} </update> <delete id="delete" parameterType="int"> delete from notice where no = #{no} </delete> <select id="getTotalCount" parameterType="map" resultType="int"> select count(no) from notice <if test="word != null and word != ''"> <if test="key == 'subject'"> where subject like concat('%', #{word}, '%') </if> <if test="key != 'subject'"> where #{key} = #{word} </if> </if> </select> </mapper> | cs |
|| Service
/src/main/java/com/cristoval/web/service/NoticeService.java
1 2 3 4 5 6 7 8 9 10 | public interface NoticeService { public PageNavigation makePageNavigation(Map<String, String> map) throws Exception; public List<NoticeDto> list(Map<String, String> map) throws Exception; public void write(NoticeDto noticeDto) throws Exception; public NoticeDto show(int no) throws SQLException; public void modifyInfo(NoticeDto noticeDto) throws SQLException; public void delete(int no) throws SQLException; public NoticeDto getInfo(int no) throws SQLException; } | cs |
/src/main/java/com/cristoval/web/service/NoticeServiceImpl.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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | @Service public class NoticeServiceImpl implements NoticeService { @Autowired private NoticeRepository noticeReop; @Override public void write(NoticeDto noticeDto) throws Exception { if (noticeDto.getTitle() == null || noticeDto.getContent() == null) throw new Exception(); noticeReop.write(noticeDto); } @Override public NoticeDto show(int no) throws SQLException { return noticeReop.show(no); } @Override public NoticeDto getInfo(int no) throws SQLException { return noticeReop.show(no); } @Override public void modifyInfo(NoticeDto noticeDto) throws SQLException { noticeReop.modifyInfo(noticeDto); } @Override public void delete(int no) throws SQLException { noticeReop.delete(no); } public PageNavigation makePageNavigation(Map<String, String> map) throws Exception { int naviSize = 10; int currentPage = Integer.parseInt(map.get("pg")); // 현재 페이지 번호 int sizePerPage = Integer.parseInt(map.get("spp")); // 페이지 글 갯수 PageNavigation pageNavigation = new PageNavigation(); pageNavigation.setCurrentPage(currentPage); pageNavigation.setNaviSize(naviSize); int totalCount = noticeReop.getTotalCount(map); pageNavigation.setTotalCount(totalCount); int totalPageCount = (totalCount - 1) / sizePerPage + 1; pageNavigation.setTotalPageCount(totalPageCount); boolean startRange = currentPage <= naviSize; pageNavigation.setStartRange(startRange); boolean endRange = (totalPageCount - 1) / naviSize * naviSize < currentPage; pageNavigation.setEndRange(endRange); pageNavigation.makeNavigator(); return pageNavigation; } @Override public List<NoticeDto> list(Map<String, String> map) throws Exception { Map<String, Object> param = new HashMap<String, Object>(); param.put("key", map.get("key") == null ? "" : map.get("key")); param.put("word", map.get("word") == null ? "" : map.get("word")); int currentPage = Integer.parseInt(map.get("pg")); int sizePerPage = Integer.parseInt(map.get("spp")); int start = (currentPage - 1) * sizePerPage; param.put("start", start); param.put("spp", sizePerPage); return noticeReop.list(param); } } | cs |
|| Pagging
/src/main/java/com/cristoval/web/util/PageNavigation.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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | public class PageNavigation { private boolean startRange; private boolean endRange; private int totalCount; private int newCount; private int totalPageCount; private int currentPage; private int naviSize; private int countPerPage; private String navigator; public boolean isStartRange() { return startRange; } public void setStartRange(boolean startRange) { this.startRange = startRange; } public boolean isEndRange() { return endRange; } public void setEndRange(boolean endRange) { this.endRange = endRange; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getNewCount() { return newCount; } public void setNewCount(int newCount) { this.newCount = newCount; } public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getNaviSize() { return naviSize; } public void setNaviSize(int naviSize) { this.naviSize = naviSize; } public String getNavigator() { return navigator; } public int getCountPerPage() { return countPerPage; } public void setCountPerPage(int countPerPage) { this.countPerPage = countPerPage; } public void makeNavigator() { int startPage = (currentPage - 1) / naviSize * naviSize + 1; int endPage = startPage + naviSize - 1; if(totalPageCount < endPage) endPage = totalPageCount; StringBuilder buffer = new StringBuilder(); buffer.append(" <ul class=\"pagination\"> \n"); buffer.append(" <li class=\"page-item\" data-pg=\"1\"> \n"); buffer.append(" <a href=\"#\" class=\"page-link\">최신</a> \n"); buffer.append(" </li> \n"); buffer.append(" <li class=\"page-item\" data-pg=\"" + (this.startRange ? 1 : (startPage - 1)) + "\"> \n"); buffer.append(" <a href=\"#\" class=\"page-link\">이전</a> \n"); buffer.append(" </li> \n"); for(int i=startPage;i<=endPage;i++) { buffer.append(" <li class=\"" + (currentPage == i ? "page-item active" : "page-item") + "\" data-pg=\"" + i + "\"><a href=\"#\" class=\"page-link\">" + i + "</a></li> \n"); } buffer.append(" <li class=\"page-item\" data-pg=\"" + (this.endRange ? endPage : (endPage + 1)) + "\"> \n"); buffer.append(" <a href=\"#\" class=\"page-link\">다음</a> \n"); buffer.append(" </li> \n"); buffer.append(" <li class=\"page-item\" data-pg=\"" + totalPageCount + "\"> \n"); buffer.append(" <a href=\"#\" class=\"page-link\">마지막</a> \n"); buffer.append(" </li> \n"); buffer.append(" </ul> \n"); this.navigator = buffer.toString(); } } | cs |
|| Filter Encoding
/src/main/java/com/cristoval/web/FilterConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Configuration public class FilterConfig { @Bean public FilterRegistrationBean encodingFilterBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setForceEncoding(true); filter.setEncoding("UTF-8"); registrationBean.setFilter(filter); return registrationBean; } } | cs |
|| View
||| List
/src/main/webapp/WEB-INF/views/notice/list.jsp
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <script type="text/javascript"> function movewrite() { location.href = "${root}/notice/mvwrite"; } function pageMove(pg) { document.getElementById("pg").value = pg; document.getElementById("pageform").action = "${root}/notice/list?pg=" + pg; document.getElementById("pageform").submit(); } </script> <body> <form name="pageform" id="pageform" method="GET" action=""> <input type="hidden" name="act" id="act" value="notice"> <input type="hidden" name="pg" id="pg" value=""> </form> <div class="container" align="center"> <br> <h2 style="font-weight: bold;">공지사항</h2> <table class="table table-borderless"> <c:if test="${userDto != null}"> <tr> <td align="right"><button type="button" class="btn btn-info" onclick="javascript:movewrite();">글쓰기</button></td> </tr> </c:if> </table> <form id="searchform" method="get" class="form-inline" action=""> <input type="hidden" name="act" id="act" value="notice"> <input type="hidden" name="pg" id="pg" value="1"> <table class="table table-active"> <thead> <tr class="table-primary"> <th scope="col" colspan="4">번호</th> <th scope="col" colspan="4">제목</th> <th scope="col" colspan="4">작성자</th> <th scope="col" colspan="4">작성일</th> </tr> </thead> <c:if test="${list.size() != 0}"> <tbody> <c:forEach var="lists" items="${list}"> <tr style="background-color: white;" onmouseover="this.style.background='#e6ecff'" onmouseout="this.style.background='white'"> <td colspan="4">${lists.no}</td> <td colspan="4"><a href="${root}/notice/show?no=${lists.no}"><strong>${lists.title}</strong></a></td> <td colspan="4">${lists.id}</td> <td colspan="4">${lists.regtime}</td> </tr> </c:forEach> <tbody> </c:if> <c:if test="${list.size() == 0}"> <table class="table table-active"> <tbody> <tr class="table-info" align="center"> <td>작성된 글이 없습니다.</td> </tr> </tbody> </table> </c:if> </table> </form> <table> <tr> <td>${navigation.navigator}</td> </tr> </table> </div> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
||| Show
/src/main/webapp/WEB-INF/views/notice/show.jsp
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <style type="text/css"> .blue { background: #3379FE; } .red { background: #FF0000; } .green { background: #008000; } .button { color: #ffffff; font-size: 1.2em; padding: 0.3em 0.5em; margin-right: 0.1em; } </style> <body> <section id="blog" class="blog"> <div class="container aos-init aos-animate" data-aos="fade-up"> <div class="row"> <div class="col-lg-2"></div> <div class="col-lg-8 entries"> <article class="entry"> <h2 class="entry-title"> <a>${list.title}</a> </h2> <div class="entry-meta"> <ul> <li class="d-flex align-items-center"><i class="icofont-user"></i>${list.id}</li> <li class="d-flex align-items-center"><i class="icofont-wall-clock"></i>${list.regtime}</li> <li class="d-flex align-items-center"><i class="icofont-comment"></i> notice ${list.no}</li> </ul> </div> <div class="entry-content"> <br> <p>${list.content}</p> </div> </article> </div> <div class="col-lg-4"></div> </div> <div class="form-group" align="center"> <c:if test="${userDto != null}"> <a href="${root}/notice/mvmodify?no=${list.no}" class="button blue">수정</a> <a href="${root}/notice/delete?no=${list.no}" class="button red">삭제</a> </c:if> <a href="${root}/notice/list?pg=1" class="button green">목록</a> </div> </div> </section> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
||| Write
/src/main/webapp/WEB-INF/views/notice/write.jsp
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 39 40 41 42 43 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <script type="text/javascript"> function writeNotice() { if (document.getElementById("title").value == "") { alert("제목을 입력해주세요."); return; } else if (document.getElementById("content").value == "") { alert("내용을 입력해주세요."); return; } else { document.getElementById("writeform").action = "${root}/notice/write"; document.getElementById("writeform").submit(); } } </script> </head> <body> <div class="container" align="center"> <br> <h2 style="font-weight: bold;">공지사항 작성</h2> <form id="writeform" method="post" action=""> <input type="hidden" name="act" id="act" value="write"> <div class="form-group" align="left"> <input type="hidden" name="id" id="id" value="${userDto.id}"> </div> <div class="form-group" align="left"> <label for="subject">제목:</label> <input type="text" class="form-control" id="title" name="title"> </div> <div class="form-group" align="left"> <label for="content">내용:</label> <textarea class="form-control" rows="15" id="content" name="content"></textarea> </div> <button type="button" class="btn btn-primary" onclick="javascript:writeNotice();">작성</button> <button type="reset" class="btn btn-warning">초기화</button> </form> </div> <br/><br/> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
/src/main/webapp/WEB-INF/views/notice/writesuccess.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <style type="text/css"> .button { color: #ffffff; background: #008000; font-size: 1.2em; padding: 0.3em 0.5em; margin-right: 0.1em; } </style> <body> <br> <div align="center"> <br> <h2 style="font-weight: bold;">공지사항이 성공적으로 작성되었습니다.</h2> <br> <a href="${root}/notice/list?pg=1" class="button">목록으로</a> </div> <br><br> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
||| Delete
/src/main/webapp/WEB-INF/views/notice/deletesuccess.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <style type="text/css"> .button { color: #ffffff; background: #008000; font-size: 1.2em; padding: 0.3em 0.5em; margin-right: 0.1em; } </style> <body> <br> <div align="center"> <br> <h2 style="font-weight: bold;">공지사항이 성공적으로 삭제되었습니다.</h2> <br> <a href="${root}/notice/list?pg=1" class="button">목록으로</a> </div> <br><br> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
||| Modify
/src/main/webapp/WEB-INF/views/notice/modify.jsp
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 39 40 41 42 43 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/WEB-INF/views/include/header.jsp"%> <script type="text/javascript"> function writeNotice() { if (document.getElementById("title").value == "") { alert("제목을 입력해주세요."); return; } else if (document.getElementById("content").value == "") { alert("내용을 입력해주세요."); return; } else { document.getElementById("writeform").action = "${root}/notice/modify"; document.getElementById("writeform").submit(); alert("수정이 완료되었습니다."); } } </script> </head> <body> <div class="container" align="center"> <br> <h2 style="font-weight: bold;">공지사항 수정</h2> <form id="writeform" method="post" action=""> <input type="hidden" name="act" id="act" value="modify"> <input type="hidden" name="no" id="no" value="${list.no}"> <input type="hidden" name="regtime" id="regtime" value="${list.regtime}"> <div class="form-group" align="left"> <label for="subject">제목:</label> <input type="text" class="form-control" id="title" name="title" value="${list.title}"> </div> <div class="form-group" align="left"> <label for="content">내용:</label> <textarea class="form-control" rows="15" id="content" name="content">${list.content}</textarea> </div> <button type="button" class="btn btn-primary" onclick="javascript:writeNotice();">수정</button> <button type="reset" class="btn btn-warning">초기화</button> </form> </div> <br/><br/> </body> <%@ include file="/WEB-INF/views/include/footer.jsp"%> | cs |
'Web > Spring' 카테고리의 다른 글
Local에 있는 SpringBoot(maven, gradle) project import하기(STS, Eclipse) (0) | 2021.04.26 |
---|---|
[Spring] File Upload, Download (파일 업로드, 다운로드 기능 만들기) (4) | 2020.12.02 |
[Spring-Boot] Spring, Spring-Boot 기본 개념 정리 (1) | 2020.11.08 |
[Spring-Boot] Spring-Boot Project A to Z (Lombok) (0) | 2020.10.31 |
[Spring-Boot] Lombok을 사용해보자. (0) | 2020.10.29 |