본문으로 바로가기

02. Spring MVC 게시판 구현⑤ - 답글작성★

category IT/SpringDay 2022. 5. 18. 23:10

< 답변할 원글의 정보가져오기 >

1.Controller 

@RequestMapping("/reply_view")
	public String reply_view(HttpServletRequest request, Model model) {
		model.addAttribute("request", request);
		command = new BReplyViewCommand();
		command.execute(model);
		
		return "reply_view";
	}

 

2.BReplyViewCommand()

- bId를 사용하여 원글의 정보를 가져온다.

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;

public class BReplyViewCommand implements BCommand {

	@Override
	public void execute(Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");

		int bId = Integer.parseInt(request.getParameter("bId"));
		
		BDao dao = new BDao();
		BDto dto = dao.reply_view(bId);
		
		model.addAttribute("reply_view", dto);
	}

}

 

3. DAO : reply_view(int bId)

public BDto reply_view(int bId) {
		BDto dto = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dataSource.getConnection();
			String sql = "select * from mvc_board where bId=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bId);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				String bName = rs.getString("bName");
				String bTitle = rs.getString("bTitle");
				String bContent = rs.getString("bContent");
				Timestamp bDate = rs.getTimestamp("bDate");
				int bHit = rs.getInt("bHit");
				int bGroup = rs.getInt("bGroup");
				int bStep = rs.getInt("bStep");
				int bIndent = rs.getInt("bIndent");
				dto = new  BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
				System.out.println("reply_view bId->" + bId);
				System.out.println("reply_view bGroup->" + bGroup);
				System.out.println("reply_view bStep->" + bStep);
				System.out.println("reply_view bIndent->" + bIndent);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) 	pstmt.close();
				if(conn != null) 	conn.close();
				if(rs != null) 		rs.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return dto;
	}

 

 

< 원글의 정보를 가져온 후 답글 작성>

1. Controller

@RequestMapping(value = "/reply", method = RequestMethod.POST)
	public String reply(HttpServletRequest request, Model model) {
		System.out.println("Controller reply Start..");
		model.addAttribute("request", request);
		command = new BReplyCommand();
		command.execute(model);
		
		return "redirect:list";
	}

 

2. BReplyCommand();

package com.oracle.oMVCBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.oracle.oMVCBoard.dao.BDao;

public class BReplyCommand implements BCommand {

	@Override
	public void execute(Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		int bId = Integer.parseInt(request.getParameter("bId"));
		String bName = request.getParameter("bName");
		String bTitle = request.getParameter("bTitle");
		String bContent = request.getParameter("bContent");
		//게시판 그룹
		int bGroup = Integer.parseInt(request.getParameter("bGroup"));
		//답글 순서
		int bStep = Integer.parseInt(request.getParameter("bStep"));
		//답글 들여쓰기
		int bIndent = Integer.parseInt(request.getParameter("bIndent"));
		
		BDao dao = new BDao();
		dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
	}

}

 

3. DAO

 - replyShape : 답글이 들어갈 위치를 만들어 준다고 생각하자.

  -> 게시글 그룹 중 댓글 순서가 작으면 중간 순서에 들어가야 하니까.

     step이 파라메터에서 가져온 bStep 보다 다 하나씩 커져야 함. 

public void reply(int bId, String bName, String bTitle, String bContent, int bGroup, int bStep, int bIndent) {
		
		System.out.println("Dao reply bId->" + bId);
		
		replyShape(bGroup, bStep);
		System.out.println("Shape End");
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = dataSource.getConnection();
			String sql = "insert into mvc_board (bId, bName, bTitle, bContent, bGroup, bStep, bIndent)"
					+ "values (mvc_board_seq.nextval, ?, ?, ?, ?, ?, ?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, bName);
			pstmt.setString(2, bTitle);
			pstmt.setString(3, bContent);
			pstmt.setInt(4, bGroup);
			pstmt.setInt(5, bStep+1);
			pstmt.setInt(6, bIndent+1);
			
			int rn = pstmt.executeUpdate();
			if(rn > 0) System.out.println("reply 성공");
			else       System.out.println("reply 실패");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
	
	}

	private void replyShape(int bGroup, int bStep) {
		Connection conn = null;
		PreparedStatement pstmt = null;	
		try {
			conn = dataSource.getConnection();
			//게시글 그룹 중 댓글 순서가 작으면 중간 순서에 들어가야 하니까. step이 파라메터에서 가져온 bStep 보다 다 하나씩 커져야 함. 
			String sql = "update mvc_board set bStep=bStep+1 where bGroup =? and bStep > ?";
			System.out.println("DAO replyShape bGroup->"+bGroup);
			System.out.println("DAO replyShape bStep->"+bStep);
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, bGroup);
			pstmt.setInt(2, bStep);	
			int rn = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
		
	}

 

4. 화면