[Spring] 게시판 리스트 boardList
1) 게시판 boardController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Controller
@RequestMapping(value = "/board")
public class BoardController {
@Autowired
private BoardService boardService;
//게시판 리스트
@RequestMapping(value = "/boardList")
public String boardList(HttpServletRequest request, HttpServletResponse response) throws Exception {
return "board/boardList";
}
@RequestMapping(value = "/getBoardList")
@ResponseBody
public ResultUtil getBoardList(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {
ResultUtil resultUtils = boardService.getBoardList(boardForm);
return resultUtils;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
@RequestMapping - 요청 URL을 어떤 메소드가 처리할 지 여부를 결정
@Autowired - 각 상황의 타입에 맞는 IoC 컨테이너 안에 존재하는 Bean을 자동으로 주입해주게됨
@ResponseBody - 메소드에 @ResponseBody로 Annotation이 되어 있으면 메소드에서 리턴되는 값은 View를 통해서 출력되지 않고 HTTP ResponseBody에 직접 쓰여지게 됨 이때 쓰여지기 전에
리턴되는 데이터 타입에 따라 MessageConverter에서 변환이 이뤄진 후 쓰여지게 됨
- 자바 객체를 HTTP 응답 몸체로 전송
- 자바 객체를 HTTP 요청의 body 내용으로 매핑하는 역할
2 ) 게시판 boardDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Repository
public class BoardDao {
@Resource(name = "sqlSession")
private SqlSession sqlSession;
public int getBoardCnt(BoardForm boardForm) throws Exception {
return sqlSession.selectOne(NAMESPACE + ".getBoardCnt", boardForm);
}
public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {
return sqlSession.selectList(NAMESPACE + ".getBoardList", boardForm);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
sqlSession.selectOne - 오직 하나의 객체 만을 return 해야함, 한개 이상을 return 하거나 null이 return 된다면
exception이 발생
sqlSession.selectList - 하나 이상의 객체를 return 할때 사용하는 selectList
3 ) 게시판 boardDto getter/setter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int board_seq; // 게시글 번호
int board_re_ref; // 게시글 그룹 번호
int board_re_lev; // 게시글 글의 깊이
int board_re_seq; // 게시글 글의 순서
String board_writer; // 작성자
String board_subject; // 제목
String board_content; // 내용
int board_hits; // 조회수
String del_yn; // 삭제유무
String ins_user_id; // Insert 사용자 아이디
String ins_date; // Insert 날짜
String upd_user_id; // Update 사용자 아이디
String upd_date; // Update 날짜
String result;
|
4 ) 게시판 boardService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class BoardService {
protected final Logger logger = LoggerFactory.getLogger(BoardService.class);
@Autowired
private BoardDao boardDao;
/* 게시판 리스트 보기 */
public ResultUtil getBoardList(BoardForm boardForm) throws Exception {
ResultUtil resultUtil = new ResultUtil();
CommonDto commonDto = new CommonDto();
//totalCount - 총괄 카운트
int totalCount = boardDao.getBoardCnt(boardForm);
if (totalCount != 0) {
CommonForm commonForm = new CommonForm();
commonForm.setFunction_name(boardForm.getFunction_name());
commonForm.setCurrent_page_no(boardForm.getCurrent_page_no());
commonForm.setCount_per_page(10);
commonForm.setCount_per_list(10);
commonForm.setTatal_list_count(totalCount);
commonDto = PagingUtil.setPageUtil(commonForm);
}
boardForm.setLimit(commonDto.getLimit());
boardForm.setOffset(commonDto.getOffset());
List<BoardDto> list = boardDao.getBoardList(boardForm);
HashMap<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("list", list);
resultMap.put("totalCount", totalCount);
resultMap.put("pagination", commonDto.getPagination());
resultUtil.setData(resultMap);
resultUtil.setState("SUCCESS");
return resultUtil;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
@Autowired - 각 상황에 맞는 IoC 컨테이너 안에 존재하는 Bean을 자동으로 주입해주게 됨
HashMap - HashMap은 Map을 구현함 / key와 value를 묶어 하나의 entry로 저장한다는 특징을 가짐
그리고 hashing을 사용하기 때문에 많은 양의 데이터를 검색하는데 뛰어난 성능을 보임
5 ) 게시판 mapper 쿼리
LIMIT - 가져올 row의 수
OFFSET은 - 몇 번 쨰 row부터 가져올 지를 결정
6 ) 게시판 View boardList.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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>
<!-- 공통 CSS -->
<!-- 공통 JavaScript -->
<script type="text/javascript">
//자바스크립트의 onload같은 기능 html페이지가 화면에 뿌려지고 나서 ready안에 서술된 이벤트들이 동작 준비를 하는 것
$(document).ready(function(){
getBoardList();
});
/** 게시판 - 상세 페이지 이동 */
//boardSeq를 받아온 boardDetail의 링크를 염
function goBoardDetail(boardSeq){
location.href = "/board/boardDetail?boardSeq="+ boardSeq;
}
/** 게시판 - 작성 페이지 이동 */
function goBoardWrite(){
location.href = "/board/boardWrite";
}
/** 게시판 - 목록 조회 */
function getBoardList(currentPageNo){
if(currentPageNo === undefined){
currentPageNo = "1";
}
//currentpageno = 현재화면에 출력되고있는 페이지번호
$("#current_page_no").val(currentPageNo);
$.ajax({
url :"/board/getBoardList",
//serialize - 데이터를 보내기 위해 form요소 집합을 문자열로 인코딩
data : $("#boardForm").serialize(),
dataType:"JSON",
cache : false,
async : true,
type :"POST",
success : function(obj) {
getBoardListCallback(obj);
},
error : function(xhr, status, error) {}
});
}
/** 게시판 - 목록 조회 콜백 함수 */
function getBoardListCallback(obj){
if(state == "SUCCESS"){
var listLen = list.length;// 데이터베이스 data의 list의 length
var totalCount = data.totalCount; //
var pagination = data.pagination;
var str = "";
if(listLen > 0){
for(var a=0; a<listLen; a++){
var boardSeq = list[a].board_seq;
var boardReRef = list[a].board_re_ref; //글의 그룹 번호
var boardReLev = list[a].board_re_lev; //답변 글의 길이
var boardReSeq = list[a].board_re_seq; //답변 글의 순서
var boardWriter = list[a].board_writer;
var boardSubject = list[a].board_subject;
var boardContent = list[a].board_content;
var boardHits = list[a].board_hits;
var delYn = list[a].del_yn;
var insUserId = list[a].ins_user_id;
var insDate = list[a].ins_date;
var updUserId = list[a].upd_user_id;
var updDate = list[a].upd_date;
//boardseq의 데이터를 받아서 goboardDetail함수에 넘겨주는 onclick
str += "<tr>";
str += "<td>"+ boardSeq +"</td>";
str += "<td onclick='javascript:goBoardDetail("+ boardSeq +");' style='cursor:Pointer'>";
if(boardReLev > 0){
for(var b=0; b<boardReLev; b++){
str += "Re:";
}
}
str += boardSubject +"</td>"; // 글의 제목
str += "<td>"+ boardHits +"</td>"; // 조회수
str += "<td>"+ boardWriter +"</td>"; // 작성자
str += "<td>"+ insDate +"</td>"; // 날짜
str += "</tr>";
}
} else {
str += "<tr>";
str += "<td colspan='5'>등록된 글이 존재하지 않습니다.</td>";
str += "<tr>";
}
$("#tbody").html(str);
$("#total_count").text(totalCount);
$("#pagination").html(pagination);
} else {
alert("관리자에게 문의하세요.");
return;
}
}
</script>
</head>
<body>
<div id="wrap">
<div id="container">
<div class="inner">
<h2>게시글 목록</h2>
<form id="boardForm" name="boardForm">
<input type="hidden" id="function_name" name="function_name" value="getBoardList" />
<input type="hidden" id="current_page_no" name="current_page_no" value="1" />
<div class="page_info">
<span class="total_count"><strong>전체</strong> : <span id="total_count" class="t_red">0</span>개</span>
</div>
<table width="100%" class="table01">
<colgroup>
<col width="10%" />
<col width="25%" />
<col width="10%" />
<col width="15%" />
<col width="20%" />
</colgroup>
<thead>
<tr>
<th>글번호</th>
<th>제목</th>
<th>조회수</th>
<th>작성자</th>
<th>작성일</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
</form>
<div class="btn_right mt15">
<button type="button" class="btn black mr5" onclick="javascript:goBoardWrite();">작성하기</button>
</div>
</div>
<div id="pagination"></div>
</div>
</div>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
7 ) 게시판 리스트 결과 화면
//호출시 localhost:8080/board/boardList