---------------글번호로 조회--------------------------------
시작 : list_all.jsp(목록에서 글제목 클릭시) - command=get_content
Ctr : GetContentController.execute();
BoardService : getContentByNO(int no) : BoardDTO
BoardDAO : updateViewCount (int no)
selectContentByNO(int no) : BoardDTO
응답 : show_content.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>전체목록보기</title>
</head>
<style type="text/css">
table{
width : 650px;
border-collapse: collapse;
}
td{
padding: 5px;
}
</style>
<body>
<h2>글목록</h2>
<p>
<table border="1" width="800">
<tr align="center" style="font-weight: bold" bgcolor="yellow">
<td width="65px">글번호</td>
<td width="300">제목</td>
<td width="80px">작성자</td>
<td width="60">작성일</td>
<td width="60">조회수</td>
</tr>
<c:forEach items="${requestScope.list_all }" var="list_all">
<tr align="center">
<td>${list_all.no }</td>
<td><a href="/${initParam.context_root }/boardController?command=get_content&no=${list_all.no}">${list_all.title }</a></td>
<td>${list_all.writer }</td>
<td>${list_all.writedate }</td>
<td>${list_all.viewcount }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
package board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.ForwardDTO;
public class BoardFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BoardFrontController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 공통 사전 작업 - 한글처리
request.setCharacterEncoding("utf-8");
//2. Controller 로직 처리 - Controller객체.execute()호출
String command = request.getParameter("command");
Controller ctr = ControllerCommandMapping.getController(command);
ForwardDTO fdto = ctr.execute(request, response);
//3. 공통 사후 작업
if(fdto.isRedirect()){
response.sendRedirect(fdto.getUrl());
}else{
RequestDispatcher rdp = request.getRequestDispatcher(fdto.getUrl());
rdp.forward(request, response);
}
}
}
package board.controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import board.dto.BoardDTO;
import board.dto.ForwardDTO;
import board.model.service.BoardService;
public class GetContentController implements Controller {
@Override
public ForwardDTO execute(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//1. 비지니스 로직
BoardService boardService = BoardService.getInstance();
ForwardDTO forwardDTO = null;
int no = Integer.parseInt(request.getParameter("no"));
try{
BoardDTO get_content = boardService.getContentByNO(no);
request.setAttribute("board", get_content);
forwardDTO = new ForwardDTO("/show_content.jsp",false);
} catch(SQLException e){
e.printStackTrace();
request.setAttribute("error_message", e.getMessage());
forwardDTO = new ForwardDTO("/error.jsp",false);
}
return forwardDTO;
}
}
package board.controller;
public class ControllerCommandMapping {
public static Controller getController(String command){
Controller ctr = null;
if(command.equals("write_content")){
ctr = new WriteContentController();
}else if(command.equals("list_all")){
ctr = new BoardAllListController();
}else if(command.equals("get_content")){
ctr = new GetContentController();
}
return ctr;
}
}
package board.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import board.dto.BoardDTO;
import board.util.DBUtil;
public class BoardDAO {
private static BoardDAO instance = new BoardDAO();
private DBUtil dbUtil;
private BoardDAO() {
dbUtil = DBUtil.getInstance();
}
public static BoardDAO getInstance() {
return instance;
}
public int selectBoardNo() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select board_no_seq.nextval from dual";
int no = 0;
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
if (rset.next()) {
no = rset.getInt(1);
}
} finally {
dbUtil.close(conn, pstmt, rset);
}
return no;
}
public void insertContent(BoardDTO bto) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into board (no, title, writer, content, writedate, viewcount, refamily, restep, relevel) values(?,?,?,?,?,0,?,?,?)";
try {
BasicDataSource ds = dbUtil.getDataSource();
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bto.getNo());
pstmt.setString(2, bto.getTitle());
pstmt.setString(3, bto.getWriter());
pstmt.setString(4, bto.getContent());
pstmt.setString(5, bto.getWritedate());
//viewcount값은 처음 등록하면 무조건 0이라서 쿼리에서 부터 그냥 값을 넣어줬다
// pstmt.setInt(6, bto.getViewcount());
pstmt.setInt(6, bto.getRefamily());
pstmt.setInt(7, bto.getRestep());
pstmt.setInt(8, bto.getRelevel());
int cnt = pstmt.executeUpdate();
System.out.println(cnt + "개의 행이 삽입되었습니다.");
} finally {
dbUtil.close(conn, pstmt);
}
}
public ArrayList<BoardDTO> selectBoardAllList() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board order by refamily desc";
ArrayList<BoardDTO> list_all = new ArrayList<BoardDTO>();
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
while(rset.next()){
list_all.add(new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9)));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return list_all;
}
public BoardDTO selectContentByNO(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
String sql = "select no, title, writer, content, writedate, viewcount, refamily, restep, relevel from board where no=?";
BoardDTO bto = null;
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
rset = pstmt.executeQuery();
if(rset.next()){
bto = new BoardDTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getString(4), rset.getString(5), rset.getInt(6), rset.getInt(7), rset.getInt(8), rset.getInt(9));
}
}finally{
dbUtil.close(conn, pstmt, rset);
}
return bto;
}
public void updateViewCount(int no) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update board set viewcount=viewcount+1 where no=?";
try{
BasicDataSource dataSource = dbUtil.getDataSource();
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, no);
int cnt = pstmt.executeUpdate();
System.out.println(cnt+"개의 행이 수정되었습니다.");
}finally{
dbUtil.close(conn, pstmt);
}
}
}
package board.model.service;
import java.sql.SQLException;
import java.util.ArrayList;
import board.dto.BoardDTO;
import board.model.dao.BoardDAO;
import board.util.Utilities;
public class BoardService {
private static BoardService instance = new BoardService();
private BoardDAO dao;
private BoardService(){
dao = BoardDAO.getInstance();
}
public static BoardService getInstance(){
return instance;
}
public void writeContent(BoardDTO bto)throws SQLException{
/*
* 1. bto에 property 설정 - no, writedate, content
* no - sequence의 값 - dao.selectBoardNO();
* writedate - Utilities.getNow();
* content를 DB에 넣는 용으로 변경 Utilities.changeContentForDB();
* bdto의 refamily에 no 값을 설정
* 2. dao.insertContent(bto);
* 3. 날짜 형태를 년.월.일 시:분:초 형태로 변경 - bto의 property 변경
* Utilities.changeDayTimeFormat();
*/
//1
int no = dao.selectBoardNo();
String writedate = Utilities.getNow();
bto.setNo(no);
bto.setWritedate(writedate);
bto.setContent(Utilities.changeContentForDB(bto.getContent()));
bto.setRefamily(no);
//2
dao.insertContent(bto);
//3
bto.setWritedate(Utilities.changeDayTimeFormat(writedate));
}
//글목록
public ArrayList<BoardDTO> getBoardAllList() throws SQLException{
//1. dao로 부터 전체 글내용을 ArrayList로 조회
ArrayList<BoardDTO> list_all = dao.selectBoardAllList();
//2. list내의 BoardDTO객체들의 wrtiedate를 변경
for(int i=0;i<list_all.size();i++){
list_all.get(i).setWritedate(Utilities.changeDayFormat(list_all.get(i).getWritedate()));
}
return list_all;
}
public BoardDTO getContentByNO(int no) throws SQLException{
BoardDTO bto = dao.selectContentByNO(no);
if(bto!=null){
dao.updateViewCount(no);
bto.setWritedate(Utilities.changeDayTimeFormat(bto.getWritedate()));
}
return bto;
}
}
<%@page import="board.dto.BoardDTO"%>
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글내용</title>
<style type="text/css">
div{
width:600px;
border:1px solid gray;
padding : 5px;
}
#title{
font-weight: bold;
background-color: yellow;
}
#info{
font-size:13px;
}
#content{
min-height:300px;
height:auto;
}
</style>
</head>
<body>
<h1>글목록</h1>
<p>
<div id="title">${requestScope.board.no}.${requestScope.board.title }</div>
<div id="info">작성자 : ${requestScope.board.writer } | 조회수 : ${requestScope.board.viewcount } 작성일시 : ${requestScope.board.writedate }</div>
<div id="content">${requestScope.board.content }</div>
<p>
<a href="/${initParam.context_root }/write_form.jsp">글쓰기</a>
<a href="/${initParam.context_root }/boardController?command=list_all">전체 글목록</a>
<a href="">글목록(페이징)</a>
<a href="">글수정</a>
<a href="">글답변</a>
</body>
</html>
* 결과보기
'프로그래밍 > JSP Programming' 카테고리의 다른 글
2012-5-18 MVC패턴을 활용한 게시판 글삭제하기 (0) | 2012.05.18 |
---|---|
2012-5-17 게시판 글수정하기 (0) | 2012.05.17 |
2012-5-17 전체 글목록 조회(no paging) (0) | 2012.05.17 |
2012-5-16 MVC를 이용한 게시판 만들기[실습] (0) | 2012.05.16 |
2012-5-15 Oracle Sequence (0) | 2012.05.15 |