BACK-END/Spring

[Spring] 페이징 Controller

단비_danbee 2020. 10. 15. 11:20

| BoardController.java

//페이징 기능 추가
	@RequestMapping("/boardList.do")
	public ModelAndView board_select(HttpServletRequest request, Model model) {
		String pnum;
		int pageNUM, pagecount;
		int start, end;
		int startpage, endpage;
		int temp;
		String skey="", sval="";
		String returnpage="";
		
		//[시작페이지21]~[26선택]~[30]
		pnum=request.getParameter("pageNum"); //<a href="boardList.do?pageNum=${i}">
		if(pnum==""||pnum==null) { 
			pnum="1"; //처음 index에서 boardList로 갈때, 한건 삭제 후, 신규 등록 후에는 1페이지로 가도록 설정 이거 안하면 nullpointException 된다
		}
		
		//[시작페이지21]~[26선택]~[30]
		pageNUM = Integer.parseInt(pnum); //26 
		
		int GGtotal = dao.boardCount(); //전체 레코드 수
		int Gtotal  = dao.boardCountSearch(skey,sval); //조회 레코드 갯수 중요해서 두번 기술함 한번만 기술해도 됌
		int Stotal  = dao.boardCountSearch(skey,sval);
		
		//행번호 251~260
		start=(pageNUM-1)*10+1; // 행번호 251  (26-1)*10-1 = 251행
		end  =pageNUM*10;		// 행번호 260  26*10 = 260행
		
		if(Stotal%10==0) {pagecount=Stotal/10;}
		else {pagecount=(Stotal/10)+1;}//총 레코드 갯수가 317이니까 총 페이지수는 32페이지
		
		//[시작페이지21]~[26선택]~[30]
		temp      = (pageNUM-1)%10; //5
		startpage = pageNUM-temp;   //26-5 = 21 시작페이지
		endpage   = startpage+9;	//21+9 = 30 끝페이지
		if(endpage>pagecount) {endpage=pagecount;} //if(40>32페이지수)
		
		List<BoardDTO> LG = dao.boardSelect(start,end);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("GGtotal", GGtotal);//전체갯수
		mav.addObject("Gtotal", Gtotal);//조회갯수
		mav.addObject("Stotal", Stotal);
		mav.addObject("pageNUM", pageNUM);
		mav.addObject("LG", LG);
		mav.setViewName("boardList");
		return mav; 
	}//end

 

| BoardList.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>
<html>
<head>
<meta charset="UTF-8">
<title>[boardList.jsp]</title>
   <style type="text/css">
    *{font-size: 12pt; font-weight: bold; }
    a{text-decoration:none;font-size: 12pt; font-weight: bold;   color:blue ;}
    a:hover{font-size: 12pt; font-weight: bold; text-decoration:underline; color:green ;  }
  </style>

</head>
<body>
 <font color=blue>[boardList.jsp]</font> <br>
    
  <table width="600"  border=1   cellspacing="0" >
  	 <tr align="right" height=50>
  	 	<td colspan="6"> 전체레코드갯수: ${Gtotal} &nbsp;  </td>
  	 </tr>
  
     <tr bgcolor=yellow height=50>
      <td>번호</td> <td>이름</td> 
      <td>제목</td> <td>성별</td> <td>취미</td>  <td>이미지</td> 
     </tr>
     
     <c:forEach var="dto" items="${LG}">
     <tr height=20>
    	<td> ${dto.rn} </td>
    	<td> ${dto.name} </td>
    	<td> 
    		<a href='boardDetail.do?idx=${dto.hobby_idx}'>${dto.title}</a>
    		<c:if test="${dto.rcnt>0}"> 
    			<font style="color:red;font-size:14pt">[${dto.rcnt}]</font> 
    		</c:if>
    	</td>
    	<td> ${dto.gender} </td>
    	<td> ${dto.hobby} </td>
    	<td>
    	 <img src ='${pageContext.request.contextPath}/resources/upload/${dto.img_file_name}' width=150 height=50 border=0> 
    	</td>     
     </tr> 
   </c:forEach>  
   
   
     
    <tr align="center">
  	 <td colspan="6">
  	  <!-- 이전 -->
  	  <c:if test="${startpage>10}"> 
  	   <a href="boardList.do?pageNum=${startpage-10}">[이전]</a>
  	  </c:if>
  	  
  	  <c:forEach var="i" begin="${startpage}" end="${endpage}" step="1">
  	  	<c:choose>
  	  		<c:when test="${i==pageNUM}"> 
  	  		  <font style='color:red;font-size:20pt'>[${i}]</font> 
  	  		</c:when>
  	  		<c:otherwise>
  	  		 <a href="boardList.do?pageNum=${i}${returnpage}">[${i}]</a>
  	  		</c:otherwise>
  	  	</c:choose>
  	  </c:forEach>
  	 
  	  <!-- 다음 -->
  	  <c:if test="${endpage<pagecount}"> 
  	   <a href="boardList.do?pageNum=${startpage+10}">[다음]</a>
  	  </c:if> 
     </td>
   </tr>
   
   
   <tr align="center">
  	 <td colspan="6">
  	  <form name="myform">
  	  	  검색 :     
  	  	 <select name="keyfield" onchange="clearText();">  
  	  	 	<option value="">-----선택하세요----- </option>
  	  	 	<option value="name"   <c:if test="${skey eq 'name'}">selected</c:if> > 하비이름필드 </option>
  	  	 	<option value="title"  <c:if test="${skey eq 'title'}">selected</c:if> > 하비제목필드 </option>
  	  	 	<option value="content" <c:if test="${skey eq 'content'}">selected</c:if> > 하비내용필드 </option>
  	  	 	<option value=""> 전체출력 </option>
  	  	 </select>
  	  	 <input type="text" name="keyword" value="${sval}" size=10>
  	  	 <input type="submit" value="검색">
  	  </form>
  	</td>
   </tr>     
