Flaming Soccer ball
통합

MVC2 게시판 리스트 출력하기 55일차 22.12.07

leo lee 2022. 12. 7.
반응형

어제 생성했던 데이터를 이제 페이지에 나타내는 작업을 하려고한다.

 

항상 가장 먼저 해야할 일은 DAO에 메서드를 생성해 주는 것이다 이 경우에는 기존에 저장이 되어있는 데이터를 불러와야 하므로 SELECT문을 활용하여 데이터를 불러오고 저장해준다. 

가장 최신순으로 값을 출력해주기 위해서 PRIMARY KEY인 BNO를 내림차순을 통해서 정렬해준다

 

활용할 SQL문

select * from board order by bno desc;

 

VO객체 리스트를 받아올 DAO method 생성 

public ArrayList<BoardVO> getList(){


		ArrayList<BoardVO> list = new ArrayList<>();		
		String sql = "select * from board order by bno desc";

		try {

			conn = DriverManager.getConnection(URL,UID,UPW);
			pstmt = conn.prepareStatement(sql);

			rs = pstmt.executeQuery();

			while(rs.next()) {
				int bno = rs.getInt("bno");
				String writer = rs.getString("writer");
				String title = rs.getString("title");
				String content = rs.getString("content");
				Timestamp regdate = rs.getTimestamp("regdate");
				int hit = rs.getInt("hit");

				BoardVO vo = new BoardVO(bno, writer, title, content, regdate, hit);
				list.add(vo);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, pstmt, rs);
		}

		return list;
	}

 

VO객체 리스트를 받아올 Service method 생성 

 

어제 로직과 동일하게 객체를 받아서 저장해 줄 수 있는 리스트를 리턴하는 메서드를 생성한다.

public ArrayList<BoardVO> getList(HttpServletRequest request, HttpServletResponse response) {


		ArrayList<BoardVO> list = dao.getList();

		return list;
	}

 

Controller를 통해 리스트 페이지 이동

아래 주소와 같은 경로의 URL인 경우네는 VO리스트에 service 메서드 getlist로 담아준다.

if(command.equals("/board/board_list.board")) {//목록화면
			
			
			//조회 메서드
			ArrayList<BoardVO> list = service.getList(request, response);
			request.setAttribute("list", list);
			
			request.getRequestDispatcher("board_list.jsp").forward(request, response);

 

리스트 페이지 코드

위에서 list의 정보를 넘겨줘서 아래 페이지에 EL코드를 통해 값을 출력해준다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<%@taglib uri ="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%@include file="../include/header.jsp"%>

	
	<div class="container">
		<h3>My Web게시판</h3>

		<table class="table table-bordered">
			<thead>
				<tr>
				
					<th>순서</th>
					<th>글 번호</th>
					<th>작성자</th>
					<th>제목</th>
					<th>날짜</th>
					<th>조회수</th>
				</tr>
			</thead>

			<tbody><!--몸체  -->
			<c:forEach var="vo" items="${list }" varStatus="num">
				<tr>
				
					<td>${num.count }</td>
					<td>${vo.bno }</td>
					<td>${vo.writer }</td>
					<td><a href="board_content.board?bno=${vo.bno }">${vo.title }</a></td>
					<td><fmt:formatDate value="${vo.regdate }" pattern="yyyy-MM-dd HH시mm분ss초"/></td>
					<td>${vo.hit }</td>
				</tr>
			</c:forEach>
			</tbody>
			
			<tbody>
				<tr>
					<td colspan="6" align="right">
						<form action="" class="form-inline" >
						  <div class="form-group">
						    <input type="text" name="search" placeholder="제목검색" class="form-control" >
						  	<input type="submit" value="검색" class="btn btn-default">
							<input type="button" value="글 작성" class="btn btn-default"  onclick="location.href='board_write.board'">
						  </div>
						</form> 
					</td>
				</tr>
			</tbody>
		
		</table>
	</div>


<%@include file="../include/footer.jsp"%>

리스트 페이지 결과창

아래와 같이 결과창이 출력된다.

반응형

댓글