2022.10.11 - [Spring] - Spring) How to start a new Spring Framework Project[1]

 

Spring) How to start a new Spring Framework Project[1]

Starting a new Spring Framework Project is similar to creating a Dynamic Web Project in Eclipse. However, the structure is a bit more complicated if it is your very first time. So in this post, we w..

www.agilemeadow.com

 

32. Create boardlist.jsp 

<%@ 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="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post List</title>
</head>
<body>
	<a href="boardform.do">New post</a>
	<br> Total posts: ${listcount }
	<table border=1 align=center width=700>
		<caption>Post list</caption>
		<tr>
			<th>No.</th>
			<th>Title</th>
			<th>Writer</th>
			<th>Date</th>
			<th>View Count</th>
		</tr>

		<!-- Post List -->
		<c:set var="num" value="${listcount - (page-1) * 10 }" />
		<c:forEach var="b" items="${boardlist}">
			<tr>
				<td>${num }<c:set var="num" value="${num-1 }" />
				</td>
				<td>
				<a href="boardcontent.do?no=${b.no }&page=${page}"/>
				${b.subject }
				</td>
				<td>${b.writer }</td>
				<td>
				<fmt:formatDate value="${b.register }"
					 pattern="dd-MM-yyyy HH:mm:ss"/>
				</td>
				<td></td>
			</tr>
		</c:forEach>
	</table>
	
	<!-- Pagination -->
	<center>
	<c:if test="${listcount > 0 }">
	
	<!-- Move to page no.1 -->
	<a href="boardList.do?page=1" style="text-decoration:none"> << </a>
	
	<!-- Move to the previous block -->
	<c:if test="${starPage > 10 }">
	<a href="boardlist.do?page=${startPage-1 }" style="text-decoration:none">[Prev]</a>
	</c:if>
	
	<!-- Print 10 posts in each block -->
	<c:forEach var="i" begin="${startPage }" end="${endPage }">
	<c:if test="${i == page }"> <!-- Current Page -->
	 [${i }]
	</c:if>
	
	<c:if test="${i != page }"> <!-- Not a current Page -->
	 <a href="boardlist.do?page${i}">[${i }]</a>
	</c:if>
	
	</c:forEach>
	
	<!-- Move to the next block -->
	<c:if test="${endPage < pageCount }">
	<a href="boardlist.do?page${startPage+10 }" style="text-decoration:none">[Next]</a>
	</c:if>
	
	<!--  Move to the last page -->
	<a href="boardlist.do?page${pageCount }" style="text-decoration:none"> >> </a>
	
	</c:if>
	</center>
	
</body>
</html>

33. In the controller class, add codes for the detail page. 

BoardController.java

// Post Detail : view count +1 , Post detail
		@RequestMapping("boardcontent.do")
		public String boardcontent(int no, int page, Model model) {
			
			//View count +1
			bs.updatecount(no);
			Board board = bs.getBoard(no);
			String content = board.getContent().replace("\n", "<br>");
			
			model.addAttribute("board", board);
			model.addAttribute("content", content);
			model.addAttribute("page", page);
			
			return "board/boardcontent";
		}

33. In the service class, connect it to the dao class.

BoardService.java

	public void updatecount(int no) {
		dao.updatecount(no);
	}

	public Board getBoard(int no) {
		return dao.getBoard(no);
	}

BoardDao.java

	public void updatecount(int no) {
		// TODO Auto-generated method stub
		session.update("hit", no);
	}

	public Board getBoard(int no) {
		// TODO Auto-generated method stub
		return session.selectOne("content", no);
	}

34. In the board.xml, add the update and select SQL.

	<!-- view count + 1 -->
	<update id="hit" parameterType="int">
		update myboard set readcount=readcount+1 where no=#{no}
	</update>
	
	<!-- Post detail -->
	<select id="content" parameterType="int" resultType="board">
		select * from myboard where no = #{no}
	</select>

35. Create boardcontent.jsp.

Link the List, Edit, and Delete buttons to corresponding pages. 

<%@ 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>Post detail</title>
</head>
<body>
	<table border=1 width=400 align=center>
		<caption>Post detail</caption>
		<tr>
			<td>Writer</td>
			<td>${board.writer }</td>
		</tr>
		<tr>
			<td>Date</td>
			<td><fmt:formatDate value="${board.register }"
					pattern="dd-MM-yyyy HH:mm:ss" /></td>
		</tr>
		<tr>
			<td>View</td>
			<td>${board.readcount }</td>
		</tr>
		<tr>
			<td>Title</td>
			<td>${board.subject }</td>
		</tr>
		<tr>
			<td>Content</td>
			<td><pre>${board.content }</pre> ${content}</td>
		</tr>
		<tr>
			<td colspan=2 align=center>
			<input type="button" value="List"
			onClick ="location.href='boardlist.do?page=${page}' ">
			<input type="button" value="Edit"
			onClick="location.href='boardupdateform.do?no=${board.no}&page=${page}'"> 
			<input type="button" value="Delete"
			onClick="location.href='boarddeleteform.do?no=${board.no }&page=${page}'">
			</td>
		</tr>

	</table>

