본문 바로가기
IT/JSP Practice

[JSP Practice] - JSP 게시판 CRUD + 페이징(Paging)처리 Model 1 방식

by 차이나는 개발자 2020. 7. 27.
728x90
반응형

#JSP 게시판 CRUD + 페이징(Paging)처리 Model 1 방식

 

 

※JSP Model 1 방식

-data => application

 

 

1. 글쓰기 화면입니다.

 

 

2. 글목록 화면입니다.

 

 

3. 상세 화면입니다.

 

 

#Doc.java

package kr.ac.green;

public class Doc implements Comparable<Doc>{
	
	private static int count = 1;
	private int num;
	private String title;
	private String writer;
	private String contents;
	private String pw;
	private String date;

	public Doc(int num){
		this.num = num;
	}
	
	public Doc(String title, String writer, String contents, String pw, String date) {
		super();
		this.title = title;
		this.writer = writer;
		this.contents = contents;
		this.pw = pw;
		this.date = date;
		num = count++;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public static int getCount() {
		return count;
	}

	public static void setCount(int count) {
		Doc.count = count;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	
	@Override
	public int compareTo(Doc other) {
		return (num - other.getNum()) * -1;
	}
	
	@Override
	public boolean equals(Object o) {
		if(o == null || !(o instanceof Doc)) {
			return false;
		}
		Doc other = (Doc)o;
		return num == other.getNum();
	}
}

 

 

#template.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style>
	html {
		width : 100%;
		height : 100%;
		margin : 0;
		padding: 0;
	}
	
	body {
		width : 100%;
		height : 100%;
		margin : 0;
		padding: 0;
	}
	
	#mainFrame {
		margin : 0 auto;
		width : 70%;
		height : 80%;
		text-align : center;
		padding : 0;
	}
	
	#logo {
		width : 100%;
		height : 20%;
	}
	
	#center {
		width : 100%;
		height : 80%;
		margin : 0;
		padding : 0;
	}
	
	#menu{
		float : left;
		width : 20%;
		height : 100%;
	}
	
	#contents {
		float : right;
		width : 80%;
		height : 100%;
		text-align : left;
	}
	
	a {
		text-decoration : none;
	}
	
	a:hover {
		font-weight : bold;
	}
	
	li {
		list-style : none;
		line-height : 3em;
	}
	
	input[type=text]{
		width : 20em;
		height : 1.5em;
	}
	
	textarea {
		width : 40em;
	}
	
	#docList {
		border-collapse : collapse;
		width : 80%;
	}
	
	#docListHeader {
		background-color : #cccccc;
	}
	
	#docList td, #docList th {
		border : 1px solid black;
		text-align : center;
	}
	
	caption {
		line-height : 3em;
		font-weight : bold;
	}
	
	#docNum {
		width : 5%;
	}
	
	#docContents {
		width : 50%;
	}
	
	#docWriter {
		width : 25%;
	}
	
	fieldset{
		width : 80%;
		line-height : 2em;
	}
</style>
<%
	String contentsPage = request.getParameter("contentsPage");
	if(contentsPage == null){
		contentsPage = "list";
	}
		contentsPage += ".jsp";
%>

<%
	String msg = "어서오세요";

	Object o = session.getAttribute("msg");
	
	if(o != null){
		msg = o.toString();
	}
	
	session.removeAttribute("msg");
%>
</head>
<body>
	<div id="mainFrame">
		<div id="logo">
			<h1>Blue Book</h1>
			<p><%= msg %></p>
		</div>
		<div id="center">
			<div id="menu"><jsp:include page="menu.jsp" /></div>
			<div id="contents"><jsp:include page="<%=contentsPage %>" /></div>
		</div>
		<div>
			<div id="footer"><jsp:include page="footer.jsp" /></div>
		</div>
	</div>	
</body>
</html>

 

 

#menu.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<ul>
	<li> - Menu -</li>
	<li><a href="template.jsp?contentsPage=list">목록보기</a></li>
	<li><a href="template.jsp?contentsPage=write">글쓰기</a></li>
</ul>

 

 

#write.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<fieldset>
	<legend>글쓰기</legend>
	<!-- 사용자 입력값 -->
	<form action="doWrite.jsp" method="post">
		<input type="text" name="title" placeholder="제목" required/>
		<br/> 
		<input type="text" name="writer" placeholder="작성자" required />
		<br/><br/>
		<textarea name="contents" rows="5" placeholder="내용" required></textarea>
		<br>
		<input type="password" name="pw" placeholder="비밀번호" required/>
		<br/>
		<input type="submit" value="완료" />
		<input type="reset" value="초기화" />
	</form> 
</fieldset>

 

 

#doWrite.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="kr.ac.green.*"%>
<%
	request.setCharacterEncoding("euc_kr");

String title = request.getParameter("title");
String writer = request.getParameter("writer");
String contents = request.getParameter("contents");
String pw = request.getParameter("pw");
String date = new SimpleDateFormat("yyyy.MM.dd").format(new Date());

Doc doc = new Doc(title, writer, contents, pw, date);

	Vector<Doc> docList = (Vector<Doc>)application.getAttribute("docList");
	if(docList == null){
		docList = new Vector<Doc>();
		application.setAttribute("docList", docList);
	}
	
	docList.add(doc);
	
	response.sendRedirect("template.jsp");
%>

 

 

