본문 바로가기

Java/게시판만들기

[2.게시판만들기] EL과 jstl을 활용하여 간단히 게시판 만들기

이번에는 EL과 jstl을 사용하여 간단히 게시판  

 

1. jstl taglib 다운받기  글쓴이 본인은 C://lecture/web-workspace/Lecture-Web/WebContent/WEB-INF/lib에 다운 받음

다운로드: tomcat.apache.org/

 

 

Apache Tomcat® - Welcome!

The Apache Tomcat® software is an open source implementation of the Jakarta Servlet, Jakarta Server Pages, Jakarta Expression Language, Jakarta WebSocket, Jakarta Annotations and Jakarta Authentication specifications. These specifications are part of the

tomcat.apache.org

(1)taglibs-standard-impl-1.2.5.jar

(2)taglibs-standard-jstlel-1.2.5.jar

(3)taglibs-standard-spec-1.2.5.jar 총 3개 다운

 

2. list.jsp만들기

 

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="kr.ac.mlec.board.vo.BoardVO"%>
<%@page import="kr.ac.mlec.util.JDBCClose"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="kr.ac.mlec.util.ConnectionFactory"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
<%
   Connection conn = new ConnectionFactory().getConnection();
   //string: +쓸 때 속도느려져서 StringBuilder를 쓰겠다
   StringBuilder sql = new StringBuilder();
   sql.append("select no, title, writer, to_char(reg_date, 'yyyy-mm-dd') as reg_date");
   sql.append(" from t_board ");
   sql.append(" order by no desc ");
   
   //sql을 string으로 다시 바꿔줄때 toString을 쓴다
   PreparedStatement pstmt = conn.prepareStatement(sql.toString());
   ResultSet rs = pstmt.executeQuery();
   
   List<BoardVO> boardList = new ArrayList<>();
   
   while(rs.next()){
	   int no = rs.getInt("no");
	   String title = rs.getString("title");
	   String writer = rs.getString("writer");
	   String regDate = rs.getString("reg_date");
	   
	   BoardVO board = new BoardVO();
	   board.setNo(no);
	   board.setTitle(title);
	   board.setWriter(writer);
	   board.setRegDate(regDate);
	   
	   boardList.add(board);
   }
   //공유영역에 등록
   pageContext.setAttribute("boardList", boardList);
   
   JDBCClose.close(conn, pstmt);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <div align ="center">
   <hr width="70%">
   <h1>전제게시물 목록</h1>
   <hr width="70%">
   
      <table border="1" style="width: 70%;">
         <tr>
            <th width="7%">번호</th>
            <th>제목</th>
            <th width="16%">작성자</th>
            <th width="20%">등록일</th>
         </tr>
         <c:forEach items="${boardList }" var="board">
	         <tr>
	         	<td>${board.no }</td>
	         	<%--다른 경로일 경우(다른 폴더)
	         	<a href="/Misiion-Web/jsp/board/detail.jsp">
	         	 --%>
	         	<td><a href="detail.jsp?no=${board.no }">${board.title }</a></td>
	         	<td>${board.writer }</td>
	         	<td>${board.regDate }</td>
	         </tr>
         </c:forEach>
         
      </table>
   </div>
</body>
</html>

 

 

3. detail.jsp만들기

 

<%@page import="kr.ac.mlec.util.JDBCClose"%>
<%@page import="kr.ac.mlec.board.vo.BoardVO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="kr.ac.mlec.util.ConnectionFactory"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//조회할 게시물 번호 추출
	//getParameter > String 으로 반환
	int no = Integer.parseInt(request.getParameter("no"));
	
	//t_board테이블에서 추출한 게시물을 조회
	Connection conn = new ConnectionFactory().getConnection();
	StringBuilder sql = new StringBuilder();
	sql.append("select no, title, writer, content, view_cnt");
	sql.append("   , to_char(reg_date,'yyyy-mm-dd')as reg_date");
	sql.append(" from t_board");
	sql.append(" where no = ?");
	
	PreparedStatement pstmt = conn.prepareStatement(sql.toString());
	pstmt.setInt(1, no);
	ResultSet rs = pstmt.executeQuery();
	
	BoardVO board = null;
	//한개만 해당하는 경우는 if - boardVO
	// 여러개 해당하는 경우 다 뽑으려면 while - list<boardVO>
	if(rs.next()){
		int bNo = rs.getInt("no");
		String title = rs.getString("title");
		String writer = rs.getString("writer");
		String content = rs.getString("content");
		int viewCnt = rs.getInt("view_cnt");
		String regDate = rs.getString("reg_date");
		
		board = new BoardVO(bNo, title, writer, content, viewCnt, regDate);
		
	}
	
	JDBCClose.close(conn, pstmt);
	//java html 공유영역에 등록
	pageContext.setAttribute("board", board);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
	//뭔가 이동하고 싶은 객체 쓸때 location.href를 쓴다. 
	function goList(){
		location.href="list.jsp"
	}
</script>
</head>
<body>
	<div align="center">
	<hr>
	<h1>게시물 상세</h1>
	<hr>
	<br>
	<table border="1" style="width: 80%">
		<tr>
			<th width="25%">번호</th>
			<td>${board.no }</td>
		</tr>
		<tr>
			<th width="25%">제목</th>
			<td>${board.title }</td>
		</tr>
		<tr>
			<th width="25%">작성자</th>
			<td>${board.writer }</td>
		</tr>
		<tr>
			<th width="25%">내용</th>
			<td>${board.content }</td>
		</tr>
		<tr>
			<th width="25%">조회수</th>
			<td>${board.viewCnt }</td>
		</tr>
		<tr>
			<th width="25%">등록일</th>
			<td>${board.regDate }</td>
		</tr>
	</table>
	<br><br>
	<button onclick="goList()">목  록</button>
	</div>
</body>
</html>