</body>
</html>

36. In the controller class, we will request the values with @RequestMapping annotation

// Post Detail : view count +1 , Post detail
		@RequestMapping("boardcontent.do")
		public String boardcontent(int no, int page, Model model) {
			
			//View count +1
			bs.updatecount(no);
			Board board = bs.getBoard(no);
			String content = board.getContent().replace("\n", "<br>");
			
			model.addAttribute("board", board);
			model.addAttribute("content", content);
			model.addAttribute("page", page);
			
			return "board/boardcontent";
		}

37. Create boardupdateform.jsp and link the controller class to this file. 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Post</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>Edit Post</caption>
	<tr><th>Writer</th>
		<td><input type=text name="writer" required="required"
		value="${board.writer }" autofocus="autofocus"></td>
	</tr>
	<tr><th>Password</th>
		<td><input type=password name="passwd" required="required"></td>
	</tr>
	<tr><th>Title</th>
		<td><input type=text name="subject" required="required" value="${board.subject }"></td>
	</tr>
	<tr><th>Content</th>
		<td><textarea cols=40 rows=5 name="content" required="required">${board.content }</textarea></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="Edit">
			<input type=reset value="Cancel">
		</td>
	</tr>
</table>
</form>

</body>
</html>

38. BoardController.java -> BoardService.java -> BoardDao.java

BoardController.java

//Update Form 
		@RequestMapping("boardupdateform.do")
		public String boardupdateform(int no, int page, Model model) {
			
			Board board = bs.getBoard(no);	//Detail 
			
			model.addAttribute("board", board);
			model.addAttribute("page", page);
			
			return "board/boardupdateform";
		}
		
		// Update
		@RequestMapping("boardupdate.do")
		public String boardupdate(Board board, int page, Model model) {
			int result = 0;
			Board old = bs.getBoard(board.getNo());
			
			// Password Check
			if(old.getPasswd().equals(board.getPasswd())) { //Correct Password
				result = bs.update(board);			// Update 
			}else {    										//Incorrect Password
				result = -1;
			}
			model.addAttribute("result", result);
			model.addAttribute("board", board);
			model.addAttribute("page", page);

			return "board/updateresult";
		}

BoardService.java 

	public int update(Board board) {
		// TODO Auto-generated method stub
		return dao.update(board);
	}

BoardDao.java

	public int update(Board board) {
		// TODO Auto-generated method stub
		return session.update("update", board);
	}

39. Insert update SQL in the board.xml.

<!-- Update -->
	<update id="update" parameterType="board">
		update myboard set writer=#{writer}, subject=#{subject}, 
		content=#{content}, register=sysdate where no=#{no}
	</update>

40. Create 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>updateresult</title>
</head>
<body>

	<c:if test="${result == 1 }">
		<script>
			alert("Successfully updated.");
			location.href ="boardlist.do?page=${page}"; //List page
			//	location.href="boardcontent.do?no=${board.no}&page=${page}";	//Detail page
		</script>
	</c:if>

	<c:if test="${result != 1 }">
		<script>
			alert("Failed to update.");
			history.go(-1);
		</script>
	</c:if>

</body>
</html>

41. Create boarddeleteform.jsp.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete Post</title>
</head>
<body>

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

<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>Delete Post</caption>
	
	<tr><th>Password</th>
		<td><input type=password name="passwd" required="required"></td>
	</tr>
	<tr><td colspan=2 align=center>
			<input type=submit value="Delete">
			<input type=reset value="Cancel">
		</td>
	</tr>
</table>
</form>

</body>
</html>

42. Add the code in the BoardController.java -> BoardService.java -> BoardDao.java.

BoardController.java

		// Delete
		@RequestMapping("boarddelete.do")
		public String boarddelete(Board board, int page, Model model) {
			int result = 0;
			Board old = bs.getBoard(board.getNo());		// Post Detail
			
			//Password Check
			if(old.getPasswd().equals(board.getPasswd())) { //Correct Password
				result = bs.delete(board.getNo());			// Delete
			}else {    										//Incorrect Password
				result = -1;
			}
			
			model.addAttribute("result", result);
			model.addAttribute("page", page);
			
			return"board/deleteresult";
		}
}

BoardService.java

	public int delete(int no) {
		// TODO Auto-generated method stub
		return dao.delete(no);
	}

BoardDao.java.

	public int delete(int no) {
		// TODO Auto-generated method stub
		return session.delete("delete", no);
	}

43. Insert delete SQL in the board.xml.

	<!-- Delete -->
	<delete id="delete" parameterType="int">
		delete from myboard where no = #{no}
	</delete>

44. Create deleteresult.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>Delete</title>
</head>
<body>
	<c:if test="${result == 1 }">
		<script>
			alert("Successfully deleted.");
			location.href = "boardlist.do?page=${page}";
		</script>
	</c:if>

	<c:if test="${result != 1 }">
		<script>
			alert("Failed to delete.");
			history.go(-1);
		</script>
	</c:if>

</body>
</html>

As you can see, once you get the pattern, it becomes easier to process more efficiently. 

+ Recent posts