[스프링]MVC2방식 CRUD 게시판 만들기 8.게시판 목록페이지 구현
1. 선행작업
WAS 구동시 home.jsp 페이지에서 게시판 목록(listAll.jsp)으로 가는 기능 구현
1-1. home.jsp 수정
(form을 사용해서 게시판 이동 버튼 생성, 상단에 한글필터 적용)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<form action = "listAll" method="get">
<body>
<button type ="submit">CRUD게시판 가기</button>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</form>
</html>
2. listAll.jsp 만들기
2-1. \src\main\webapp\WEB-INF\views 디렉토리에 listAll.jsp 생성
(view 폴더 우클릭 -> New -> Other...)
(JSP 검색 -> JSP File)
(File name : listAll.jsp)
(최종 구성)
2-2. listAll.jsp 구현
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page session="false"%>
<!DOCTYPE html>
<html>
<head>
<title>게시판 목록</title>
</head>
<form action = "regist" method = "get">
<body>
<table border="1" width="880">
<tr>
<td width="77">
<p align="center">글번호</p>
</td>
<td width="327">
<p align="center">제목</p>
</td>
<td width="197">
<p align="center">작성자</p>
</td>
<td width="155">
<p align="center">작성일</p>
</td>
<td width="90">
<p align="center">조회수</p>
</td>
</tr>
<c:forEach items="${list}" var="boardVO">
<tr>
<td>${boardVO.bno}</td>
<td><a href='/read?bno=${boardVO.bno}'>${boardVO.title}</a></td>
<td>${boardVO.writer}</td>
<td><fmt:formatDate pattern="yyyy-MM-dd HH:mm"
value="${boardVO.regdate}" /></td>
<td><span class="badge bg-red">${boardVO.viewcnt}</span></td>
</tr>
</c:forEach>
</table>
<button type ="submit">글쓰기</button>
</body>
</form>
</html>
3. Controller 클래스 작성
3-1. BoardController 클래스 생성
(controller 패키지 우클릭 -> New -> Class)
-- 클래스 네임 : BoardController
3-2. Controller 작성
package com.myp.controller;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.myp.service.BoardService;
@Controller // 컨트롤러임을 명시
@RequestMapping(value = "/") // 주소 패턴
public class BoardController {
@Inject // 주입(심부름꾼) 명시
private BoardService service; // Service 호출을 위한 객체생성
@RequestMapping(value= "/listAll", method = RequestMethod.GET) // 주소 호출 명시 . 호출하려는 주소 와 REST 방식설정 (GET)
public void listAll(Model model)throws Exception { // 메소드 인자값은 model 인터페이스(jsp전달 심부름꾼)
model.addAttribute("list",service.listAll()); // jsp에 심부름할 내역(서비스 호출)
}
}
3-3. WAS의 기본 주소값 변경
- http://localhost:8080/controller -> http://localhost:8080/
(톰캣서버 -> Modules)
(Edit... -> Path값을 / )
4. 최종 화면