μμ‘΄μ± μ£Όμ - μ΄λ Έν μ΄μ
1.
ν΄λμ€ νμΌ λ° μμ±
a.
BoardDAO.java
b.
BoardService.java
c.
BoardServiceImpl.java
d.
Board.java
e.
HomeController.java
f.
home.jsp
2.
μ΄λ
Έν
μ΄μ
μΌλ‘ λΉ λ±λ‘
a.
BoardDAO.java
b.
BoardServiceImpl.java
3.
μμ‘΄μ± μλ μ£Όμ
ν΄λμ€ νμΌ μμ±
1.
Board.java
2.
BoardDAO.java
3.
BoardService.java
4.
BoardServiceImpl.java
5.
HomeController.java
Board.java
package com.aloha.spring.dto;
public class Board {
private String title;
private String writer;
private String content;
public Board() {
}
public Board(String title, String writer, String content) {
this.title = title;
this.writer = writer;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Board [title=" + title + ", writer=" + writer + ", content=" + content + "]";
}
}
Java
볡μ¬
BoardDAO.java
@Repository μ ν΄λμ€μ μ§μ νμ¬ DAO μν μ λΉμΌλ‘ λ±λ‘νλ€.
package com.aloha.spring.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.aloha.spring.dto.Board;
@Repository
public class BoardDAO {
public List<Board> list() {
List<Board> boardList = new ArrayList<Board>();
boardList.add(new Board("μ λͺ©01","μμ±μ01","λ΄μ©01"));
boardList.add(new Board("μ λͺ©02","μμ±μ02","λ΄μ©02"));
boardList.add(new Board("μ λͺ©03","μμ±μ03","λ΄μ©03"));
System.out.println("BoardDAO μ€νλ¨...");
return boardList;
}
}
Java
볡μ¬
BoardService.java
package com.aloha.spring.service;
import java.util.List;
import com.aloha.spring.dto.Board;
public interface BoardService {
// κ²μκΈ λͺ©λ‘
public List<Board> list();
}
Java
볡μ¬
BoardServiceImpl.java
package com.aloha.spring.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aloha.spring.dao.BoardDAO;
import com.aloha.spring.dto.Board;
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardDAO boardDAO;
@Override
public List<Board> list() {
return boardDAO.list();
}
}
Java
볡μ¬
HomeController.java
package com.aloha.spring;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.aloha.spring.dto.Board;
import com.aloha.spring.service.BoardService;
import com.aloha.spring.service.BoardServiceImpl;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private BoardService boardService;
@Autowired
public HomeController(BoardService boardService) {
this.boardService = boardService;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
// κ²μκΈ λͺ©λ‘ μ‘°ν μμ²
List<Board> boardList = boardService.list();
model.addAttribute("boardList", boardList);
if( boardList != null )
for (Board board : boardList) {
logger.info("board - " + board);
}
return "home";
}
}
Java
볡μ¬
home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
<hr>
<table border="1">
<tr>
<th>μ λͺ©</th>
<th>μμ±μ</th>
<th>λ΄μ©</th>
</tr>
<c:forEach var="board" items="${ boardList }" >
<tr>
<td>${ board.title }</td>
<td>${ board.writer }</td>
<td>${ board.content }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
HTML
볡μ¬
@Service λ₯Ό ν΄λμ€μ μ§μ νμ¬ μλΉμ€ μν μ λΉμΌλ‘ λ±λ‘νλ€.