본문 바로가기

spring/소스코드

페이지 번호로 이동하기

https://colorscripter.com/

코드 색깔 변환

 

Color Scripter

Simple & Flexible Syntax HighLighter

colorscripter.com

실습영상

 

 

<com.example.domain>-BBSVO.java(Class)

 

package com.example.domain;

import java.util.Date;

public class BBSVO {
	private int id;
	private String title;
	private String content;
	private Date wdate;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getWdate() {
		return wdate;
	}
	public void setWdate(Date wdate) {
		this.wdate = wdate;
	}

 

<com.example.domain>-Criteria.java(Class)

 

//시작 페이지,DISPLAY,페이지 정보
package com.example.domain;
public class Criteria{
	  private int page;
	  private int perPageNum;
	  //한 페이지 당 몇개를 할 건지
	  public Criteria(){
	    this.page = 1;
	    this.perPageNum = 3;
	    //한 페이지당 3개가 나온다.
	  }
	  //페이지를 입력 안했을때 설정
	  public void setPage(int page){
	    if (page <= 0){
	      this.page = 1;
	      return;
	    }
	    this.page = page;
	  }
	  //시작 페이지 지정 메서드
	  public void setPerPageNum(int perPageNum){
	    if(perPageNum <= 0 || perPageNum > 100){
	      this.perPageNum = 10;
	      return;
	    }
	    this.perPageNum = perPageNum;
	  }
	  public int getPage(){
	    return page;
	  }
	  
	  public int getPageStart(){
	    return (this.page - 1) * perPageNum;
	  }
	  
	  public int getPerPageNum(){
	    return this.perPageNum;
	  }
	  @Override
	  public String toString(){
	    return "Criteria [page=" + page + ", " + "perPageNum=" + perPageNum + "]";
	  }
	}

 

<com.example.domain>-PageMaker.java(Class)

 

//페이지 번호 클래스
package com.example.domain;
public class PageMaker{
	  private int totalCount;
	  
	  private int startPage;
	  private int endPage;
	  
	  private boolean prev;
	  //이전 버튼이 보여지는지
	  private boolean next;
	  //다음 버튼이 보여지는지
	  
	  private int displayPageNum = 10;
	  //한페이지당 보여지는 데이터 개수
	  
	  private Criteria cri;
	  
	  public void setCri(Criteria cri){
	    this.cri = cri;
	  }
	  public void setTotalCount(int totalCount){
	    this.totalCount = totalCount;
	    calcData();
	  }
	  private void calcData(){
	    endPage = (int) (Math.ceil(cri.getPage() / (double) displayPageNum) * displayPageNum);
	    startPage = (endPage - displayPageNum) + 1;
	    int tempEndPage = (int) (Math.ceil(totalCount / (double) cri.getPerPageNum()));
	    if(endPage > tempEndPage){
	      endPage = tempEndPage;
	    }
	    prev = startPage == 1 ? false : true;
	    next = endPage * cri.getPerPageNum() >= totalCount ? false : true;
	  }
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public boolean isPrev() {
		return prev;
	}
	public void setPrev(boolean prev) {
		this.prev = prev;
	}
	public boolean isNext() {
		return next;
	}
	public void setNext(boolean next) {
		this.next = next;
	}
	public int getDisplayPageNum() {
		return displayPageNum;
	}
	public void setDisplayPageNum(int displayPageNum) {
		this.displayPageNum = displayPageNum;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public Criteria getCri() {
		return cri;
	}
}

 

<com.example.domain>-SearchCriteria.java(Class)

 

//Criteria 상속
package com.example.domain;


public class SearchCriteria extends Criteria{
	private String searchType;
	private String keyword;
	
	public String getSearchType() {
		return searchType;
	}
	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	@Override
	public String toString() {
		return "SearchCriteria [searchType=" + searchType + ", keyword=" + keyword + "]";
	}
}

 

<com.example.persistence>-BBSDAO.java(Interface)

 

package com.example.persistence;

import java.util.List;

import com.example.domain.SearchCriteria;

//페이징 Criteria
//검색 Search

public interface BBSDAO {
	public List<BBSDAO> list(SearchCriteria cri) throws Exception;
	public int total(SearchCriteria cri) throws Exception;
}

 

<com.example.persistence>-BBSDAOImpl(Class)

 

package com.example.persistence;

import java.util.List;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.example.domain.SearchCriteria;

@Repository
public class BBSDAOImpl implements BBSDAO{
	@Inject
	SqlSession session;
	private static final String namespace="BBSMapper";
	
	
	@Override
	public List<BBSDAO> list(SearchCriteria cri) throws Exception {
		// TODO Auto-generated method stub
		return session.selectList(namespace + ".list" ,cri);
	}


	@Override
	public int total(SearchCriteria cri) throws Exception {
		// TODO Auto-generated method stub
		
		return session.selectOne(namespace +".total",cri);
	}
}

 

 

<com.example.web>-BBSController.java(Class)

 

 

package com.example.web;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.domain.PageMaker;
import com.example.domain.SearchCriteria;
import com.example.persistence.BBSDAO;

@Controller
public class BBSController {
	@Inject
	BBSDAO dao;
	