#list.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.*" %>
<%
	Vector<Doc> docList = (Vector<Doc>)application.getAttribute("docList");
	if(docList == null){
		docList = new Vector<Doc>();
	}
	
	Collections.sort(docList);
	
	int pageNum = 1;
	String paramPageNum = request.getParameter("pageNum");
	if(paramPageNum != null){
		pageNum = Integer.parseInt(paramPageNum);
	}
	
	int size = docList.size();
	int perPage = 3;
	int pageCount = size / perPage;
	if((size % perPage) != 0){
		pageCount++;
	}
	
	int start = (pageNum - 1) * perPage;
	int end = start + perPage;
	if(end > size){
		end = size;
	}
	
	List<Doc> list = docList.subList(start, end);

%>

<table id="docList">
	<caption> - 글목록 - </caption>
	<thead>
		<tr id="docListHeader">
			<th id="docNum">번호</th>
			<th id="docContents">제목</th>
			<th id="docWriter">작성자</th>
			<th id="docDate">작성일</th>
		</tr>
	</thead>
	<%
		if(size != 0){
	%>
	<tfoot>
		<tr>
			<td colspan="4">
				<%
					for(int i = 1; i <= pageCount; i++){
						if(i == pageNum){
				%>
					[<b><%= i %></b>] 
				<%
					} else {
				%>	
					[<a href="template.jsp?contentsPage=list&pageNum=<%=i %>"><%= i %></a>]
				<%
						}
					}
				%>
			</td>
		</tr>
	</tfoot>
	<% 
		}
	%>
	<tbody>
		<%
			if(size == 0){
		%>
			<tr>
				<td colspan="4">no data</td>
			</tr>
		<%
			}else{
				for(int num = 0; num<list.size(); num++){				
				Doc doc = list.get(num);					
		%>
				<tr onclick="select(<%=doc.getNum()%>)">
				<td><%= doc.getNum() %></td>
				<td><%= doc.getTitle() %></td>
				<td><%= doc.getWriter() %></td>
				<td><%= doc.getDate() %></td>
			</tr>
			
		<%
				}
			}
		%>	
	</tbody>
	
</table>
<form id="hiddenForm" name="hiddenForm" action="showDetail.jsp">
	<input type="hidden" name="num" />
</form>
<script>

	function select(num){
		var myForm = document.hiddenForm;
		myForm.num.value = num;
		myForm.submit();
	}	

</script>

 

 

#showDetail.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>    
<%@ page import="kr.ac.green.*" %>
<%
	int num = Integer.parseInt(request.getParameter("num"));

	Vector<Doc> list = (Vector<Doc>)application.getAttribute("docList");
	
	Doc doc = list.get(list.indexOf(new Doc(num))); 
	
%>


<fieldset>
	<legend>상세화면</legend>
	<!-- 사용자 입력값 -->
	<form id="modifyForm" method="post">
		글번호 : <input type="text" name="num" placeholder="번호" value="<%= doc.getNum()%>" readonly />
		<br/><br/> 
		제목 : <input type="text" name="title" placeholder="제목" value="<%= doc.getTitle() %>" />
		<br/><br/> 
		작성자 : <input type="text" name="writer" placeholder="작성자" value="<%=doc.getWriter() %>" readonly />
		<br/><br/>
		내용 : <br>
		<textarea name="contents" rows="5" ><%= doc.getContents() %></textarea>
		<br><br/>
		날짜 : <input type="text" name="date" placeholder="날짜" value="<%= doc.getDate() %>" />
		<br/><br/>
		비밀번호 : <input type="password" name="pw" placeholder="비밀번호" />
		<br/><br/>
		<input type="button" value="목록가기" onclick="goList()" />
		<input type="button" value="수정" onclick="todo('update')"/>
		<input type="button" value="삭제" onclick="todo('delete')"/>
	</form> 
</fieldset> 
<script>
	function goList(){ 
		location.href ="template.jsp";
	}
	
	function todo(){
		var what = arguments[0];
		
		var modifyForm = document.getElementById("modifyForm");
		
		modifyForm.action = what + ".jsp";
		modifyForm.submit();
	}
</script>

 

 

#update.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.*" %>
<%
	// 번호, 제목, 내용, 비밀번호
	int num = Integer.parseInt(request.getParameter("num"));
	String title = request.getParameter("title");
	String contents = request.getParameter("contents");
	String pw = request.getParameter("pw");
	
	Vector<Doc> list = (Vector<Doc>)application.getAttribute("docList");
	
	Doc original = list.get(list.indexOf(new Doc(num)));
	
	
	String msg = "수정이 완료되었습니다.";
	if(pw.equals(original.getPw())){
		original.setTitle(title);
		original.setContents(contents);
	}else{
		msg = "비밀번호가 일치하지 않습니다.";
	}
	
	
	session.setAttribute("msg", msg);
	response.sendRedirect("template.jsp");
%>

 

 

#delete.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>    
<%@ page import="kr.ac.green.*" %>
<%
	int num = Integer.parseInt(request.getParameter("num"));

	String pw = request.getParameter("pw");
	 	
	Vector<Doc> list = (Vector<Doc>)application.getAttribute("docList");
	
	Doc temp = new Doc(num);
	
	Doc original = list.get(list.indexOf(temp));
	
	String msg = "삭제가 완료되었습니다.";
	if(pw.equals(original.getPw())){
		list.remove(temp);
	}else{
		msg ="비밀번호가 일치하지 않습니다.";
	}
	
	session.setAttribute("msg", msg);
	response.sendRedirect("template.jsp");
%>

 

 

 

728x90
반응형

댓글