spring실습1-4(게시판)

거너거너·2021년 11월 2일
0

spring(학원)

목록 보기
7/16
  1. boardcontent.jsp(목록버튼까지)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상세 페이지</title>
</head>
<body>

<table	border=1 width=400 align="center">
	<caption>상세 페이지</caption>
	<tr>
		<td>작성자</td>
		<td>${board.writer }</td>
	</tr>
	<tr>
		<td>날짜</td>
		<td>
		<fmt:formatDate value="${board.register }"
			pattern="yyyy-MM-dd HH:mm:ss"/>
		</td>
	</tr>
	<tr>
		<td>조회수</td>
		<td>${board.readcount }</td>
	</tr>
	<tr>
		<td>제목</td>
		<td>${board.subject }</td>
	</tr>
		<tr>
		<td>내용</td>
		<td>
		<pre>${board.content }</pre>
		${content }
		</td>
	</tr>
	<tr>
		<td colspan=2 align=center>
			<input type="button" value="목록"
			onClick="location.href='boardlist.do?page=${page}' ">
			<input type="button" value="수정">
			<input type="button" value="삭제">
		</td>
	</tr>
</table>

</body>
</html>
  1. boardcontent.jsp(수정버튼 추가)
<input type="button" value="수정"
onClick="location.href='boardupdateform.do?no=${board.no}&page=${page}' ">
  1. 컨트롤러
	// 수정 폼
	@RequestMapping("boardupdateform.do")
	public String boardupdateform(int no, String page, Model model) {
		
		Board board = service.getBoard(no);		// 상세 정보 구하기
		
		model.addAttribute("board", board);
		model.addAttribute("page", page);
		
		return "board/boardupdateform";
	}
  1. boardupdateform.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글수정</title>
</head>
<body>

<form method=post action="boardupdate.do">
<input type="hidden" name="no" value="${board.no}">
<input type="hidden" name="page" value="${page}">
<table border=1 width=400 align=center>
	<caption>글수정</caption>
	<tr><th>작성자명</th>
		<td><input type=text name="writer" value="${board.writer }"></td>
	</tr>
	<tr><th>비밀번호</th>
		<td><input type=password name="passwd" required="required"></td>
	</tr>
	<tr><th>제목</th>
		<td><input type=text name="subject" value="${board.subject }"></td>
	</tr>
	<tr><th>내용</th>
		<td><textarea cols=40 rows=5 name="content">${board.content }</textarea></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="글수정">
			<input type=reset value="취소">
		</td>
	</tr>
</table>
</form>

</body>
</html>
  1. 컨트롤러
	// 글 수정
	@RequestMapping("boardupdate.do")
	public String boardupdate(Board board, String page, Model model) {
		int result = 0;
		Board old = service.getBoard(board.getNo());		// 상세 정보 구하기
		if(old.getPasswd().equals(board.getPasswd())) {	// 비번 일치시
			result = service.update(board);
		}else {	// 비번 불일치시
			result = -1;
		}
		model.addAttribute("result", result);
		model.addAttribute("board", board);
		model.addAttribute("page", page);
		
		return "board/updateresult";
	}
  1. 서비스
	public int update(Board board) {
		return dao.update(board);
	}

51.DAO

	public int update(Board board) {
		return session.update("update", board);
	}
  1. board.xml
	<!-- 글수정 -->
	<update id="update" parameterType="board">
		update myboard set writer=#{writer}, subject=#{subject},
		content=#{content}, register=sysdate where no=#{no}
	</update>
  1. updateresult.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>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<c:if test="${result == 1 }">
	<script>
		alert("수정 성공");
//		location.href="boardlist.do?page=${page}";	// 목록 페이지
		location.href="boardcontent.do?no=${board.no}&page=${page}";	// 상세 페이지
	</script>
</c:if>
<c:if test="${result != 1 }">
	<script>
		alert("수정 실패");
		history.go(-1);
	</script>
</c:if>

</body>
</html>
  1. 컨트롤러
	// 삭제 폼
	@RequestMapping("boarddeleteform.do")
	public String boarddeleteform() {
		return "board/boarddeleteform";
	}
  1. boarddeleteform.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글삭제</title>
</head>
<body>

no : ${param.no} <br>
page : ${param.page}

<form method=post action="boarddelete.do">
<input type="hidden" name="no" value="${param.no}">
<input type="hidden" name="page" value="${param.page}">
<table border=1 width=400 align=center>
	<caption>글삭제</caption>
	<tr><th>비밀번호</th>
		<td><input type=password name="passwd" required="required"></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="글삭제">
			<input type=reset value="취소">
		</td>
	</tr>
</table>
</form>

</body>
</html>
  1. 컨트롤러
	// 글 삭제
	@RequestMapping("boarddelete.do")
	public String boarddelete(Board board, String page, Model model) {
		int result = 0;
		Board old = service.getBoard(board.getNo());
		if(old.getPasswd().equals(board.getPasswd())) {	// 비번 일치시
			result = service.delete(board.getNo());
		}else {		// 비번 불일치시
			result = -1;
		}
		model.addAttribute("result", result);
		model.addAttribute("page", page);
		
		return "board/deleteresult";
	}
  1. 서비스
	public int delete(int no) {
		return dao.delete(no);
	}
  1. DAO
	public int delete(int no) {
		return session.delete("delete", no);
	}
  1. board.xml
	<!-- 글삭제 -->
	<delete id="delete" parameterType="int">
		delete from myboard where no = #{no}
	</delete>
profile
배움이 필요한 사람

0개의 댓글