	@RequestMapping("list")
	public String list(SearchCriteria cri,Model model) throws Exception {
		
		cri.setPerPageNum(10);
		
		PageMaker pm=new PageMaker();
		pm.setCri(cri);
		pm.setTotalCount(dao.total(cri));
		
		
		model.addAttribute("list", dao.list(cri));
		model.addAttribute("pm",pm);
		model.addAttribute("cri" ,cri);
		return "list";
	}
}

 

<src/main/resources>-<mappers>-BBSMapper.xml(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="BBSMapper">
	<select id="list" resultType="com.example.domain.BBSVO">
		select * from tblbbs
		<if test="searchType=='title'">
			where title like concat('%' ,#{keyword},'%')
		</if>
		<if test="searchType=='content'">
			where content like concat('%' ,#{keyword},'%')
		</if> 
		order by id desc
		limit #{pageStart},#{perPageNum}
	</select>
	<select id="total" resultType="int">
		select count(*) from tblbbs
		<if test="searchType=='title'">
			where title like concat('%' ,#{keyword},'%')
		</if>
		<if test="searchType=='content'">
			where content like concat('%' ,#{keyword},'%')
		</if> 
	</select>
</mapper>

 

<src>-<main>-<webapp>-<WEB-INF>-<views>-list.jsp(jsp파일)

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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 -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

<!-- 부가적인 테마 -->
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">

<!-- 합쳐지고 최소화된 최신 자바스크립트 -->
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>

<style>
#page {
	width: 1024px;
	margin: 0px auto;
}

body {
	text-align: center;
}
</style>
</head>
<body>
	<div id="page">
		<div class="dropdown">
			<button id="dLabel" type="button" data-toggle="dropdown"
				aria-haspopup="true" aria-expanded="false">
				Dropdown trigger <span class="caret"></span>
			</button>
			<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
				...
			</ul>
		</div>
		<h1>[게시판 목록]</h1>
		<div class="box-body">
			<!-- box-body 시작 -->
			<form name="frm" action="list">
				<input type="hidden" name="page" value="${pm.cri.page}"> <select
					name="searchType">
					<option value="title"
						<c:out value="${cri.searchType=='title'?'selected':''}"/>>제목</option>
					<option value="content"
						<c:out value="${cri.searchType=='content'?'selected':''}"/>>내용</option>
				</select> <input type="text" name="keyword" value="${cri.keyword}"> <input
					type="submit" value="검색"> 검색:${pm.totalCount}건
			</form>
			<table class="table table-hover">
				<tr>
					<th>번호</th>
					<th>제목</th>
					<th>작성일</th>
				</tr>
				<c:forEach items="${list}" var="vo">
					<tr>
						<td>${vo.id}</td>
						<td>${vo.title}</td>
						<td><fmt:formatDate value="${vo.wdate}"
								pattern="yyyy-MM-dd kk:mm:ss" /></td>
					</tr>
				</c:forEach>
			</table>
			<!-- 페이지 번호 출력 시작 -->
			<ul class="pagination">
				<c:if test="${pm.prev}">
					<li><a href="#" onClick="funList('${pm.startPage-1}')">◀</a></li>
				</c:if>
				<c:forEach begin="${pm.startPage}" end="${pm.endPage}" var="i">
					<li <c:out value="${pm.cri.page==i?'class=active':''}"/>><a
						href="#" onClick="funList('${i}')">${i}</a></li>
				</c:forEach>
				<c:if test="${pm.next}">
					<li><a href="#" onClick="funList('${pm.endPage+1}')">▶</a></li>
				</c:if>
			</ul>
			<!-- 페이지 번호 출력 종료 -->
		</div>
		<!-- box-body 끝 -->
	</div>
	<!-- page 끝 -->
	<div class="container">

		<div class="btn-group" role="group" aria-label="...">
			<button type="button" class="btn btn-primary button-class1">바르셀로나</button>
			<button type="button" class="btn btn-default button-class2">레알마드리드</button>
		</div>

	</div>
</body>

<script>
	function funList(page) {
		frm.page.value = page;
		frm.submit();
	}
	$(function() {

		$('.button-class1').click(function() {
			if ($(this).hasClass('btn-default'))
				$(this).removeClass('btn-default');
			if (!$(this).hasClass('btn-primary'))
				$(this).addClass('btn-primary');
			if ($('.button-class2').hasClass('btn-primary'))
				$('.button-class2').removeClass('btn-primary');
			if (!$('.button-class2').hasClass('btn-default'))
				$('.button-class2').addClass('btn-default');
		});

		$('.button-class2').click(function() {
			if ($(this).hasClass('btn-default'))
				$(this).removeClass('btn-default');
			if (!$(this).hasClass('btn-primary'))
				$(this).addClass('btn-primary');
			if ($('.button-class1').hasClass('btn-primary'))
				$('.button-class1').removeClass('btn-primary');
			if (!$('.button-class1').hasClass('btn-default'))
				$('.button-class1').addClass('btn-default');
		});

	});
	$('.dropdown-toggle').dropdown()
</script>
</html>