티스토리 뷰

지난 글에서 write.jsp를 통해 데이터를 입력받고 DB에 적재하는 것을 구축했다.

그럼 이제 그 글을 읽어보자. 아차차 우리는 아직 확인할수가 없다

 

그럼 어떻게해?

그래서 이번에는 목록 조회를 구현해보고자 한다.

 

순서대로

DAO에 데이터를 꺼내오는 기능을 추가 -> BoardListCommand 생성 -> 컨트롤러에 길 뚫어주기 -> list.jsp 만들기

해보자

 

  public ArrayList<BoardDto> selectBoardList() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ArrayList<BoardDto> list = new ArrayList<>();
    //데이터를 담아올 바구니
    ResultSet rs = null;

    //실행할 쿼리문
    String sql = "SELECT * FROM board";

    try {
      conn = ConnectionTest.getConnection();
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      
      while (rs.next()) {
        BoardDto dto = new BoardDto();
        //DB에서 가져온 값을 DTO에 넣어주기
        dto.setId(rs.getLong("id"));
        dto.setCategory(rs.getString("category"));
        dto.setTitle(rs.getString("title"));
        dto.setWriter(rs.getString("writer"));
        
        //다채운  DTO를 리스트에 넣어줌
        list.add(dto);
      }
    } catch (Exception e) {
      e.printStackTrace();
      //닫아주기
    } finally {
      try {
        if (rs != null) rs.close();
        if (pstmt != null) pstmt.close();
        if (conn != null) conn.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return list;
  }

먼저 DAO에 데이터를 꺼내오는 기능을 추가했다

 

public class BoardListCommand implements BoardCommand {
  
  @Override
  public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

    //DAO를 통해 DB에서 글 목록 가져오기
    BoardDao dao = BoardDao.getInstance();
    ArrayList<BoardDto> list = dao.selectBoardList();

    //가져온 리스트를 request 객체에 저장하여 JSP로 전달
    request.setAttribute("list", list);
  }
}

두번째로 BoardListCommand를 만들었다

 

 

else if (commandPath.equals("/list.do")) {
        command = new BoardListCommand(); //게시글 목록
        command.execute(request, response); //실행
        request.getRequestDispatcher("list.jsp").forward(request, response); //실행 후 목록 페이지로 이동
      }

다음으로는 컨트롤러에 길을 뚫어줘야하는데 

전에 작성한 write.do 아래에 작성해주면 된다

 

이제 list.jsp를 작성해야하는데 막막하다

    implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api:3.0.0'
    implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl:3.0.0'

먼저 이 두 의존성을 추가해주자

자바 코드없이 HTML처럼 깔끔하게 반복문을 처리할수 있는 <c:forEach> 라는 기능을 사용할수 있는 JSTL 라이브러리라는 것이다

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>게시글 목록</title>
</head>
<body>
<h2>게시글 목록</h2>
<table>
    <tr>
        <th>번호</th>
        <th>카테고리</th>
        <th>제목</th>
        <th>작성자</th>
    </tr>
<c:forEach var="dto" items="${list}">
    <tr>
        <td>${dto.id}</td>
        <td>${dto.category}</td>
        <td>${dto.title}</td>
        <td>${dto.writer}</td>
    </tr>
</c:forEach>
</table>
<br>
<a href="write.jsp">글쓰기</a>
</body>
</html>

list.do를 작성해준다

 

/list.do 경로로 접근하면 투박하지만 게시글 목록이 출력되는것을 확인할수있다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2026/08   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
글 보관함