JSP - 간단한 게시판 구현하기(7) - 게시물 수정 (2022-09-29)

2022. 9. 30. 02:293층 1구역 - 개발의 장/JSP

1. 서론

 

이제 게시물을 수정해보자.

일련의 과정들을 거쳐온 여러분들은 수정과 삭제는 이전 코드 돌려쓰기 라는 것을 깨닫게 될 것 이다.

 

2. 본론

 

boardModify(전체코드)

 

<%@page import="session_quiz.BoardDTO"%>
<%@page import="session_quiz.BoardDAO"%>
<%@ page language="java"  pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>boardModify</title>
</head>
<body>
<%@ include file="header.jsp"%>
<div align="center" >

<%
	String n = request.getParameter("num");
	//num이라는 파라미터가 없다면 null이 나오거나
	//num이 빈문자열이라면 boardForm으로 이동한다.
	if(n == null || n == ""){
		response.sendRedirect("boardForm.jsp");
		return;
	}
		
	int num = 0;
	try{
		//문자열의 데이터를 정수로 치환
		num = Integer.parseInt(n);
		
		//정수로 치환할때 문제가 생기면 ex == 18a
	} catch(Exception e){
		//예외처리를 통해 boardForm으로 이동한다.
		response.sendRedirect("boardForm.jsp");
		return;
	}
	
	BoardDAO boardDao = new BoardDAO();
	BoardDTO board = boardDao.selectNum(num);
	if(board == null){
		//예를 들어 게시물 넘버가 18인데 주소창에 19번을 입력하거나
		//장난질 치면 boardForm으로 돌아온다.
		response.sendRedirect("boardForm.jsp");
		return;
	}
%>
<!-- 인풋의 타입을 히든으로 하여 value에 변수명을 출력하면
form태그가 사용중인 메서드형식으로 보내줄 수 있다.(url에 담겨서 가는게 아닌 post타입으로...) -->
<form action="boardModifyService.jsp" method="post">
	<input type="hidden" name="num" value="<%=num%>">
	<table border='1'>
		<tr>
			<th width="200">작성자</th>
			<td width="200"><%=board.getId() %></td>
			<th width="100">조회수</th>
			<td width="200"><%=board.getHit() %></td>
		</tr>
		<tr>
			<th>작성일</th>
			<td><%=board.getWriteTime() %></td>
			<th>다운로드</th>
			<td><%=board.getFileName() %></td>
		</tr>
		<tr>
			<th>제목 수정</th>
			<td colspan="3">
				<input style="width:100%;" type="text" name="title" value="<%=board.getTitle()%>">
			</td>
		</tr>
		<tr>
			<th>문서내용 수정</th>
			<td colspan="3">
				<textarea rows="10" cols="30" style="width:100%" name="content"><%=board.getContent() %></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="4" >
				<button type="button" onclick = "location.href='javascript:history.back();'">이전</button>
				<input type="submit" value="수정">
			</td>
		</tr>
	</table>
</form>
</div>
<%@ include file="footer.jsp"%>
</body>
</html>

 

위 코드에서 눈여겨 볼건

<input type="hidden" name="num" value="<%=num%>"> 얘 하나이다.

input 태그이지만 type이 hidden이기 때문에 사용자들의 눈에는 보이지 않지만

form태그가 사용하는 메서드 형식으로 value의 값을 Service쪽으로 보내 줄수 있다.

 

<%@page import="session_quiz.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	 request.setCharacterEncoding("utf-8");
	String n = request.getParameter("num");
	//num이라는 파라미터가 없다면 null이 나오거나
	//num이 빈문자열이라면 boardForm으로 이동한다.
	if(n == null || n == ""){
		response.sendRedirect("boardForm.jsp");
		return;
	}
		
	int num = 0;
	try{
		num = Integer.parseInt(n);
		//문자열의 데이터를 정수로 치환
		//정수로 치환할때 문제가 생기면 ex == 18a
	} catch(Exception e){
		//예외처리를 통해 boardForm으로 이동한다.
		response.sendRedirect("boardForm.jsp");
		return;
	} 
	
	out.print("num : " + num);

 

예를 들어 위와 같이 service코드를 구성했다. 

22번의 게시물을 수정하려고 한다.

 

그러면 수정했을 때

service쪽에서 해당 게시물의 번호를 받게 된다.

 

boardModifyService(전체코드)

 

<%@page import="session_quiz.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	 request.setCharacterEncoding("utf-8");
	String n = request.getParameter("num");
	//num이라는 파라미터가 없다면 null이 나오거나
	//num이 빈문자열이라면 boardForm으로 이동한다.
	if(n == null || n == ""){
		response.sendRedirect("boardForm.jsp");
		return;
	}
		
	int num = 0;
	try{
		num = Integer.parseInt(n);
		//문자열의 데이터를 정수로 치환
		//정수로 치환할때 문제가 생기면 ex == 18a
	} catch(Exception e){
		//예외처리를 통해 boardForm으로 이동한다.
		response.sendRedirect("boardForm.jsp");
		return;
	} 
	
	/*out.print("num : " + num);*/
	
	//boardModify에서 입력받은 값을 불러온다.
 	 String title = request.getParameter("title");
	String content = request.getParameter("content");
	
	//만약 제목이나 내용이 비어있으면
	if(title.isEmpty() || content.isEmpty()){
		//alert창을 띄우고 다시 수정창으로 돌아간다.
		out.print("<script>alert('제목과 내용을 입력해주세요.'); location.href='boardModify.jsp';</script>");
		return;
	}
	
	//DAO클래스를 boardDao변수로 지정하여 새로운 DAO를 선언
	BoardDAO boardDao = new BoardDAO();
	//DAO클래스의 modify메서드를 호출하여 boardModify에서 받은 파라미터값을 전달한다.
	boardDao.modify(num, title, content);
	//그리고 연결을 끊는다.
	boardDao.disconnection(); 
%>
<!-- 일련의 과정이 정상적으로 완료 되었다면 alert창을 띄우고 boardForm으로 돌아간다. -->
 <script>alert('게시글 수정이 완료되었습니다.'); location.href='boardForm.jsp';</script>

 

3. 결론

 

이제 남은건 삭제 뿐인데............이 코드가 조금 복잡하다

그래도 역시나 코드 돌려쓰기 이기 때문에 코드를 어떻게 쓰고 있는지 이해했다면 조금 이해가 빠르지 않을까 생각한다.