๋ก๊ทธ์ธ ์ฑ๊ณต์ ๊ฒ์ํ์ผ๋ก ์ด๋
bbslist.do
@RequestMapping(value = "loginAf.do", method=RequestMethod.POST)
public String loginAf(MemberDto dto, HttpServletRequest req) {
MemberDto login = service.login(dto);
if(login != null && !login.getId().equals("")) {
req.getSession().setAttribute("login", login);
// req.getSession().setMaxInactiveInterval(60 * 60 * 24);
return "redirect:/bbslist.do";
}
else {
return "redirect:/login.do";
}
}
BbsController
bbslist.do
package bit.com.a.controller;
import java.util.Date;
import java.util.List;
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 org.springframework.web.bind.annotation.ResponseBody;
import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;
import bit.com.a.service.BbsService;
@Controller
public class BbsController {
private static Logger logger = LoggerFactory.getLogger(BbsController.class);
@Autowired
BbsService service;
@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
public String bbslist(Model model) {
model.addAttribute("doc_title", "๊ธ๋ชฉ๋ก");
return "bbslist.tiles";
}
bbslist.tiles
<definition name="bbslist.tiles" template="/WEB-INF/views/layouts-tiles.jsp">
<put-attribute name="header" value="/WEB-INF/views/commons/header.jsp"/>
<put-attribute name="top_inc" value="/WEB-INF/views/commons/top_inc.jsp"/>
<put-attribute name="top_menu" value="/WEB-INF/views/commons/top_menu.jsp"/>
<put-attribute name="left_menu" value="/WEB-INF/views/bbs/left_bbsmenu.jsp"/>
<put-attribute name="main" value="/WEB-INF/views/bbs/bbslist.jsp"/>
<put-attribute name="bottom_inc" value="/WEB-INF/views/commons/bottom_inc.jsp"/>
</definition>
bbslist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- ๊ฒ์ -->
<div class="box_border" style="margin-top: 5px; margin-bottom: 10px">
<form action="" id="_frmFormSearch" method="get">
<table style="margin-left: auto; margin-right: auto; margin-top: 3px; margin-bottom: 3px">
<tr>
<td>๊ฒ์</td>
<td style="padding-left: 5px">
<select id="_choice" name="choice">
<option value="" selected="selected">์ ํ</option>
<option value="title">์ ๋ชฉ</option>
<option value="content">๋ด์ฉ</option>
<option value="writer">์์ฑ์</option>
</select>
</td>
<td style="padding-left: 5px">
<input type="text" id="_searchWord" name="searchWord">
</td>
<td style="padding-left: 5px">
<span class="button blue">
<button type="button" id="btnSearch">๊ฒ์</button>
</span>
</td>
</tr>
</table>
</form>
</div>
<table class="list_table" style="width: 85%" id="_list_table">
<colgroup>
<col style="width:70px">
<col style="width:auto">
<col style="width:100px">
</colgroup>
<tr>
<th>๋ฒํธ</th><th>์ ๋ชฉ</th><th>์กฐํ์</th><th>์์ฑ์</th>
</tr>
</table>
<br><br>
<div class="container">
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination" style="justify-content:center;"></ul>
</nav>
</div>
<br><br>
๊ฒ์๊ณผ ํ์ด์ง ์ฒ๋ฆฌ๋ฅผ ์ํด Controller์ ํ์ํ ๋ฉ์๋ ์์ฑ
ํ์ด์ง์ Pagingnation์ ์ฌ์ฉํจ
jquery.twbsPagination.min.js ์คํฌ๋ฆฝํธ ํ์ผ์ header.jsp์์ ํธ์ถํด์ค๋ค
์ด์ฐจํผ layouts-tiles.jsp์์ ํธ์ถํด์ฃผ๊ธฐ๋๋ฌธ
๊ฒ์ (id = "btnSearch")ํด๋ฆญ์
getBbsListData(0);
getBbsListCount();
function ์งํ
ajax๋ฅผ ํตํด ๊ฒ์ ๊ฒฐ๊ณผ(choice: ๊ฒ์์ฃผ์ (์ ๋ชฉ,๋ด์ฉ ,์์ฑ์), search: ๊ฒ์๋ด์ฉ)๋ฅผ
controller์ ์ ์ก
<script>
getBbsListData(0);
getBbsListCount();
//๊ฒ์
$("#btnSearch").click(function(){
getBbsListData(0);
getBbsListCount();
});
// bbslist์ ์ทจ๋
function getBbsListData( pNumber ) {
$.ajax({
url:"./bbslistData.do",
type:"get",
data:{ page:pNumber, choice:$("#_choice").val(), search:$("#_searchWord").val() },
success:function( list ){
// alert('success');
// alert(list);
$(".list_col").remove();
$.each(list, function (i, val) {
let app = "<tr class='list_col'>"
+ "<td>" + (i + 1) + "</td>"
+ "<td class='_hover_tr' style='text-align:left'>"
+ getArrow( val.depth ); // ๋๊ธ์ด๋ฏธ์ง
if(val.del == 0){ // ์ญ์ ๋์ง ์์ ๊ธ
app += "<a href='bbsdetail.do?seq=" + val.seq + "'> " + val.title + "</a>";
}else{ // ์ญ์ ๋ ๊ธ
app += "<font color='#ff0000'>* ์ด ๊ธ์ ์์ฑ์์ ์ํด์ ์ญ์ ๋์์ต๋๋ค *</font>";
}
app += "</td>";
app += "<td>" + val.readcount + "</td>";
app += "<td>" + val.id + "</td>";
app += "</tr>";
$("#_list_table").append(app);
});
},
error:function(){
alert('error');
}
});
}
๊ฒ์์ bbslistData.do๋ก ์ด๋
param๊ฐ์๋ฐ๊ธฐ ์ํด paramDTO ์์ฑ
package bit.com.a.dto;
import java.io.Serializable;
public class BbsParam implements Serializable{
private String choice;
private String search;
private int page;
private int start;
private int end;
public BbsParam() {
}
public BbsParam(String choice, String search, int page, int start, int end) {
super();
this.choice = choice;
this.search = search;
this.page = page;
this.start = start;
this.end = end;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
````
Controller
๊ธ๋ชฉ๋ก,
๊ฒ์ํ ๊ธ๋ชฉ๋ก
package bit.com.a.controller;
import java.util.Date;
import java.util.List;
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 org.springframework.web.bind.annotation.ResponseBody;
import bit.com.a.dto.BbsDto;
import bit.com.a.dto.BbsParam;
import bit.com.a.service.BbsService;
@Controller
public class BbsController {
private static Logger logger = LoggerFactory.getLogger(BbsController.class);
@Autowired
BbsService service;
@RequestMapping(value = "bbslist.do", method = RequestMethod.GET)
public String bbslist(Model model) {
model.addAttribute("doc_title", "๊ธ๋ชฉ๋ก");
return "bbslist.tiles";
}
@ResponseBody
@RequestMapping(value = "bbslistData.do", method = RequestMethod.GET)
public List<BbsDto> bbslistData(BbsParam param) {
//๊ฒ์์ฒ๋ฆฌ
String choice = param.getChoice();
String search = param.getSearch();
int page = param.getPage();
if(choice == null) {
choice = "";
}
if(search == null) {
search = "";
}
param.setChoice(choice);
param.setSearch(search);
// paging ์ฒ๋ฆฌ
int sn = param.getPage();
int start = sn * 10 + 1; // 1 11
int end = (sn + 1) * 10; // 10 20
param.setStart(start);
param.setEnd(end);
List<BbsDto> list = service.getBbslist(param);
return list;
}
Paging
๊ธ์ ์ด์๋ฅผ ์ทจ๋ํ์ฌ ํ์ด์ง์ ์งํํด์ผํ๋ค.
ajax๋ฅผ ํตํด bbslistCount.do(์ปจํธ๋กค๋ฌ)๋ก ๊ฐ์ ํด๋น ํ์ด์ง, ๊ฒ์์ ๋ฐ์
MyBatis์์ ํด๋น ๊ฒ์ํ์ ๊ฒ์๋ง ์ด ๊ฐฏ์๋ฅผ return ๋ฐ๋๋ค
@ResponseBody
@RequestMapping(value = "bbslistCount.do", method = RequestMethod.GET)
public int bbslistCount(BbsParam param) {
int count = service.getBbsCount(param);
return count;
}
<!-- ๊ธ์ ์ด์ -->
<select id="getBbsCount" parameterType="bit.com.a.dto.BbsParam"
resultType="java.lang.Integer">
SELECT NVL(COUNT(*), 0)
FROM BBS
WHERE 1=1
<if test="choice != null and choice != ''
and search != null and search != ''">
<if test="choice == 'title'">
AND TITLE LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'content'">
AND CONTENT LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'writer'">
AND ID=#{search}
</if>
</if>
</select>
// ๊ธ์ ์ด์๋ฅผ ์ทจ๋
function getBbsListCount() {
$.ajax({
url:"./bbslistCount.do",
type:"get",
data:{ page:0, choice:$("#_choice").val(), search:$("#_searchWord").val() },
success:function( count ){
// alert('success');
// alert(count);
loadPage(count);
},
error:function(){
alert('error');
}
});
}
// paging ์ฒ๋ฆฌ
function loadPage( totalCount ) {
let pageSize = 10;
let nowPage = 1;
let _totalPages = totalCount / pageSize;
if(totalCount % pageSize > 0){
_totalPages++;
}
$("#pagination").twbsPagination('destroy'); // ํ์ด์ง ๊ฐฑ์ : ํ์ด์ง์ ๊ฐฑ์ ํด ์ค์ผ ๋ฒํธ๊ฐ ์ฌ์ค์ ๋๋ค.
$("#pagination").twbsPagination({
// startPage: 1,
totalPages: _totalPages,
visiblePages: 10,
first:'<span sria-hidden="true">«</span>',
prev:"์ด์ ",
next:"๋ค์",
last:'<span sria-hidden="true">»</span>',
initiateStartPageClick:false, // onPageClick ์๋ ์คํ๋์ง ์๋๋ก ํ๋ค
onPageClick:function(event, page){
nowPage = page;
// alert('nowPage:' + page);
getBbsListData( page - 1 );
}
});
}
Mapper
Bbs.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Bbs">
<!--
<select id="bbslist" resultType="bit.com.a.dto.BbsDto">
SELECT *
FROM BBS
ORDER BY REF DESC, STEP ASC
</select>
-->
<select id="bbslist" parameterType="bit.com.a.dto.BbsParam"
resultType="bit.com.a.dto.BbsDto">
SELECT SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT
FROM (SELECT ROW_NUMBER()OVER(ORDER BY REF DESC, STEP ASC) AS RNUM,
SEQ, ID, REF, STEP, DEPTH, TITLE, CONTENT, WDATE, DEL, READCOUNT
FROM BBS
WHERE 1=1
<if test="choice != null and choice != ''
and search != null and search != ''">
<!-- AND DEL=0 -->
<if test="choice == 'title'">
AND TITLE LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'content'">
AND CONTENT LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'writer'">
AND ID=#{search}
</if>
</if>
ORDER BY REF DESC, STEP ASC)
WHERE RNUM BETWEEN ${start} AND ${end}
</select>
<!-- ๊ธ์ ์ด์ -->
<select id="getBbsCount" parameterType="bit.com.a.dto.BbsParam"
resultType="java.lang.Integer">
SELECT NVL(COUNT(*), 0)
FROM BBS
WHERE 1=1
<if test="choice != null and choice != ''
and search != null and search != ''">
<if test="choice == 'title'">
AND TITLE LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'content'">
AND CONTENT LIKE '%'||#{search}||'%'
</if>
<if test="choice == 'writer'">
AND ID=#{search}
</if>
</if>
</select>
'์คํ๋ง Spring > ๐งถ ์คํ๋ง ํ๋ ์์ํฌ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ (์์ฑ, ์์ , ์ญ์ , ๋ํ ์ผ ) (0) | 2021.06.24 |
---|---|
Spring ํ์ผ ์ ๋ก๋ (0) | 2021.06.24 |
Spring Model ๊ฐ์ฒด๋ ๋ฌด์์ผ๊น? (0) | 2021.06.22 |
Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ ๋ง๋ค๊ธฐ (๋ก๊ทธ์ธ, ํ์๊ฐ์ ) (0) | 2021.06.22 |
Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ ๋ง๋ค๊ธฐ์ Tiles ์ค์ ๋ฒ (0) | 2021.06.22 |
๋๊ธ