AR삽질러

JSP 개인프로젝트(8) - 게시판만들기(게시글목록) 본문

JAVA/JSP

JSP 개인프로젝트(8) - 게시판만들기(게시글목록)

아랑팡팡 2023. 2. 28. 19:57
728x90

https://arang95.tistory.com/56

 

JSP 개인프로젝트(7) - singleton적용, 게시판만들기

https://arang95.tistory.com/43 JSP 개인프로젝트(6) - 회원탈퇴 https://arang95.tistory.com/42 JSP 개인프로젝트(5) - 회원정보수정 https://arang95.tistory.com/40 JSP 개인프로젝트(4) - 회원가입완성, 로그인구현 https://ara

arang95.tistory.com

지난시간에는 singleton을 적용하고 게시판 만들기에 들어갔다.

생각보다 시간이 많이걸리고 애먹는 부분이 많아 또 삽질했다고한다...그래서 예전에 학원에서 배웠던 방법을 적용해서 구현하는 중이다ㅠㅠ

이번시간에는 데이터 삽입과 글목록을 만들어볼시간이다!!

 

게시판 글쓰기 

현재 기능 : 작성자12 이메일30 제목 50 내용 4000 비밀번호12으로 설정하고 글목록 또는 목록을 클릭시 게시판의 글목록이 출력된다.

게시판 글쓰기 페이지에서는 작성자, 이메일, 제목, 내용, 비밀번호를 입력해야 글쓰기가 가능하지만 아직 작성한 글을 넘겨주는 페이지와 글목록 페이지가 없어서 작성해도 소용이 없다. 글쓰기 처리와 글목록 기능과 페이지를 구현해보겠다.

1. 글목록.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="boardone.BoardDAO" %>
<%@ page import="boardone.BoardVO" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<%
	int count = 0;
	int number = 0;
	List<BoardVO> articleList = null;
	BoardDAO dbPro = BoardDAO.getInstance();
	count = dbPro.getArticleCount(); 
	if(count > 0){
		articleList = dbPro.getArticles();
	}
	number = count;
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="../css/bootstrap.css">
	<link rel="stylesheet" href="../css/custom.css">
	<title>메인페이지</title>
	<script type="text/javascript" src="script.js"></script>
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	<script src="../js/bootstrap.js"></script>
</head>
<body>
	<%
		String id = (String)session.getAttribute("id");

	%>
	<nav class="navbar navbar-default">
	 <div class="navbar-header">
	  <button type="button" class="navbar-toggle collapsed"
	  	data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
	  	aria-expanded="false">
		 	<span class="icon-bar"></span>
		 	<span class="icon-bar"></span>
		 	<span class="icon-bar"></span>
		 </button>
		 <a class="navbar-brand" href="main.jsp">AR 웹페이지</a>
		</div>
		<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
		 <ul class="nav navbar-nav">
		  <li class="active"><a href="../memberone/main.jsp">메인</a>
		   <li class="active"><a href="../boardone/writeForm.jsp">게시판</a>
		 </ul>
		 <%
		 	if(id == null){
		 %>
		 <ul class="nav navbar-nav navbar-right">
		  <li class="dropdown">
		   <a href="#" class="dropdown-toggle"
		   	data-toggle="dropdown" role="button" aria-haspopup="true"
		   	aria-expanded="false">접속하기<span class="caret"></span>
		   </a>
		   <ul class="dropdown-menu">
		   	<li><a href="../memberone/login.jsp">로그인</a></li>
		   	<li><a href="../memberone/regForm.jsp">회원가입</a></li>
		   </ul>
		  </li>
		 </ul>
		 <%
		 	}
		 %>
		 
		</div>
	</nav>

<div class="container">
	  <table class="table table-bordered table-hover" style="text-align: center; border: 1px solid #ddddddd">
	   <thead>
	    <tr>
	     <th colspan="6"><h4>글목록(전체 글 : <%=count %>)</h4></th>
	    </tr>
	    <tr>
  		 <td colspan="6" align="right">
  		 <a href="writeForm.jsp"><h5>글쓰기</h5></a>
  	    </td>
	<%
		if(count == 0){
	%>
	<tbody>
     <tr>
      <td align="center"><h5>게시판에 저장된 글이 없습니다.</h5></td>
     </tr>
	</tbody>
	<%}else{ %>
	<tbody>
	     <tr>
	      <td align="center" style="width:50px;"><h5>번 호</h5></td>
	      <td align="center" style="width:250px;" width="250"><h5>제 목</h5></td>
  		  <td align="center" style="width:100px;" width="100"><h5>작성자</h5></td>
  		  <td align="center" style="width:150px;" width="150"><h5>작성일</h5></td>
  		  <td align="center" style="width:50px;" width="50"><h5>조회</h5></td>
  		 
	     </tr>
	</tbody>
	<%
		for(int i =0; i < articleList.size(); i++){
			BoardVO article = (BoardVO)articleList.get(i);
	%>
	<tr height="20">
      <td align="center" style="width:40px;"><h5><%=number--%></h5></td>
      <td width="200">
	 <!--  -->
	  <a href="content.jsp?num=<%=article.getNum()%>&pageNum=1">
	 <!--  -->
	 	<h5><%=article.getSubject() %></h5></a>
	 	<% if(article.getReadcount()>=10){%>
	 	<img src="images/hot.gif" border="0" height="16"><%}%></td>
	 <td align="center" width="100">
	 	<a href="mailto:<h5><%=article.getEmail()%>"></h5><h5><%=article.getWriter() %></h5></a>
	 <td align="center" width="150"><h5><%= sdf.format(article.getRegdate())%></h5></td>
	 <td align="center" width="50"><h5><%=article.getReadcount() %></h5></td>
	 
	</tr>
		<%}%>
</table>
<%}%>
<!--  -->
	   </thead>
	  </table>
	</form>
</div>	      

</body>
</html>

 

2. 게시물 작성글 처리

<%@page import="boardone.BoardDAO"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="article" scope="page" class="boardone.BoardVO">
 <jsp:setProperty name="article" property="*"/>
</jsp:useBean>
<%
	article.setRegdate(new Timestamp(System.currentTimeMillis()) );
	article.setIp(request.getRemoteAddr());
	BoardDAO dbPro = BoardDAO.getInstance();
	dbPro.insertArticle(article);
	response.sendRedirect("list.jsp");
%>

 

3. 글수 읽어오기DAO

public int getArticleCount() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int x = 0;
		try {
			conn = ConnUtil.getcConnection();
			pstmt = conn.prepareStatement("select count(*) from board");
			rs = pstmt.executeQuery();
			if(rs.next()) {
				x = rs.getInt(1);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(rs != null)try {rs.close();}catch(SQLException e) {}
			if(pstmt != null)try {pstmt.close();}catch(SQLException e) {}
			if(conn != null)try {conn.close();}catch(SQLException e) {}
		}
		return x;
	}

 

4. 완성화면

728x90
반응형
LIST