Search

์˜์กด์„ฑ ์ฃผ์ž… - XML

์˜์กด์„ฑ ์ฃผ์ž… - 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
๋ณต์‚ฌ