728x90
MVC Pattern (Model-View-Controller)
MVC패턴이란
MVC 는 Model, View, Controller의 약자로, 프로젝트를 구성할 때 그 구성요소를 세가지의 역할로 구분한 패턴이다.
- Model : Logic(Business & DB)을 처리하기 위한 모든 것, controller로 부터 넘어온 data를 이용하여 이를 수행하고 그에 대한 결과를 다시 conteoller에 return한다. (DAO, Service)
- View : 모든 화면 처리를 담당. Client의 요청에 대한 결과뿐 아니라 controller에 요청을 보내는 화면단도 jsp에서 처리한다. Logic처리를 위한 java code는 사라지고 결과 출력을 위한 code만 존재 (JSP)
- Controller : Client의 요청을 분석하여 Logic처리응 위한 Model단을 호툴한다. return 받은 결과data를 필요에 따라 request, session등에 저장하고 redirect 또는 forward 방식으로 jsp(view) page를 이용하여 출력한다. (Servlet)
[실습예제]
서점 도서관리프로그램 (Book 객체 CURD)
BookVO.java
package vo;
import java.io.Serializable;
public class BookVO implements Serializable{
private int bookno; // NUMBER(4) PRIMARY KEY,
private String title; // VARCHAR2(40),
private String publisher; // VARCHAR2(40),
private int price; // NUMBER(8)
public BookVO() { }
public BookVO(int bookno, String title, String publisher, int price) {
this.bookno = bookno;
this.title = title;
this.publisher = publisher;
this.price = price;
}
public int getBookno() {
return bookno;
}
public void setBookno(int bookno) {
this.bookno = bookno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "BookVO [bookno=" + bookno + ", title=" + title + ", publisher=" + publisher + ", price=" + price + "]";
}
}
BooKDAO.java
package dao;
import java.util.List;
import vo.BookVO;
public interface BookDAO {
List<BookVO> bookList();
void bookAdd(BookVO vo);
void deleteBook(int bookno);
void updateBook(BookVO vo);
BookVO getBook(int no) ;
List<BookVO> searchBook(String condition,String keyword);
}
BookDAOImple.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.DBUtil;
import vo.BookVO;
public class BookDAOImpl implements BookDAO{
public List<BookVO> bookList() {
List<BookVO> list = new ArrayList<BookVO>();
String sql = "select * from book order by bookno desc";
//"select * from book order by bookno desc limit 1 , 3"
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
BookVO b = new BookVO();
b.setBookno(rs.getInt("bookno"));
b.setPrice(rs.getInt("price"));
b.setPublisher(rs.getString("publisher"));
b.setTitle(rs.getString("title"));
list.add(b);
}
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
return list;
}
public void bookAdd(BookVO vo) {
String sql = "insert into book (title,publisher,price) " +
"values (?,?,?)";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, vo.getTitle());
ps.setString(2, vo.getPublisher());
ps.setInt(3, vo.getPrice());
int i = ps.executeUpdate();
if(i == 0) throw new Exception("등록 실패");
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
}
public BookVO getBook(int no) {
String sql = "select * from book where bookno = ?";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
BookVO b = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1, no);
rs = ps.executeQuery();
while(rs.next()) {
b = new BookVO();
b.setBookno(rs.getInt("bookno"));
b.setPrice(rs.getInt("price"));
b.setPublisher(rs.getString("publisher"));
b.setTitle(rs.getString("title"));
}
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
return b;
}
public void deleteBook(int bookno) {
String sql = "delete from book where bookno = ?";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1, bookno);
int i = ps.executeUpdate();
if(i == 0) throw new Exception("삭제 오류 발생 ");
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
}
@Override
public void updateBook(BookVO vo) {
// TODO Auto-generated method stub
String sql = "UPDATE Book SET price = ? WHERE bookno = ?";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int result = 0;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
// ? 세팅
ps.setInt(1, vo.getPrice());
ps.setInt(2, vo.getBookno());
//실행 및 결과값 핸들링
result = ps.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
}
public List<BookVO> searchBook(String condition, String keyword) {
List<BookVO> list = new ArrayList<BookVO>();
String sql = "select * from Book where "+condition+" like ? order by bookno desc";
//select * from Book where publisher like '%한%' order by bookno desc;
//select * from Book where publisher like concat('%','한','%') order by bookno desc;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1,"%"+ keyword+"%");
rs = ps.executeQuery();
while (rs.next()) {
BookVO vo = new BookVO();
vo.setBookno(rs.getInt("bookno"));
vo.setTitle(rs.getString("title"));
vo.setPublisher(rs.getString("publisher"));
vo.setPrice(rs.getInt("price"));
list.add(vo);
}
} catch (Exception e) {
System.out.println(e);
}finally {
DBUtil.close(rs,ps,con);
}
return list;
}
}
BookService.java
package service;
import java.util.List;
import vo.BookVO;
public interface BookService {
List<BookVO> bookList();
void bookAdd(BookVO vo);
void deleteBook(int bookno);
void updateBook(BookVO vo);
BookVO getBook(int no) ;
List<BookVO> searchBook(String condition,String keyword);
}
BookServiceImpl.java
package service;
import java.util.List;
import dao.BookDAO;
import vo.BookVO;
public class BookServiceImpl implements BookService{
private BookDAO dao = null;
public BookServiceImpl() { }
public BookServiceImpl(BookDAO dao) {
this.dao = dao;
}
public BookDAO getDao() {
return dao;
}
public void setDao(BookDAO dao) {
this.dao = dao;
}
@Override
public List<BookVO> bookList() {
return dao.bookList();
}
@Override
public void bookAdd(BookVO vo) {
dao.bookAdd(vo);
}
@Override
public void deleteBook(int bookno) {
// TODO Auto-generated method stub
dao.deleteBook(bookno);
}
@Override
public void updateBook(BookVO vo) {
// TODO Auto-generated method stub
dao.updateBook(vo);
}
@Override
public List<BookVO> searchBook(String condition, String keyword) {
// TODO Auto-generated method stub
return dao.searchBook(condition, keyword);
}
@Override
public BookVO getBook(int no) {
// TODO Auto-generated method stub
return dao.getBook(no);
}
}
ListBookServlet.java (도서조회)
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BookDAO;
import dao.BookDAOImpl;
import service.BookService;
import service.BookServiceImpl;
import vo.BookVO;
@WebServlet({ "/ListBookServlet", "/bookList.do" })
public class ListBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
BookDAO dao = new BookDAOImpl();
BookService service = new BookServiceImpl(dao);
List<BookVO> list = service.bookList();
request.setAttribute("booklist", list);
String page = "/booklist.jsp";
getServletContext().getRequestDispatcher(page).forward(request, response);
// request.getRequestDispatcher(page).forward(request, response); .
}
}
booklist.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>
<html>
<head>
<meta charset="UTF-8">
<title>book list</title>
<link rel="stylesheet" href="./css/my.css">
<link rel="stylesheet" href="./css/main.css">
</head>
<body>
<%@ include file="common/header.jsp"%>
<section>
<h1>Book List ${pageContext.request.contextPath}</h1>
<form method="post" action="/ex_00/search.do">
<div style="text-align: center">
<select name = "condition">
<option value ="title">도서제목</option>
<option value ="publisher">출판사</option>
</select>
<input type="text" name="keyword">
<button type="submit">검색</button>
</div>
</form>
<br>
<form method="post" action="/ex_00/deleteBook.do">
<table>
<tr>
<th>bookno</th>
<th>title</th>
<th>publisher</th>
<th>price</th>
<th><button type="submit">삭제</button></th>
</tr>
<c:forEach var="data" items="${booklist}">
<tr>
<td>${data.bookno}</td>
<td><a href="viewBook.do?bookno=${data.bookno}">${data.title}</a>
</td>
<td>${data.publisher}</td>
<td>${data.price}</td>
<td><input type="checkbox" name="bookno"
value="${data.bookno}"></td>
</tr>
</c:forEach>
</table>
</form>
</section>
<%@ include file="common/footer.jsp"%>
</body>
</html>
728x90
'WEB' 카테고리의 다른 글
JSP(Java Server Page) (0) | 2022.03.26 |
---|---|
Cookie(쿠키) (0) | 2022.03.25 |
Servlet (서블릿)이해하기 + 실습예제 (0) | 2022.03.22 |
댓글