๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์Šคํ”„๋ง Spring/๐Ÿงถ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ Spring

์Šคํ”„๋ง Tiles๋ฅผ ํ™œ์šฉํ•œ ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ (๊ฒŒ์‹œํŒ ํŽ˜์ด์ง•, ๊ฒ€์ƒ‰)

by Meteora_ 2021. 6. 22.
728x90

๋กœ๊ทธ์ธ ์„ฑ๊ณต์‹œ ๊ฒŒ์‹œํŒ์œผ๋กœ ์ด๋™

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 + "'>&nbsp;" + 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>

๋Œ“๊ธ€