-
1일차(2) - 게시물 목록프로젝트 일지/Spring 프로젝트- 헬스 커뮤니티 2023. 2. 20. 19:29
postList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>게시물 목록</title> <style> td{ text-align: center; } </style> </head> <body> <h1>헬스 커뮤니티</h1> <br><br> <a href="">로그아웃</a> <a href="">마이페이지</a> <br><br> <c:if test="${fn:length(postList) == 0}"> <table width=500> <tr> <th>NO.</th> <th>제목</th> <th>글쓴이</th> <th>조회수</th> <th>등록일</th> </tr> <tr> <td colspan="5">게시물이 없습니다.</td> </tr> </table> </c:if> <form action="" method="get"> <c:if test="${fn:length(postList) > 0}"> <table width=500> <tr> <th>NO.</th> <th>제목</th> <th>글쓴이</th> <th>조회수</th> <th>등록일</th> </tr> <c:forEach var="post" items="${postList}"> <tr> <td>${post.idx}</td> <td><a href="">${post.postTitle}</a></td> <td>${post.memberId}</td> <td>${post.readCount}</td> <td><fmt:formatDate value="${post.postDate}" pattern="yyyy-MM-dd"/></td> </tr> </c:forEach> </table> </c:if> <br> <input type="submit" value="새 게시물 등록"> </form> </body> </html>
PostController.java
@Controller public class PostController { @Autowired private PostService postService; /* basic (로그인 창으로 이동)*/ /* 게시물 목록 (홈) */ @RequestMapping(value = "/list", method = RequestMethod.GET) public ModelAndView home(@RequestParam(defaultValue = "1") int currentPage) { ModelAndView mav = new ModelAndView(); String url = "./post/postList"; List<PostVO> postList = postService.searchAll(); mav.addObject("postList", postList); mav.setViewName(url); return mav; } }
PostService.java
@Service public class PostService { @Autowired private PostDAO postDAO; /* 게시물 목록 (홈) */ public List<PostVO> searchAll() { List<PostVO> postList = null; try { postList = postDAO.selectAll(); } catch (SQLException e) { e.printStackTrace(); } return postList; } }
PostDAO.java
@Repository public class PostDAO { @Autowired private DataSource dataSource; private Connection con; private Statement stmt; private PreparedStatement pstmt; private ResultSet rs; private String sql; /* 게시물 목록 (홈) */ public List<PostVO> selectAll() throws SQLException { sql = "select *, row_number() over(order by post_id desc) from post"; con = dataSource.getConnection(); stmt = con.createStatement(); rs = stmt.executeQuery(sql); List<PostVO> postList = new ArrayList<PostVO>(); while(rs.next()) { PostVO post = new PostVO(rs.getInt(9), rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getString(7), rs.getTimestamp(8)); postList.add(post); } ConnectionManager.closeConnection(rs, stmt, con); return postList; } }
ConnectionManager.java
public class ConnectionManager { public static void closeConnection(ResultSet rs, Statement stmt, Connection con) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
PostVO.java
public class PostVO { private int idx; // DB테이블에 없음. 목록용 private int postId; // 기본키 private String postTitle; private String postContent; private int readCount; private String imgAttachment; private String dataAttachment; private Timestamp postDate; private String memberId; public PostVO() { } public PostVO(int idx, int postId, String memberId, String postTitle, String postContent, int readCount, String imgAttachment, String dataAttachment, Timestamp postDate) { super(); this.idx = idx; this.postId = postId; this.memberId = memberId; this.postTitle = postTitle; this.postContent = postContent; this.readCount = readCount; this.imgAttachment = imgAttachment; this.dataAttachment = dataAttachment; this.postDate = postDate; } public PostVO(int postId, String postTitle, String memberId, String postContent, int readCount, String imgAttachment, String dataAttachment, Timestamp postDate) { super(); this.postId = postId; this.memberId = memberId; this.postTitle = postTitle; this.postContent = postContent; this.readCount = readCount; this.imgAttachment = imgAttachment; this.dataAttachment = dataAttachment; this.postDate = postDate; } public int getIdx() { return idx; } public void setIdx(int idx) { this.idx = idx; } public int getPostId() { return postId; } public void setPostId(int postId) { this.postId = postId; } public String getPostTitle() { return postTitle; } public void setPostTitle(String postTitle) { this.postTitle = postTitle; } public String getPostContent() { return postContent; } public void setPostContent(String postContent) { this.postContent = postContent; } public int getReadCount() { return readCount; } public void setReadCount(int readCount) { this.readCount = readCount; } public String getImgAttachment() { return imgAttachment; } public void setImgAttachment(String imgAttachment) { this.imgAttachment = imgAttachment; } public String getDataAttachment() { return dataAttachment; } public void setDataAttachment(String dataAttachment) { this.dataAttachment = dataAttachment; } public Timestamp getPostDate() { return postDate; } public void setPostDate(Timestamp postDate) { this.postDate = postDate; } public String getMemberId() { return memberId; } public void setMemberId(String memberId) { this.memberId = memberId; } @Override public String toString() { return "PostVO [idx=" + idx + ", postId=" + postId + ", postTitle=" + postTitle + ", postContent=" + postContent + ", readCount=" + readCount + ", imgAttachment=" + imgAttachment + ", dataAttachment=" + dataAttachment + ", postDate=" + postDate + "]"; } }
이제 페이징을 추가해보자!
'프로젝트 일지 > Spring 프로젝트- 헬스 커뮤니티' 카테고리의 다른 글
최종 결과물 - <헬스 커뮤니티> (0) 2023.02.27 3일차 - 게시물 검색 (0) 2023.02.22 2일차 - 게시물 목록의 페이징 처리 (0) 2023.02.21 1일차(1) - 프로젝트 설계 <헬스 커뮤니티> (0) 2023.02.20