♥ 最新消息 ♥

♨ 好吃好玩,民宿體驗,試吃 ~ 歡迎合作邀約 

E-Mail : luckyboy7527@hotmail.com   LINE ID:raylee0527

歡迎加入粉絲專頁: 瑞與瑪姬 ♫ 趴趴走黑白吃 


[月子餐試吃心得] 玉膳坊,懷恩,藍田,康和,禾馨芽果,荷風,好婆婆,悅子

[長灘島] 五日遊行程 (持續更新中)

 [日本]大阪, 京都, 姬路, 奈良五日自由行

 [泰國]蘇美島蜜月旅行七日遊記

 [台灣]澎湖三日自由行

禁止改作,歡迎轉貼但請註明出處.

 

廣告贊助

今天筆記一下,Spring 分頁實作

首先資料來源我就不多說了, model 或是 dao 就自己弄

我這邊就簡單從拿到資料開始到產生分頁

(1)建立一個 PagingService , 這是我資料的來源

@Service("pagingService")
public class PagingService {
	
	@Autowired
	private ThwCitiesService thwCitiesService;
	
    public List<ThwCities> getPaginglist() {
        
        List<ThwCities> pagingList = new ArrayList<ThwCities>();
        
        ThwCities[] thwCitiesArray =  thwCitiesService.selectAll();
        
        for(int i = 0; i < thwCitiesArray.length; i++){
        	pagingList.add(thwCitiesArray[i]);   	
        }
        
        return pagingList;
    }
}

=======================================================

(2) 分頁controller

@Controller
public class MainController {
   
    @Autowired
    PagingService pagingService;
  
        @RequestMapping(value = {"/paging/{type}","/paging"})
        public ModelAndView all(@PathVariable Map<String, String> pathVariablesMap, HttpServletRequest req) {   
        
        PagedListHolder<ThwCities> productList = null;
        
        String type = pathVariablesMap.get("type");
        
        if(null == type) {
            // First Request, Return first set of list
            List<ThwCities> pagingList = pagingService.getPaginglist();
            
            productList = new PagedListHolder<ThwCities>();
            productList.setSource(pagingList);
            productList.setPageSize(8);
            
            req.getSession().setAttribute("phonesList",  productList);
        
            printPageDetails(productList);
            
        } else if("next".equals(type)) {
            // Return next set of list
            productList = (PagedListHolder<ThwCities>) req.getSession().getAttribute("phonesList");
            
            productList.nextPage();
            
            printPageDetails(productList);

        } else if("prev".equals(type)) {
            // Return previous set of list
            productList = (PagedListHolder<ThwCities>) req.getSession().getAttribute("phonesList");
            
            productList.previousPage();
            
            printPageDetails(productList);
            
        } else {
            // Return specific index set of list
             logger.info("type="+type);
            
            productList = (PagedListHolder<ThwCities>) req.getSession().getAttribute("phonesList");
            
            int pageNum = Integer.parseInt(type);
            
            productList.setPage(pageNum);
            
            printPageDetails(productList);
        }

        ModelAndView mv = new ModelAndView("test");
            
        return  mv;
        
        }
    
    <!--這段可有可無,只是紀錄動作-->
    @SuppressWarnings("rawtypes")
    private void printPageDetails(PagedListHolder productList) {
        
         logger.info("curent page - productList.getPage() :"+ productList.getPage());
         logger.info("Total Num of pages - productList.getPageCount :"+ productList.getPageCount());
         logger.info("is First page - productList.isFirstPage :"+ productList.isFirstPage());
         logger.info("is Last page - productList.isLastPage :"+ productList.isLastPage());
    }
}

=======================================================

(3) 分頁jsp  

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<spring:url value="/resources/css/style.css" var="style" />
<spring:url value="/paging" var="pageurl" />

</head>
<body>

	<div>
		<c:set var="pageListHolder" value="${phonesList}" scope="session" />
		<table cellspacing="0">
			<thead>
				<tr>
					<th>ID</th>
					<th>CitySort</th>
					<th>Name</th>
					<th>AREA</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="ph" items="${pageListHolder.pageList}">
					<tr>
						<td>${ph.uuid}</td>
						<td>${ph.citySort}</td>
						<td>${ph.cityName}</td>
						<td>${ph.cityarea}</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
	</div>
	<div>
		<span style="float: left;"> <c:choose>
				<c:when test="${pageListHolder.firstPage}">上一頁</c:when>
				<c:otherwise>
					<a href="${pageurl}/prev">上一頁</a>
				</c:otherwise>
			</c:choose>
		</span> 
		<span> 
			<c:forEach begin="0" end="${pageListHolder.pageCount-1}" varStatus="loop">&nbsp;&nbsp;
   		 		<c:choose>
					<c:when test="${loop.index == pageListHolder.page}">${loop.index+1}</c:when>
					<c:otherwise>
						<a href="${pageurl}/${loop.index}" >${loop.index+1}</a>
					</c:otherwise>
				</c:choose>
    			&nbsp;&nbsp;
   			 </c:forEach>
	   </span> 
	   <span> 
	   		<c:choose>
				<c:when test="${pageListHolder.lastPage}">下一頁</c:when>
				<c:otherwise>
					<a href="${pageurl}/next">下一頁</a>
				</c:otherwise>
			</c:choose>
		</span>
	</div>
