์์กด์ฑ ์ฃผ์ - XML
1.
ํด๋์ค ํ์ผ ๋ฐ ์์ฑ
a.
BoardDAO.java
b.
BoardService.java
c.
BoardServiceImpl.java
d.
Board.java
e.
HomeController.java
f.
home.jsp
2.
root-context.xml ์์ ๋น ๋ฑ๋ก
a.
BoardDAO.java
b.
BoardServiceImpl.java
i.
BoardDAO ๋น์ ์ฐธ์กฐํ์ฌ, ๋น์ ๋ฑ๋ก
3.
์์กด์ฑ ์๋ ์ฃผ์
ํด๋์ค ํ์ผ ์์ฑ
1.
BoardDAO.java
2.
BoardService.java
3.
BoardServiceImpl.java
4.
Board.java
5.
HomeController.java
BoardDAO.java
package com.aloha.spring.dao;
import java.util.ArrayList;
import java.util.List;
import com.aloha.spring.dto.Board;
public class BoardDAO {
// ๋น์ผ๋ก ๋ฑ๋ก๋ ๋, ์์ฑ๋ ๊ธฐ๋ณธ ์์ฑ์ ์ ์
public 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 com.aloha.spring.dao.BoardDAO;
import com.aloha.spring.dto.Board;
public class BoardServiceImpl implements BoardService {
// BoardServiceImpl --> BoardDAO ์ ์์กด
private BoardDAO boardDAO;
// ์์กด์ฑ ์๋ ์ฃผ์
// - BoardDAO ๋น์ ์ฐธ์กฐํ๋ค๋ฉด,
// <bean><constructor-arg ref="boardDAO" /></bean>
@Autowired
public BoardServiceImpl(BoardDAO boardDAO) {
this.boardDAO = boardDAO;
}
// @Autowired
// public BoardServiceImpl(int test) {
// this.test = test;
// }
@Override
public List<Board> list() {
return boardDAO.list();
}
}
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
๋ณต์ฌ
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
๋ณต์ฌ