-
[Spring] 게시판 만들기(18) - 계층형 게시판(답글쓰기) 적용Spring/4.3.x - 게시판 만들기 2018. 12. 19. 13:06반응형
계층형 게시판
계층형 게시판이라는 것은 하나의 게시글에 대해 답글 쓰면 해당 게시글의 하위로 답글이 위치되어 계층적인 구조를 이루면서 목록을 구성해 주는 게시판입니다.
1. 쿼리 추가 및 수정
1_1) 게시글 등록(insertBoard) 쿼리 변경
계층형 게시판을 위해서는 게시글 간에 그룹을 만들어야 하기 때문에 BOARD_RE_REF 컬럼에 값을 입력하도록 변경해야 합니다.
더보기123456789101112131415161718192021222324252627282930313233343536373839<insert id="insertBoard" parameterType="com.spring.board.form.BoardForm"><selectKey resultType="int" keyProperty="board_re_ref" order="BEFORE">SELECT IFNULL(MAX(BOARD_RE_REF), 0) + 1FROM BOARD.TB_BOARD</selectKey>INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}
, 0
, 0
, #{board_writer}
, #{board_subject}
, #{board_content}
, 'NONMEMBER'
, NOW()
, 'NONMEMBER'
, NOW())</insert>cs 1_2) 게시글 답글 정보 조회(getBoardReplyInfo) 쿼리 추가
게시글 답글을 동록할 때 부모 게시글에 그룹 번호와 글의 깊이 및 순서를 조회하는 쿼리를 추가해야 합니다.
더보기1234567891011<select id="getBoardReplyInfo" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQFROM TB_BOARDWHERE BOARD_SEQ = #{board_parent_seq}</select>cs 1_3) 게시글 답글 순서 수정(updateBoardReSeq) 쿼리 추가
게시글 답글을 등록할 때 기존에 등록된 답글의 순서를 증가시키는 쿼리를 추가해야 합니다.
더보기1234567891011<delete id="updateBoardReSeq" parameterType="com.spring.board.form.BoardForm">UPDATE TB_BOARDSET BOARD_RE_SEQ = BOARD_RE_SEQ + 1, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_RE_REF = #{board_re_ref}AND BOARD_RE_SEQ > #{board_re_seq}</delete>cs 1_4) 게시글 답글 등록(insertBoardReply) 쿼리 추가
게시글 답글을 등록하는 쿼리르 추가해야 합니다.
더보기1234567891011121314151617181920212223242526272829303132<insert id="insertBoardReply" parameterType="com.spring.board.form.BoardForm">INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}, #{board_re_lev} + 1, #{board_re_seq} + 1, #{board_writer}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert>cs 1_5) 게시글 목록 조회(getBoardList) 쿼리 수정
게시글 그룹 번호와 게시글 답글의 깊이를 기준으로 ORDER BY를 변경해야 합니다.
더보기1234567891011121314151617181920212223<select id="getBoardList" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT *FROM ( SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDORDER BY BOARD_RE_REF DESC, BOARD_RE_SEQ ASC) T1LIMIT #{limit} OFFSET #{offset}</select>cs 2. Back-End 파일 수정
2_1) boardMapper.xml더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.spring.board.boardMapper"><select id="getBoardCnt" parameterType="com.spring.board.form.BoardForm" resultType="int">SELECT COUNT(*)FROM BOARD.TB_BOARD</select><select id="getBoardList" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT *FROM ( SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDORDER BY BOARD_RE_REF DESC, BOARD_RE_SEQ ASC) T1LIMIT #{limit} OFFSET #{offset}</select><delete id="updateBoardHits" parameterType="com.spring.board.form.BoardForm">UPDATE BOARD.TB_BOARDSET BOARD_HITS = BOARD_HITS + 1, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_SEQ = #{board_seq}</delete><select id="getBoardDetail" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE, UPD_USER_ID, CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}</select><insert id="insertBoard" parameterType="com.spring.board.form.BoardForm"><selectKey resultType="int" keyProperty="board_re_ref" order="BEFORE">SELECT IFNULL(MAX(BOARD_RE_REF), 0) + 1FROM BOARD.TB_BOARD</selectKey>INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}, 0, 0, #{board_writer}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert><insert id="insertBoardFail" parameterType="com.spring.board.form.BoardForm">INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(0, 0, 0, #{board_writer1}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert><delete id="deleteBoard" parameterType="com.spring.board.form.BoardForm">DELETEFROM BOARD.TB_BOARDWHERE BOARD_SEQ = #{board_seq}</delete><delete id="updateBoard" parameterType="com.spring.board.form.BoardForm">UPDATE BOARD.TB_BOARDSET BOARD_SUBJECT = #{board_subject}, BOARD_CONTENT = #{board_content}, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_SEQ = #{board_seq}</delete><select id="getBoardReplyInfo" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQFROM TB_BOARDWHERE BOARD_SEQ = #{board_parent_seq}</select><delete id="updateBoardReSeq" parameterType="com.spring.board.form.BoardForm">UPDATE TB_BOARDSET BOARD_RE_SEQ = BOARD_RE_SEQ + 1, UPD_USER_ID = 'NONMEMBER', UPD_DATE = NOW()WHERE BOARD_RE_REF = #{board_re_ref}AND BOARD_RE_SEQ > #{board_re_seq}</delete><insert id="insertBoardReply" parameterType="com.spring.board.form.BoardForm">INSERTINTO BOARD.TB_BOARD(BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATE)VALUES(#{board_re_ref}, #{board_re_lev} + 1, #{board_re_seq} + 1, #{board_writer}, #{board_subject}, #{board_content}, 'NONMEMBER', NOW(), 'NONMEMBER', NOW())</insert></mapper>cs
2_2) BoardForm.javaboard_parent_seq를 추하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142package com.spring.board.form;import java.util.Date;public class BoardForm extends CommonForm {int board_seq;int board_parent_seq;int board_re_ref;int board_re_lev;int board_re_seq;String board_writer;String board_subject;String board_content;int board_hits;String del_yn;String ins_user_id;Date ins_date;String upd_user_id;Date upd_date;String search_type;public int getBoard_seq() {return board_seq;}public void setBoard_seq(int board_seq) {this.board_seq = board_seq;}public int getBoard_parent_seq() {return board_parent_seq;}public void setBoard_parent_seq(int board_parent_seq) {this.board_parent_seq = board_parent_seq;}public int getBoard_re_ref() {return board_re_ref;}public void setBoard_re_ref(int board_re_ref) {this.board_re_ref = board_re_ref;}public int getBoard_re_lev() {return board_re_lev;}public void setBoard_re_lev(int board_re_lev) {this.board_re_lev = board_re_lev;}public int getBoard_re_seq() {return board_re_seq;}public void setBoard_re_seq(int board_re_seq) {this.board_re_seq = board_re_seq;}public String getBoard_writer() {return board_writer;}public void setBoard_writer(String board_writer) {this.board_writer = board_writer;}public String getBoard_subject() {return board_subject;}public void setBoard_subject(String board_subject) {this.board_subject = board_subject;}public String getBoard_content() {return board_content;}public void setBoard_content(String board_content) {this.board_content = board_content;}public int getBoard_hits() {return board_hits;}public void setBoard_hits(int board_hits) {this.board_hits = board_hits;}public String getDel_yn() {return del_yn;}public void setDel_yn(String del_yn) {this.del_yn = del_yn;}public String getIns_user_id() {return ins_user_id;}public void setIns_user_id(String ins_user_id) {this.ins_user_id = ins_user_id;}public Date getIns_date() {return ins_date;}public void setIns_date(Date ins_date) {this.ins_date = ins_date;}public String getUpd_user_id() {return upd_user_id;}public void setUpd_user_id(String upd_user_id) {this.upd_user_id = upd_user_id;}public Date getUpd_date() {return upd_date;}public void setUpd_date(Date upd_date) {this.upd_date = upd_date;}public String getSearch_type() {return search_type;}public void setSearch_type(String search_type) {this.search_type = search_type;}}cs 2_3) BoardDto.java
board_parent_seq를 추하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141package com.spring.board.dto;public class BoardDto extends CommonDto {int board_seq;int board_parent_seq;int board_re_ref;int board_re_lev;int board_re_seq;String board_writer;String board_subject;String board_content;int board_hits;String del_yn;String ins_user_id;String ins_date;String upd_user_id;String upd_date;String result;public int getBoard_seq() {return board_seq;}public void setBoard_seq(int board_seq) {this.board_seq = board_seq;}public int getBoard_parent_seq() {return board_parent_seq;}public void setBoard_parent_seq(int board_parent_seq) {this.board_parent_seq = board_parent_seq;}public int getBoard_re_ref() {return board_re_ref;}public void setBoard_re_ref(int board_re_ref) {this.board_re_ref = board_re_ref;}public int getBoard_re_lev() {return board_re_lev;}public void setBoard_re_lev(int board_re_lev) {this.board_re_lev = board_re_lev;}public int getBoard_re_seq() {return board_re_seq;}public void setBoard_re_seq(int board_re_seq) {this.board_re_seq = board_re_seq;}public String getBoard_writer() {return board_writer;}public void setBoard_writer(String board_writer) {this.board_writer = board_writer;}public String getBoard_subject() {return board_subject;}public void setBoard_subject(String board_subject) {this.board_subject = board_subject;}public String getBoard_content() {return board_content;}public void setBoard_content(String board_content) {this.board_content = board_content;}public int getBoard_hits() {return board_hits;}public void setBoard_hits(int board_hits) {this.board_hits = board_hits;}public String getDel_yn() {return del_yn;}public void setDel_yn(String del_yn) {this.del_yn = del_yn;}public String getIns_user_id() {return ins_user_id;}public void setIns_user_id(String ins_user_id) {this.ins_user_id = ins_user_id;}public String getIns_date() {return ins_date;}public void setIns_date(String ins_date) {this.ins_date = ins_date;}public String getUpd_user_id() {return upd_user_id;}public void setUpd_user_id(String upd_user_id) {this.upd_user_id = upd_user_id;}public String getUpd_date() {return upd_date;}public void setUpd_date(String upd_date) {this.upd_date = upd_date;}public String getResult() {return result;}public void setResult(String result) {this.result = result;}}cs 2_4) boardDao.java
게시글 그룹 번호 조회, 게시글 답글 정보 조회, 게시글 답글 순서 수정, 게시글 답글 등록 메소드를 추가하세요.
더보기12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788package com.spring.board.dao;import java.util.List;import javax.annotation.Resource;import org.apache.ibatis.session.SqlSession;import org.springframework.stereotype.Repository;import com.spring.board.dto.BoardDto;import com.spring.board.form.BoardForm;@Repositorypublic class BoardDao {@Resource(name = "sqlSession")private SqlSession sqlSession;private static final String NAMESPACE = "com.spring.board.boardMapper";/** 게시판 - 목록 수 */public int getBoardCnt(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardCnt", boardForm);}/** 게시판 - 목록 조회 */public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {return sqlSession.selectList(NAMESPACE + ".getBoardList", boardForm);}/** 게시판 - 조회 수 수정 */public int updateBoardHits(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoardHits", boardForm);}/** 게시판 - 상세 조회 */public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardDetail", boardForm);}/** 게시판 - 그룹 번호 조회 */public int getBoardReRef(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardReRef", boardForm);}/** 게시판 - 등록 */public int insertBoard(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoard", boardForm);}/** 게시판 - 등록 실패(트랜잭션 테스트) */public int insertBoardFail(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoardFail", boardForm);}/** 게시판 - 삭제 */public int deleteBoard(BoardForm boardForm) throws Exception {return sqlSession.delete(NAMESPACE + ".deleteBoard", boardForm);}/** 게시판 - 수정 */public int updateBoard(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoard", boardForm);}/** 게시판 - 답글 정보 조회 */public BoardDto getBoardReplyInfo(BoardForm boardForm) throws Exception {return sqlSession.selectOne(NAMESPACE + ".getBoardReplyInfo", boardForm);}/** 게시판 - 답글의 순서 수정 */public int updateBoardReSeq(BoardForm boardForm) throws Exception {return sqlSession.update(NAMESPACE + ".updateBoardReSeq", boardForm);}/** 게시판 - 답글 등록 */public int insertBoardReply(BoardForm boardForm) throws Exception {return sqlSession.insert(NAMESPACE + ".insertBoardReply", boardForm);}}cs 2_5) boardService.java
게시글 그룹 번호를 조회한 후 게시글을 등록할 수 있도록 수정하고, 게시글 답글 메소드를 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161package com.spring.board.service;import java.util.HashMap;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.spring.board.common.PagingUtil;import com.spring.board.common.ResultUtil;import com.spring.board.dao.BoardDao;import com.spring.board.dto.BoardDto;import com.spring.board.dto.CommonDto;import com.spring.board.form.BoardForm;import com.spring.board.form.CommonForm;@Servicepublic class BoardService {@Autowiredprivate BoardDao boardDao;/** 게시판 - 목록 조회 */public ResultUtil getBoardList(BoardForm boardForm) throws Exception {ResultUtil resultUtil = new ResultUtil();CommonDto commonDto = new CommonDto();int totalCount = boardDao.getBoardCnt(boardForm);if (totalCount != 0) {CommonForm commonForm = new CommonForm();commonForm.setFunction_name(boardForm.getFunction_name());commonForm.setCurrent_page_no(boardForm.getCurrent_page_no());commonForm.setCount_per_page(10);commonForm.setCount_per_list(10);commonForm.setTatal_list_count(totalCount);commonDto = PagingUtil.setPageUtil(commonForm);}boardForm.setLimit(commonDto.getLimit());boardForm.setOffset(commonDto.getOffset());List<BoardDto> list = boardDao.getBoardList(boardForm);HashMap<String, Object> resultMap = new HashMap<String, Object>();resultMap.put("list", list);resultMap.put("totalCount", totalCount);resultMap.put("pagination", commonDto.getPagination());resultUtil.setData(resultMap);resultUtil.setState("SUCCESS");return resultUtil;}/** 게시판 - 상세 조회 */public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();String searchType = boardForm.getSearch_type();if ("S".equals(searchType)) {int updateCnt = boardDao.updateBoardHits(boardForm);if (updateCnt > 0) {boardDto = boardDao.getBoardDetail(boardForm);}} else {boardDto = boardDao.getBoardDetail(boardForm);}return boardDto;}/** 게시판 - 등록 */public BoardDto insertBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int insertCnt = 0;int boardReRef = boardDao.getBoardReRef(boardForm);boardForm.setBoard_re_ref(boardReRef);insertCnt = boardDao.insertBoard(boardForm);//insertCnt = boardDao.insertBoardFail(boardForm);if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 삭제 */public BoardDto deleteBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int deleteCnt = boardDao.deleteBoard(boardForm);if (deleteCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 수정 */public BoardDto updateBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int deleteCnt = boardDao.updateBoard(boardForm);if (deleteCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 답글 등록 */public BoardDto insertBoardReply(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();BoardDto boardReplayInfo = boardDao.getBoardReplyInfo(boardForm);boardForm.setBoard_seq(boardReplayInfo.getBoard_seq());boardForm.setBoard_re_lev(boardReplayInfo.getBoard_re_lev());boardForm.setBoard_re_ref(boardReplayInfo.getBoard_re_ref());boardForm.setBoard_re_seq(boardReplayInfo.getBoard_re_seq());int insertCnt = 0;insertCnt += boardDao.updateBoardReSeq(boardForm);insertCnt += boardDao.insertBoardReply(boardForm);if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}}cs
2_6) boardController.java게시글 답글 페이지 이동, 게시글 답글 등록 메소드를 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117package com.spring.board.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.spring.board.common.ResultUtil;import com.spring.board.dto.BoardDto;import com.spring.board.form.BoardForm;import com.spring.board.service.BoardService;@Controller@RequestMapping(value = "/board")public class BoardController {@Autowiredprivate BoardService boardService;/** 게시판 - 목록 페이지 이동 */@RequestMapping( value = "/boardList")public String boardList(HttpServletRequest request, HttpServletResponse response) throws Exception{return "board/boardList";}/** 게시판 - 목록 조회 */@RequestMapping(value = "/getBoardList")@ResponseBodypublic ResultUtil getBoardList(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {ResultUtil resultUtils = boardService.getBoardList(boardForm);return resultUtils;}/** 게시판 - 상세 페이지 이동 */@RequestMapping( value = "/boardDetail")public String boardDetail(HttpServletRequest request, HttpServletResponse response) throws Exception{return "board/boardDetail";}/** 게시판 - 상세 조회 */@RequestMapping(value = "/getBoardDetail")@ResponseBodypublic BoardDto getBoardDetail(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.getBoardDetail(boardForm);return boardDto;}/** 게시판 - 작성 페이지 이동 */@RequestMapping( value = "/boardWrite")public String boardWrite(HttpServletRequest request, HttpServletResponse response) throws Exception{return "board/boardWrite";}/** 게시판 - 등록 */@RequestMapping( value = "/insertBoard")@ResponseBodypublic BoardDto insertBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{BoardDto boardDto = boardService.insertBoard(boardForm);return boardDto;}/** 게시판 - 삭제 */@RequestMapping( value = "/deleteBoard")@ResponseBodypublic BoardDto deleteBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{BoardDto boardDto = boardService.deleteBoard(boardForm);return boardDto;}/** 게시판 - 수정 페이지 이동 */@RequestMapping( value = "/boardUpdate")public String boardUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception{return "board/boardUpdate";}/** 게시판 - 수정 */@RequestMapping( value = "/updateBoard")@ResponseBodypublic BoardDto updateBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{BoardDto boardDto = boardService.updateBoard(boardForm);return boardDto;}/** 게시판 - 답글 페이지 이동 */@RequestMapping(value = "/boardReply")public String boardReply(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardReply";}/** 게시판 - 답글 등록 */@RequestMapping(value = "/insertBoardReply")@ResponseBodypublic BoardDto insertBoardReply(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.insertBoardReply(boardForm);return boardDto;}}cs
3. Front-End 파일 수정 및 추가3_1) 게시글 답글 페이지 추가
답글을 등록할 수 있는 답글 페이지를 추가하세요.
boardReply.jsp
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125<%@ 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="fn" uri="http://java.sun.com/jsp/jstl/functions" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>게시판 답글</title><%String boardSeq = request.getParameter("boardSeq");%><c:set var="boardSeq" value="<%=boardSeq%>"/> <!-- 게시글 번호 --><!-- 공통 CSS --><link rel="stylesheet" type="text/css" href="/css/common/common.css"/><!-- 공통 JavaScript --><script type="text/javascript" src="/js/common/jquery.js"></script><script type="text/javascript">$(document).ready(function(){});/** 게시판 - 목록 페이지 이동 */function goBoardList(){location.href = "/board/boardList";}/** 게시판 - 답글 작성 */function insertBoardReply(){var boardSubject = $("#board_subject").val();var boardContent = $("#board_content").val();if (boardSubject == ""){alert("제목을 입력해주세요.");$("#board_subject").focus();return;}if (boardContent == ""){alert("내용을 입력해주세요.");$("#board_content").focus();return;}var yn = confirm("게시글을 등록하시겠습니까?");if(yn){$.ajax({url : "/board/insertBoardReply",data : $("#boardForm").serialize(),dataType: "JSON",cache : false,async : true,type : "POST",success : function(obj) {insertBoardReplyCallback(obj);},error : function(xhr, status, error) {}});}}/** 게시판 - 작성 콜백 함수 */function insertBoardReplyCallback(obj){if(obj != null){var result = obj.result;if(result == "SUCCESS"){alert("게시글 답글 등록을 성공하였습니다.");goBoardList();} else {alert("게시글 답글 등록을 실패하였습니다.");return;}}}</script></head><body><div id="wrap"><div id="container"><div class="inner"><h2>게시글 작성</h2><form id="boardForm" name="boardForm"><input type="hidden" id="board_parent_seq" name="board_parent_seq" value="${boardSeq}"/> <!-- 부모 게시글 번호 --><table width="100%" class="table02"><caption><strong><span class="t_red">*</span> 표시는 필수입력 항목입니다.</strong></caption><colgroup><col width="20%"><col width="*"></colgroup><tbody id="tbody"><tr><th>제목<span class="t_red">*</span></th><td><input id="board_subject" name="board_subject" value="" class="tbox01"/></td></tr><tr><th>작성자<span class="t_red">*</span></th><td><input id="board_writer" name="board_writer" value="" class="tbox01"/></td></tr><tr><th>내용<span class="t_red">*</span></th><td><textarea id="board_content" name="board_content" cols="10" rows="5" class="textarea01"></textarea></td></tr></tbody></table></form><div class="btn_right mt15"><button type="button" class="btn black mr5" onclick="javascript:goBoardList();">목록으로</button><button type="button" class="btn black" onclick="javascript:insertBoardReply();">등록하기</button></div></div></div></div></body></html>cs 3_2) 게시글 상세 페이지 수정
상세 페이지에서 '답글쓰기' 버튼을 클릭하면 답글 페이지로 이동할 수 있도록 수정하세요.
boardDetail.jsp
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194<%@ 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="fn" uri="http://java.sun.com/jsp/jstl/functions" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>게시판 상세</title><%String boardSeq = request.getParameter("boardSeq");%><c:set var="boardSeq" value="<%=boardSeq%>"/> <!-- 게시글 번호 --><!-- 공통 CSS --><link rel="stylesheet" type="text/css" href="/css/common/common.css"/><!-- 공통 JavaScript --><script type="text/javascript" src="/js/common/jquery.js"></script><script type="text/javascript">$(document).ready(function(){getBoardDetail();});/** 게시판 - 목록 페이지 이동 */function goBoardList(){location.href = "/board/boardList";}/** 게시판 - 수정 페이지 이동 */function goBoardUpdate(){var boardSeq = $("#board_seq").val();location.href = "/board/boardUpdate?boardSeq="+ boardSeq;}/** 게시판 - 답글 페이지 이동 */function goBoardReply(){var boardSeq = $("#board_seq").val();location.href = "/board/boardReply?boardSeq="+ boardSeq;}/** 게시판 - 상세 조회 */function getBoardDetail(boardSeq){var boardSeq = $("#board_seq").val();if(boardSeq != ""){$.ajax({url : "/board/getBoardDetail",data : $("#boardForm").serialize(),dataType : "JSON",cache : false,async : true,type : "POST",success : function(obj) {getBoardDetailCallback(obj);},error : function(xhr, status, error) {}});} else {alert("오류가 발생했습니다.\n관리자에게 문의하세요.");}}/** 게시판 - 상세 조회 콜백 함수 */function getBoardDetailCallback(obj){var str = "";if(obj != null){var boardSeq = obj.board_seq;var boardReRef = obj.board_re_ref;var boardReLev = obj.board_re_lev;var boardReSeq = obj.board_re_seq;var boardWriter = obj.board_writer;var boardSubject = obj.board_subject;var boardContent = obj.board_content;var boardHits = obj.board_hits;var delYn = obj.del_yn;var insUserId = obj.ins_user_id;var insDate = obj.ins_date;var updUserId = obj.upd_user_id;var updDate = obj.upd_date;str += "<tr>";str += "<th>제목</th>";str += "<td>"+ boardSubject +"</td>";str += "<th>조회수</th>";str += "<td>"+ boardHits +"</td>";str += "</tr>";str += "<tr>";str += "<th>작성자</th>";str += "<td>"+ boardWriter +"</td>";str += "<th>작성일시</th>";str += "<td>"+ insDate +"</td>";str += "</tr>";str += "<tr>";str += "<th>내용</th>";str += "<td colspan='3'>"+ boardContent +"</td>";str += "</tr>";} else {alert("등록된 글이 존재하지 않습니다.");return;}$("#tbody").html(str);}/** 게시판 - 삭제 */function deleteBoard(){var boardSeq = $("#board_seq").val();var yn = confirm("게시글을 삭제하시겠습니까?");if(yn){$.ajax({url : "/board/deleteBoard",data : $("#boardForm").serialize(),dataType : "JSON",cache : false,async : true,type : "POST",success : function(obj) {deleteBoardCallback(obj);},error : function(xhr, status, error) {}});}}/** 게시판 - 삭제 콜백 함수 */function deleteBoardCallback(obj){if(obj != null){var result = obj.result;if(result == "SUCCESS"){alert("게시글 삭제를 성공하였습니다.");goBoardList();} else {alert("게시글 삭제를 실패하였습니다.");return;}}}</script></head><body><div id="wrap"><div id="container"><div class="inner"><h2>게시글 상세</h2><form id="boardForm" name="boardForm"><table width="100%" class="table01"><colgroup><col width="15%"><col width="35%"><col width="15%"><col width="*"></colgroup><tbody id="tbody"></tbody></table><input type="hidden" id="board_seq" name="board_seq" value="${boardSeq}"/> <!-- 게시글 번호 --><input type="hidden" id="search_type" name="search_type" value="S"/> <!-- 조회 타입 - 상세(S)/수정(U) --></form><div class="btn_right mt15"><button type="button" class="btn black mr5" onclick="javascript:goBoardList();">목록으로</button><button type="button" class="btn black mr5" onclick="javascript:goBoardUpdate();">수정하기</button><button type="button" class="btn black" onclick="javascript:deleteBoard();">삭제하기</button><button type="button" class="btn black mr5" onclick="javascript:goBoardReply();">답글쓰기</button></div></div></div></div></body></html>cs 3_3) 게시글 목록 페이지 수정
목록 페이지에서 답글인 경우 BOARD_RE_LEV 값 만큼 'Re:' 텍스트를 추가해서 제목을 출력하도록 수정하세요.
boardList.jsp
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>게시판 목록</title><!-- 공통 CSS --><link rel="stylesheet" type="text/css" href="/css/common/common.css"/><!-- 공통 JavaScript --><script type="text/javascript" src="/js/common/jquery.js"></script><script type="text/javascript">$(document).ready(function(){getBoardList();});/** 게시판 - 상세 페이지 이동 */function goBoardDetail(boardSeq){location.href = "/board/boardDetail?boardSeq="+ boardSeq;}/** 게시판 - 작성 페이지 이동 */function goBoardWrite(){location.href = "/board/boardWrite";}/** 게시판 - 목록 조회 */function getBoardList(currentPageNo){if(currentPageNo === undefined){currentPageNo = "1";}$("#current_page_no").val(currentPageNo);$.ajax({url : "/board/getBoardList",data : $("#boardForm").serialize(),dataType : "JSON",cache : false,async : true,type : "POST",success : function(obj) {getBoardListCallback(obj);},error : function(xhr, status, error) {}});}/** 게시판 - 목록 조회 콜백 함수 */function getBoardListCallback(obj){var state = obj.state;if(state == "SUCCESS"){var data = obj.data;var list = data.list;var listLen = list.length;var totalCount = data.totalCount;var pagination = data.pagination;var str = "";if(listLen > 0){for(var a=0; a<listLen; a++){var boardSeq = list[a].board_seq;var boardReRef = list[a].board_re_ref;var boardReLev = list[a].board_re_lev;var boardReSeq = list[a].board_re_seq;var boardWriter = list[a].board_writer;var boardSubject = list[a].board_subject;var boardContent = list[a].board_content;var boardHits = list[a].board_hits;var delYn = list[a].del_yn;var insUserId = list[a].ins_user_id;var insDate = list[a].ins_date;var updUserId = list[a].upd_user_id;var updDate = list[a].upd_date;str += "<tr>";str += "<td>"+ boardSeq +"</td>";str += "<td onclick='javascript:goBoardDetail("+ boardSeq +");' style='cursor:Pointer'>";if(boardReLev > 0){for(var b=0; b<boardReLev; b++){str += "Re:";}}str += boardSubject +"</td>";str += "<td>"+ boardHits +"</td>";str += "<td>"+ boardWriter +"</td>";str += "<td>"+ insDate +"</td>";str += "</tr>";}} else {str += "<tr>";str += "<td colspan='5'>등록된 글이 존재하지 않습니다.</td>";str += "<tr>";}$("#tbody").html(str);$("#total_count").text(totalCount);$("#pagination").html(pagination);} else {alert("관리자에게 문의하세요.");return;}}</script></head><body><div id="wrap"><div id="container"><div class="inner"><h2>게시글 목록</h2><form id="boardForm" name="boardForm"><input type="hidden" id="function_name" name="function_name" value="getBoardList" /><input type="hidden" id="current_page_no" name="current_page_no" value="1" /><div class="page_info"><span class="total_count"><strong>전체</strong> : <span id="total_count" class="t_red">0</span>개</span></div><table width="100%" class="table01"><colgroup><col width="10%" /><col width="25%" /><col width="10%" /><col width="15%" /><col width="20%" /></colgroup><thead><tr><th>글번호</th><th>제목</th><th>조회수</th><th>작성자</th><th>작성일</th></tr></thead><tbody id="tbody"></tbody></table></form><div class="btn_right mt15"><button type="button" class="btn black mr5" onclick="javascript:goBoardWrite();">작성하기</button></div></div><div id="pagination"></div></div></div></body></html>cs 4. 계층형 게시글 확인
소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section18)를 참조하세요.
Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33반응형'Spring > 4.3.x - 게시판 만들기' 카테고리의 다른 글
[Spring] 게시판 만들기(20) - 파일 다운로드(BeanNameViewResolver) (9) 2018.12.20 [Spring] 게시판 만들기(19) - 파일 업로드(MultipartHttpServletRequest) (2) 2018.12.20 [Spring] 게시판 만들기(17) - 페이징(Paging) 처리 (31) 2018.12.19 [Spring] 게시판 만들기(16) - 트랜잭션(Transaction) 설정 (11) 2018.12.19 [Spring] 게시판 만들기(15) - 게시글 등록(INSERT), 수정(UPDATE), 삭제(DELETE), 상세 조회(SELECT) (28) 2018.12.17