레이어드 아키텍처(Layered Architecture) 실습5

oyeon·2021년 1월 27일
0
  • 최종 Directory 구조

Controller 부분 실습

  1. GuestbookController.java
package kr.or.connect.guestbook.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import kr.or.connect.guestbook.dto.Guestbook;
import kr.or.connect.guestbook.service.GuestbookService;

@Controller
public class GuestbookController {
	@Autowired
	GuestbookService guestbookService;
	
	@GetMapping(path="/list")
	public String list(@RequestParam(name="start", required=false, defaultValue="0") int start,
			ModelMap model){
		// start로 시작하는 방명록 목록 구하기
		List<Guestbook> list = guestbookService.getGuestbooks(start);
		
		// 전체 페이지 수 구하기
		int count = guestbookService.getCount();
		int pageCount = count / GuestbookService.LIMIT;
		if(count % GuestbookService.LIMIT > 0)
			pageCount++;
		
		// 페이지 수 만큼 start의 값을 리스트로 저장
		// ex. 페이지 수가 3이면 0, 5, 10 이 저장
		// list?start=0, list?start=5, list?start=10 으로 링크 걸림
		List<Integer> pageStartList = new ArrayList<>();
		for(int i = 0; i < pageCount; i++){
			pageStartList.add(i * GuestbookService.LIMIT);
		}
		model.addAttribute("list", list);
		model.addAttribute("count", count);
		model.addAttribute("pageStartList", pageStartList);
		
		return "list";
	}
	
	@PostMapping(path="/write")
	public String write(@ModelAttribute Guestbook guestbook,
			HttpServletRequest request){
		String clientIp = request.getRemoteAddr();
		System.out.println("clientIp:" + clientIp);
		guestbookService.addGuestbook(guestbook, clientIp);
		return "redirect:list";
	}
}

JSP 부분 실습

  1. 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>
<h1>방명록</h1><br>
<h4>방명록 전체 수 : ${count}</h4><br>

<c:forEach items="${list}" var="guestbook">
	${guestbook.id} <br>
	${guestbook.name} <br>
	${guestbook.content} <br>
	${guestbook.regdate} <br>
</c:forEach>
<br>

${pageStartList }<br>
<c:forEach items="${pageStartList}" var="pageIndex" varStatus="status">
		<a href="list?start=${pageIndex}">${status.index + 1 }</a>&nbsp; &nbsp;
</c:forEach>
<br><br>
<form method="post" action="write">
	name : <input type="text" name="name"><br>
	<textarea name="content" cols="60" rows="6"></textarea><br>
	<input type="submit" value="등록">
</form>
</body>
</html>

결과

profile
Enjoy to study

0개의 댓글