</body>
</html>

 

style

<style>
html, body {
	height: 100%;
}

html {
	display: table;
	margin: auto;
	position: relative;
	min-width: 500px;
	min-height: 768px;
	height: 100%;
}

body {
	display: table-cell;
	font: 13px/20px 'Lucida Grande', Tahoma, Verdana, sans-serif;
	color: #404040;
	background: #ffffff;
	text-align: center;
}

div {
	margin-left: 10 px;
	text-align: left;
}

table {
	text-align: center;
	font-family: Arial, Helvetica, sans-serif;
	color: #000;
	font-size: 14px;
	text-shadow: 1px 1px 0px #fff;
	background: #ededed;
	margin: 20px;
	border: #ccc 1px solid;
	-moz-border-radius: 3px;
	-webkit-border-radius: 3px;
	border-radius: 3px;
	-moz-box-shadow: 0 1px 2px #d1d1d1;
	-webkit-box-shadow: 0 1px 2px #d1d1d1;
	box-shadow: 0 1px 2px #d1d1d1;
}

table th {
	padding: 21px 25px 22px 25px;
	border-top: 1px solid #fafafa;
	border-bottom: 1px solid #e0e0e0;
	background: #ededed;
	background: -webkit-gradient(linear, left top, left bottom, from(#ededed),
		to(#ebebeb));
	background: -moz-linear-gradient(top, #ededed, #ebebeb);
}

table a:link {
	color: #666;
	font-weight: bold;
	text-decoration: none;
}

table a:visited {
	color: #999999;
	font-weight: bold;
	text-decoration: none;
}

table a:active, table a:hover {
	color: #bd5a35;
	text-decoration: underline;
}

table tr:first-child th:first-child {
	-moz-border-radius-topleft: 3px;
	-webkit-border-top-left-radius: 3px;
	border-top-left-radius: 3px;
}

table tr:first-child th:last-child {
	-moz-border-radius-topright: 3px;
	-webkit-border-top-right-radius: 3px;
	border-top-right-radius: 3px;
}

table tr {
	text-align: left;
	padding-left: 20px;
}

table td:first-child {
	text-align: left;
	padding-left: 20px;
	border-left: 0;
}

table td {
	padding: 18px;
	border-top: 1px solid #ffffff;
	border-bottom: 1px solid #e0e0e0;
	border-left: 1px solid #e0e0e0;
	background: #ffffff;
	background: -webkit-gradient(linear, left top, left bottom, from(#fbfbfb),
		to(#fafafa));
	background: -moz-linear-gradient(top, #fbfbfb, #fafafa);
}

table tr.even td {
	background: #ffffff;
	background: -webkit-gradient(linear, left top, left bottom, from(#f8f8f8),
		to(#f6f6f6));
	background: -moz-linear-gradient(top, #f8f8f8, #f6f6f6);
}

table tr:last-child td {
	border-bottom: 0;
}

table tr:last-child td:first-child {
	-moz-border-radius-bottomleft: 3px;
	-webkit-border-bottom-left-radius: 3px;
	border-bottom-left-radius: 3px;
}

table tr:last-child td:last-child {
	-moz-border-radius-bottomright: 3px;
	-webkit-border-bottom-right-radius: 3px;
	border-bottom-right-radius: 3px;
}

table tr:hover td {
	background: #f2f2f2;
	background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2),
		to(#f0f0f0));
	background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}
</style>
創作者介紹
創作者 瑞、瑪姬與小昆妮 的頭像
瑞、瑪姬與小昆妮

瑞瑪姬與小昆妮♫趴趴走黑白吃

瑞、瑪姬與小昆妮 發表在 痞客邦 留言(1) 人氣()


留言列表 (1)

發表留言
  • xuezhongyu01
  • pagedlistholder对象有个弊端,我每次查询都要返回所有的数据,然后所有的数据中包含分页所需要的数据,感觉返回了很多冗余数据