이전 글에서 글쓰기 까지 구현 하였고, 이번 게시글에서는 글 목록 구현을 하겠습니다.

먼저 목록에 접근하기위한 링크를 메인화면에 삽입하겠습니다


index.jsp의 수정


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

<title>Index</title>
</head>
<body>
Login ID:
<c:out value="${sessionScope.user_id }" default="null"/>
<table border="1" align="center" width="500" height="400">
	<tr>
		<td colspan="4" height="100" align="center">Main page</td>
	</tr>
	<tr>
		<td colspan="4" height="100" align="center"><a href="write">글 쓰기</a></td>
	</tr>
	<tr>
		<td colspan="4" height="100" align="center"><a href="bbs">글 목록</a></td>
	</tr>
	<tr>
	<c:if test="${empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Login">로그인</a></td>
	</c:if>
	
	<c:if test="${!empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Logout">로그아웃</a></td>
	</c:if>
		<td colspan="2" align="center" width="250"><a href="Register">회원가입</a></td>
	</tr>
</table>
</body>
</html>

Controller/BbsController - bbs 메소드 작성


게시글 목록 조회에 대한 처리를 해주는 메소드 bbs를 작성합니다.

	@RequestMapping("/bbs")
	public String bbs(Model model) {
		
		BDao dao = sqlSession.getMapper(BDao.class);
		ArrayList<BbsDto> list = dao.list();
		model.addAttribute("list",list);
		return "bbs";
	}

model.addAttribute("list", list) 는 DB조회를 통해 생성된 list 객체 배열을 jsp에서 쓰기위해 속성으로 추가해주는 과정입니다.


Dao/BDao, BbsDao - DB 조회를 위한 list 메소드 추가


BDao에 list 메소드를 추가합니다.

package com.app.app.Dao;

import java.util.ArrayList;
import com.app.app.Dto.BbsDto;
import java.util.Map;
import com.app.app.Dto.UserDto;

public interface BDao {
	public void Write(Map map);
	public ArrayList<BbsDto> list();
}

mapper/Bdao.xml 수정


게시글 조회를 위한 쿼리를 추가해줍니다.

	<select id="list" resultType="com.app.app.Dto.BbsDto">
	SELECT *
		FROM bbs
	</select>
	

JSTL 을 이용한 bbs.jsp 작성


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<table width=80% height=70% border=1 align=center>
	<tr>
	<th width=10% align="">글 번호</th>
	<th width=20%>작성자</th>
	<th width=40%>글 제목</th>
	<th width=30%>작성일</th>
	</tr>
	<c:forEach var="bbs" items="${list }">
	<tr align="center">
	<td> <c:out value="${bbs.bbs_id }"/></td>
	<td> <c:out value="${bbs.author }"/></td>
	<td align="left"><a href='bbs/<c:out value="${bbs.bbs_id }"/>'> <c:out value="${bbs.bbs_title }"/></a></td>
	<td> <c:out value="${bbs.date }"/></td>
	</tr>
	</c:forEach>
	<tr>
	</tr>
</table>
</body>
</html>

 

위의 <c:forEach var="bbs" items="${list}"> ~ 의 코드는 JSTL tag로서 forEach는 for 문이라고 생각하면 된다.

bbsController에서 게시글 목록조회하여 속성으로 심어준 ArrayList<BbsDto> list 객체를 화면에 순서대로 뿌려주는 역할을 한다.

JSTL을 잘 이용하면 코드 가독성이 높아지고 간결화된다.

예를 들면 위의 코드를 스크립트릿을 이용하여 작성하면 이렇게 된다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.app.app.Dto.BbsDto" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<table width=80% height=70% border=1 align=center>
	<tr>
	<th width=10% align="">글 번호</th>
	<th width=20%>작성자</th>
	<th width=40%>글 제목</th>
	<th width=30%>작성일</th>
	</tr>
	<%
	ArrayList<BbsDto> bbs = new ArrayList<BbsDto>();
	bbs = (ArrayList<BbsDto>)request.getAttribute("list");
	for(int i=0; i<bbs.size(); i++){
	%>
	<tr align="center">
	<td> <%=bbs.get(i).getBbs_id() %>
	<td> <%=bbs.get(i).getAuthor() %>
	<td align="left"><a href='bbs/<%=bbs.get(i).getBbs_id()%>'> <%=bbs.get(i).getBbs_title() %></a></td>
	<td> <%=bbs.get(i).getDate() %></td>
	</tr>
	<%
	}
	%>
    <tr>
	</tr>
</table>
</body>
</html>

java.util.ArrayList 와 com.app.app.Dto.BbsDto를 따로 import 해주어야 하고 같은 기능을 하는 코드인데 더 많은 코드를 요구로한다.


이제 게시글을 작성하고 확인해보자.

위와 같이 나온다면 제대로 한것이다.

 

'WEB > Spring' 카테고리의 다른 글

11) Spring - 게시글 수정  (0) 2019.11.18
10) Spring - 게시글 보기 구현  (0) 2019.11.01
8) Spring - 게시판 글쓰기 구현  (0) 2019.10.12
7) Spring - 로그아웃  (0) 2019.10.12
6) Spring - 로그인(Session 이용)  (0) 2019.10.08

+ Recent posts