본문 바로가기

Java, Spring/09일

9일차 4 - 게시판 List구현 & JSTL

BoardList 할거야.

1. Bean 설정파일(dispatcher-servlet.properties) 에 등록

#로그인 폼
/Login.do=controller.Login

#로그인 확인
/LoginAction.do=controller.LoginAction

#게시판 리스트
/BoardList.do=controller.BoardList

#게시판 글쓰기 폼
/BoardInsert.do=controller.BoardInsert

#게시판 글쓰기 데이터를 DB에 저장
/BoardInsertAction.do=controller.BoardInsertAction

#게시판 상세보기	
/BoardView.do=controller.BoardView

#글수정 폼	
/BoardUpdate.do=controller.BoardUpdate

2. action class 만들어 AbstractController 상속받는 걸로

package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import board.model.BoardDao;
import board.model.BoardDto;

public class BoardList extends AbstractController {

	@Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) {

		//DB에 접속해서 현재 게시판의 리스트를 읽어와야지
		BoardDao  boardDao = new BoardDao();
		List<BoardDto> list = boardDao.getList();	//받아와
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/WEB-INF/view/list.jsp");	//다음이동할 곳
		mav.addObject("list", list);				//거기 뿌릴 데이터
		
		return mav;
	}
}

3. list.jsp만들고 데이터뿌리면 돼.

<%@page import="board.model.BoardDto"%>
<%@page import="java.util.List"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>게시판 리스트</title>
</head>
<%
	List<BoardDto> list = (List<BoardDto>)request.getAttribute("list");		
%>
<body>
<table>
	<caption>게시판 리스트</caption>
	<tr>
		<th>번호</th>
		<th>제목</th>
		<th>이름</th>
		<th>날짜</th>	
		<th>조회수</th>	
	</tr>
<% /*//예전문법 
	for(int i = 0; i <list.size(); i++) {
		BoardDto boardDto = (BoardDto)list.get(i);  */		
	for(BoardDto boardDto:list)		{	//List에서 하나씩 꺼내서 dto에 집어넣어. 
										//더이상 없을 때까지(java1.5부터) 
										//이렇게 하기위해선 반드시 제네릭<>해줘야해..
%>
	<tr>
		<td><%=boardDto.getSeq() %></td>
		<td><a href="content.jsp?seq=<%=boardDto.getSeq()%>"><%=boardDto.getTitle() %></a></td>
		<td><%=boardDto.getName() %></td>
		<td><%=boardDto.getWriteday() %></td>
		<td><%=boardDto.getReadcount() %></td>
	</tr>
<%	} %>
</table>		
<a href="BoardInsert.do">글쓰기</a>
</body>
</html>

 

어렵지 않지..?

 

근데 list.jsp에 for문으로 루프돌리니 프로그램 코드가 들어가잖아..

java standard tag library사용해서 없애보자..

http://jstl.java.net/

Download JSTL

JSTL API : javax.servlet.jsp.jstl-api-1.2.1.jar
JSTL Implementation :javax.servlet.jsp.jstl-1.2.1.jar 다운받아

WEB-INF\lib에 드래그앤드롭 해주면 돼.

 

이제 list.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>게시판 리스트</title>
</head>
<body>
<table width="500" border="1" cellspacing="0">
	<caption>게시판 리스트</caption>
	<tr>
		<th>번호</th>
		<th>제목</th>
		<th>이름</th>
		<th>날짜</th>	
		<th>조회수</th>	
	</tr>
	<c:forEach items="${list}" var="boardDto">
	<tr>
		<td>${boardDto.seq}</td>
		<td><a href="content.jsp?seq=${boardDto.seq}">${boardDto.title}</a></td>
		<td>${boardDto.name}</td>
		<td>${boardDto.writeday}</td>
		<td align="right">${boardDto.readcount}</td>
	</tr>
	</c:forEach>
</table>		
<a href="BoardInsert.do">글쓰기</a>
</body>
</html>

line 3 : 보통 tag library쓰게 되면 앞에 접두어를 붙여 기존의 tag들과 차이를 줘야해.

우리가 지금 쓸게 루프돌리는.. jstl의 core 라이브러리를 쓸거야. 그래서 약자로 “c”를 주자.

uri는 “”사이에 커서를 두고 crtl-space치면 우리가 쓸 수 있는 라이브러리가 펼쳐져.. “http://java.sun.com/jsp/jstl/core” 선택..

line 20 :  <c:forEach items="${list}" var="boardDto"> : addObject한 list를 받아와 루프돌려서 boardDto에 담아.. 없을 때까지..

line 22: ${boardDto.seq} 이렇게 주면 알아서 boardDto의 getSeq()를 호출해 받아와.

 

이제 루프돌리는 거 빼면 자바코드 모두 제거되었네…

 

날짜에 시분초까지 나올 필요없잖아..

쿼리문 수정해주자..

 
SELECT seq, title, name, writeday, readcount
FROM board 
ORDER BY seq DESC 


SELECT seq, title, name, to_char(writeday, 'YYYY/MM/DD') as writeday, readcount
FROM board 
ORDER BY seq DESC