</table>

 <p>
 	<a href="index.jsp">[index.jsp]</a>
    <a href="boardWrite.do">[게시판등록]</a>
    <a href="boardList.do">[전체출력]</a> 
</body>
</html>




 

 

| BoardDAO.java

public List<BoardDTO>  boardSelect(int start, int end) { 
		BoardDTO dto = new BoardDTO();
		dto.setStart(start);
		dto.setEnd(end);
		List<BoardDTO> list = temp.selectList("board.selectAll", dto) ;
		return list;
	}//end

 

 

| BoardDTO.java

package net.hb.crud;

import org.springframework.web.multipart.MultipartFile; //새로추가

public class BoardDTO {
	private int sabun;
	private String name;
	private String title;
	private String content;
	private String gender;
	private int pay;
	private int rn;
	private java.util.Date  wdate;
	private int hit;
	private String email;
	
	private int mid;

	private  int hobby_idx; //추가
	private  String hobby; //추가
	private  String img_file_name ; //추가
	private  MultipartFile upload_f ;  //추가 <input type=file name=uplaod_f

	
	public int getMid() {
		return mid;
	}
	public void setMid(int mid) {
		this.mid = mid;
	}
	public int getHobby_idx() { return hobby_idx;}
	public void setHobby_idx(int hobby_idx) {this.hobby_idx = hobby_idx;}
	public String getHobby() {return hobby;}
	public void setHobby(String hobby) {this.hobby = hobby;}
	public String getImg_file_name() {return img_file_name;}
	public void setImg_file_name(String img_file_name) {this.img_file_name = img_file_name;	}
	public MultipartFile getUpload_f() {return upload_f;}
	public void setUpload_f(MultipartFile upload_f) {this.upload_f = upload_f;	}


	public int getSabun() {return sabun;}
	public void setSabun(int sabun) {this.sabun = sabun;}
	public java.util.Date getWdate() {return wdate;	}
	public void setWdate(java.util.Date wdate) {this.wdate = wdate;	}
	public int getHit() {return hit;}
	public void setHit(int hit) {this.hit = hit;}
	public String getEmail() {return email;}
	public void setEmail(String email) {this.email = email;}

	public int getPay() {return pay;}
	public void setPay(int pay) {this.pay = pay;}
	public int getRn() {return rn;}
	public void setRn(int rn) {this.rn = rn;}

	public String getName() {return name;}
	public void setName(String name) {this.name = name;	}
	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 String getGender() {	return gender;	}
	public void setGender(String gender) {	this.gender = gender;}


	//페이징,검색
	private int start, end;  
	private String skey, sval;
	private int  rcnt;

	public int getStart() {return start;}
	public void setStart(int start) {this.start = start;}
	public int getEnd() {return end;	}
	public void setEnd(int end) {	this.end = end;	}
	public String getSkey() {	return skey;	}
	public void setSkey(String skey) {	this.skey = skey;	}
	public String getSval() {return sval;	}
	public void setSval(String sval) {this.sval = sval;	}
	public int getRcnt() {return rcnt;	}
	public void setRcnt(int rcnt) {this.rcnt = rcnt;} 
}//class END

 

 

| Board.xml

<select id="selectAll" resultType="net.hb.crud.BoardDTO" parameterType="net.hb.crud.BoardDTO">
	   	select * from (select  rownum rn, h.* ,(
		select count(*) from hobby_reply r where r.hobby_idx=h.hobby_idx
		) as rcnt from hobby h
		) where rn between #{dto.start} and #{dto.end}
</select>