Search

μ˜μ‘΄μ„± μ£Όμž… - μ–΄λ…Έν…Œμ΄μ…˜

μ˜μ‘΄μ„± μ£Όμž… - μ–΄λ…Έν…Œμ΄μ…˜

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 λ₯Ό ν΄λž˜μŠ€μ— μ§€μ •ν•˜μ—¬ μ„œλΉ„μŠ€ μ—­ν• μ˜ 빈으둜 λ“±λ‘ν•œλ‹€.