1.
시작하기
-
이전 까지 만든 게시판 기능(게시판리스트보기+글 내용 미리 보기)에 추가로 제목을 클릭할 때 본문 내용을 읽을 수 있는 “게시글
상세보기” 기능을 만들어 보자.
-
이전의 내용을 참고 한다면 어렵지 않게 구현할 수
있다.
-
DAO쪽, service쪽, 컨트롤러 수정,
뷰 페이지 신규작성, action-servlet.xml 수정 순서로 만들어 보자.
2. [BoardDAO.java] ,
[BoardDAOImple.java]
package com.board.dao;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import com.board.model.BoardDTO;
public interface BoardDAO {
//
전체 게시글 수
public
int boardCount(Map<String, Object>searchMap)throws
DataAccessException;
//
게시판 리스트
public
List<BoardDTO> boardList(Map<String, Object>searchMap) throws
DataAccessException;
//
게시물 본문 미리보기
public
String preView(String seq)throws DataAccessException;
// 게시글 조회수 1씩증가
public
int updateReadCount(String seq)throws DataAccessException;
// 게시글 상세보기
public
BoardDTO readContent(String seq)throws DataAccessException;
}
------------------------------------------------------------------------------------------------------------
package com.board.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import com.board.model.BoardDTO;
public class BoardDAOImple implements BoardDAO {
private
JdbcTemplate jdbaTemplate;
public
void setDataSource(DataSource dataSource){
this.jdbaTemplate
= new JdbcTemplate(dataSource);
}
//
게시글 수
public
int boardCount(Map<String, Object>searchMap)throws
DataAccessException{
int
count = 0;
String
sql = "";
if(searchMap.get("boardListSearchText")
== null){
sql
= "select count(*) from board02";
count
= jdbaTemplate.queryForObject(sql,
Integer.class
);
}else{
String
boardListSelect = (String) searchMap.get("boardListSelect");
String
boardListSearchText = (String) searchMap.get("boardListSearchText");
sql
= "select count(*) from board02 where "+boardListSelect+" like
'%"+boardListSearchText+"%'";
count
= jdbaTemplate.queryForObject(sql,Integer.class);
}
return
count;
}
//
게시판 리스트
public
List<BoardDTO> boardList(Map<String, Object>searchMap) throws
DataAccessException {
List<BoardDTO>
boardList = null;
String
sql = "";
Object[]
obj;
if(searchMap.get("boardListSearchText")
== null){
sql
= "select * from ("
+
" select b.seq
,b.name,b.title ,TO_CHAR(b.regdate,'YYYY/MM/DD')as regdate, b.readcount, "
+
"
r.reply_level"
+
" from board02 b,
reply r"
+
" where b.seq =
r.reply"
+
" order by r.reply
desc, r.reply_step
asc"
+
" )"
+
" where ROWNUM
BETWEEN ? AND
?";
obj
= new Object[] {searchMap.get("startRow"),searchMap.get("endRow")};
}else{
String
boardListSelect = (String) searchMap.get("boardListSelect");
String
boardListSearchText = (String) searchMap.get("boardListSearchText");
sql
= "select * from ("
+
" select b.seq
,b.name,b.title ,TO_CHAR(b.regdate,'YYYY/MM/DD')as regdate, b.readcount, "
+
"
r.reply_level"
+
" from board02 b,
reply r"
+
" where b.seq =
r.reply"
+
" and
"+boardListSelect+" like '%"+boardListSearchText+"%'"
+
" order by r.reply
desc, r.reply_step asc"
+
" )"
+
" where ROWNUM
BETWEEN ? AND ?";
obj
= new Object[] {searchMap.get("startRow"),searchMap.get("endRow")};
}
boardList
= jdbaTemplate.query(sql,
obj,
new RowMapper<BoardDTO>(){
public BoardDTO
mapRow(ResultSet rs, int rowNum)throws SQLException{
BoardDTO boardDTO =
new BoardDTO(rs.getString("seq"),
rs.getString("name"),
rs.getString("title"),
rs.getString("regdate"),
rs.getInt("readcount"),
rs.getInt("reply_level")
);
return
boardDTO;
}
});
return
boardList;
}
//
게시물 본문내용 미리보기
public
String preView(String seq) throws DataAccessException{
String
sql = "select content from board02 where seq = ?";
String
preContent = "";
Object
obj[] = {seq};
preContent
= jdbaTemplate.queryForObject(sql,obj,String.class);
return
preContent;
}
// 게시글 조회수 1씩증가
public
int updateReadCount(String seq)throws DataAccessException{
String
sql = " update board02 set readcount = nvl(readcount,0)+1 where seq =
?";
Object[]
obj = {seq};
return
jdbaTemplate.update(sql,obj);
}
// 게시글 상세보기
public
BoardDTO readContent(String seq)throws DataAccessException{
// 조회수 1증가 메소드 호출
this.updateReadCount(seq);
String
sql = "select * from board02 where seq = ?";
Object[]
obj = {seq};
BoardDTO
boardDTO = jdbaTemplate.queryForObject(sql,
obj,
new
RowMapper<BoardDTO>() {
public
BoardDTO mapRow(ResultSet rs,int rowNum)throws SQLException {
BoardDTO boardDTO = new
BoardDTO();
return
boardDTO;
});
return
boardDTO;
}
}
3. [BoardService.java] ,
[BoardServiceImple.java]
package com.board.service;
import java.util.List;
import java.util.Map;
import com.board.model.BoardDTO;
public interface BoardService {
// 게시글 수
public int
boardCount(Map<String, Object>searchMap)throws Exception;
// 게시판 리스트
public
List<BoardDTO> boardList(Map<String, Object>searchMap)throws
Exception;
//게시물 미리보기
public
String preView(String seq)throws Exception;
// 게시글 상세보기
public
BoardDTO readContent(String seq)throws Exception;
}
------------------------------------------------------------------------------------------------------------
package com.board.service;
import java.util.List;
import java.util.Map;
import com.board.dao.BoardDAO;
import com.board.model.BoardDTO;
public class BoardServiceImple implements BoardService {
private BoardDAO
boardDAO;
public void
setBoardDAO(BoardDAO boardDAO){
this.boardDAO =
boardDAO;
}
// 게시글 수
public int
boardCount(Map<String, Object> searchMap) throws Exception {
return
boardDAO.boardCount(searchMap);
}
// 게시판 리스트
public
List<BoardDTO> boardList(Map<String, Object> searchMap) throws
Exception {
return
boardDAO.boardList(searchMap);
}
// 게시물 미리보기
public
String preView(String seq){
return
boardDAO.preView(seq);
}
// 게시글 상세보기
public
BoardDTO readContent(String seq)throws Exception{
return
boardDAO.readContent(seq);
}
}
4. [BoardMultiController.java]
package com.onj.board;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.board.model.BoardDTO;
import com.board.service.BoardService;
import com.board.util.EncodingHandler;
import com.board.util.PageHandler;
public class BoardMultiController extends MultiActionController{
private
BoardService boardService;
private
PageHandler pageHandler;
public void
setBoardService(BoardService boardService){this.boardService =
boardService;}
public void
setPageHandler(PageHandler pageHandler){this.pageHandler = pageHandler;}
ModelAndView mav
= null;
// 게시판 리스트
public
ModelAndView list(HttpServletRequest request, HttpServletResponse
response)throws Exception{
mav = new
ModelAndView();
// 상세보기에서 사용한 session 지운다.
HttpSession
session = request.getSession();
if(session.isNew()
== false){session.invalidate();}
List<BoardDTO>
list = null;
// 검색select ,
검색Text
String
boardListSelect = request.getParameter("boardListSelect");
String
boardListSearchText = request.getParameter("boardListSearchText");
Map<String,
Object> searchMap = new HashMap<String, Object>();
if(boardListSearchText
!= null){
searchMap.put("boardListSearchText",
EncodingHandler.toKor(boardListSearchText));
searchMap.put("boardListSelect",
boardListSelect);
}
String
pageNumber = request.getParameter("pageNumber");
int pageNum =
1;
if(pageNumber !=
null){pageNum = Integer.parseInt(pageNumber);}
// 게시글 수
int totalCount =
pageHandler.boardAllNumber(searchMap);
// 페이지 갯수
int
totalPageCount = pageHandler.boardPageCount(searchMap);
// startPage ,
endPage
int startPage =
pageHandler.boardStartPage(pageNum);
int endPage =
pageHandler.boardEndPage(pageNum,searchMap);
// 처음, 마지막
rowNumber
List<Object>
rowNumberList = new ArrayList<Object>();
rowNumberList =
pageHandler.boardSetPageNumber(pageNum);
searchMap.put("startRow",
rowNumberList.get(0));
searchMap.put("endRow",
rowNumberList.get(1));
// 글 전체 출력
list =
boardService.boardList(searchMap);
mav.addObject("pageNumber",pageNum);
mav.addObject("boardCount",totalCount);
mav.addObject("totalPageCount",
totalPageCount);
mav.addObject("startPage",
startPage);
mav.addObject("endPage",
endPage);
mav.addObject("list",
list);
mav.setViewName("list");
return
mav;
}
// 게시글 상세보기
public
ModelAndView read(HttpServletRequest request, HttpServletResponse
response)throws Exception{
String
seq = request.getParameter("seq");
BoardDTO
boardDTO = boardService.readContent(seq);
// 상세글 내용 session에 담음
HttpSession
session = request.getSession();
session.setAttribute("boardDTO",
boardDTO);
mav
= new ModelAndView("read", "boardDto", boardDTO);
return
mav;
}
}
5. [/WEB-INF/jsp/read.jsp] ,
[/css/boardCss.css]
read.jsp 만들어야됨.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ 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>
<link rel="stylesheet" type="text/css"
href="/board/css/boardCss.css">
<script type="text/javascript"
src="/board/js/boardActionJs.js"></script>
<meta http-equiv="Content-Type" content="text/html;
charset=EUC-KR">
<title>Board Read</title>
</head>
<body>
<center>
<h1>오엔제이 프로그래밍 실무교육센터 스프링 게시판</h1><hr>
<form
name="boardReadForm" method="post">
<table
id="readTable" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>제목:</td>
<td
class="readTD">${boardDto.title}</td>
</tr>
<tr>
<td>작성자:</td>
<td
class="readTD">${boardDto.name}</td>
</tr>
<tr>
<td>내용:</td>
<td><textarea
rows="10" cols="77%"
readonly="readonly">${boardDto.content}</textarea></td>
</tr>
<tr>
<td>파일:</td>
<td
class="readTD">
<c:choose>
<c:when
test="${boardDto.filename != ' '}">
${boardDto.filename}
</c:when>
<c:when
test="${boardDto.filename == ' '}">
파일이 없습니다.
</c:when>
</c:choose>
</td>
</tr>
</table>
<table
class="readTable02">
<tr>
<td
colspan="2" id="readButtonTD">
<input
type="button" value="답변"
onclick="location.href='/board/reply.html?seq=${boardDto.seq}'">
<input
type="button" value="수정"
onclick="location.href='/board/modifyPage.html?seq=${boardDto.seq}'">
<input
type="button" value="삭제" onclick="deleteGo('${boardDto.seq}');">
<input
type="button" value="목록" onclick="location.href='/board/list.html'">
</td>
</tr>
</table>
<br>
<!-- 코멘트
-->
<table
class="readTable02" cellpadding="0" cellspacing="0">
<tr>
<td>이름 :
<input type="text" name="comment_name"></td>
<td>코멘트 :
<input type="text" name="comment_comm"></td>
<td>
<input
type="button" value="코멘트입력" onclick="commentInput();">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
------------------------------------------------------------------------------------------------------------
@CHARSET "EUC-KR";
td{text-align: center;}
a:link{ text-decoration:none; color: #5D5D5D;}
a:visited{ text-decoration:none; color: #5D5D5D;}
a:active{ text-decoration:none; color: #47C83E;}
a:hover{ text-decoration:none; color: #47C83E;}
.listTable{width:600px;}
#boardListCount{text-align: right;}
#boardList_a{font-weight: bold; color:#8041D9;}
#readTable{width: 600px;
height: 200px;}
#readButtonTD{text-align:
right;}
.readTD{text-align: left;}
.readTable02{width: 600px;}
#layer1{
position:
absolute;
padding:
5px;
filter:
alpha(opacity=50);
width: 250px;
height: 150px;
background-color:
white;
border: 2px
#000000 dotted;
visibility:
hidden;
}
6. [servlet-context,xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- Handles
HTTP GET requests for /resources/** by efficiently serving up static resources
in the ${webappRoot}/resources directory -->
<resources
mapping="/resources/**" location="/resources/" />
<beans:bean
id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property
name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<beans:property
name="url" value="jdbc:oracle:thin:@localhost:1521:ex"/>
<beans:property
name="username" value="study"/>
<beans:property
name="password" value="study"/>
</beans:bean>
<!-- Resolves
views selected for rendering by @Controllers to .jsp resources in the
/WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property
name="prefix" value="/WEB-INF/jsp/"/>
<beans:property
name="suffix" value=".jsp" />
</beans:bean>
<!-- 넘어오는
URL에 따라 컨트롤러에서 실행될 메소드 매핑 -->
<!--
PropertiesMethodNameResolver는 prop key로 넘어오는 url에 대해 실행할 컨트롤러의 메소드 정의
-->
<beans:bean
id="userControllerMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<beans:property
name="mappings">
<beans:props>
<beans:prop
key="/list.html">list</beans:prop>
<beans:prop
key="/read.html">read</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!--
controller mapping -->
<beans:bean name="/list.html
/read.html" class="com.onj.board.BoardMultiController">
<beans:property
name="methodNameResolver" ref="userControllerMethodNameResolver"/>
<beans:property
name="boardService" ref="boardService"/>
<beans:property
name="pageHandler" ref="pageHandler"/>
</beans:bean>
</beans:beans>
댓글 없음:
댓글 쓰기