1. 程式人生 > >spring分頁

spring分頁

builder map constant highlight mouseover str cpn vat red

1.Brand 商品品牌類

public class Brand {
	private Integer id;
	private String name;
	private String description;
	private String imgUrl;
	private String webSite;
	private Integer sort;
	private Integer isDisplay;
	private Integer pageNo=1; //頁號
	private Integer startRow; //開始行
	private Integer pageSize = 10; //每頁數
	public String getAllUrl(){
		return Constants.IMAGE_URL + imgUrl;
	}
	public Integer getstartRow() {
		return startRow;
	}
	public void setstartRow(Integer startRow) {
		this.startRow = startRow;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.startRow = (pageNo-1)* pageSize; //計算一次開始行
		this.pageSize = pageSize;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.startRow = (pageNo-1)* pageSize; //計算一次開始行
		this.pageNo = pageNo;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getImgUrl() {
		return imgUrl;
	}
	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}
	public String getWebSite() {
		return webSite;
	}
	public void setWebSite(String webSite) {
		this.webSite = webSite;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public Integer getIsDisplay() {
		return isDisplay;
	}
	public void setIsDisplay(Integer isDisplay) {
		this.isDisplay = isDisplay;
	}
	@Override
	public String toString() {
		return "Brand [id=" + id + ", name=" + name + ", description=" + description + ", imgUrl=" + imgUrl
				+ ", webSite=" + webSite + ", sort=" + sort + ", isDisplay=" + isDisplay + "]";
	}

}

2.dao層

public interface BrandDao {
     public List<Brand> getBrandListWithPage(Brand brand); //得到滿足條件的所有品牌條目
     public int getBrandCount();//獲取總記錄數
}

3.service層

public interface BrandService {
    public Pagination getBrandListWithPage(Brand brand);
    public void addBrand(Brand brand); 
}

  

@Service
@Transactional
public class BrandServiceImpl implements BrandService{
    @Resource
     private BrandDao brandDao;
	@Transactional(readOnly = true)
	public Pagination getBrandListWithPage(Brand brand){
		// 1.起始頁  2.每頁記錄數 3.總記錄數
		Pagination pagination = new Pagination(brand.getPageNo(),brand.getPageSize(),brandDao.getBrandCount());
		pagination.setList(brandDao.getBrandListWithPage(brand));
		return pagination;
	}
}

4.controller層

@Controller
public class BrandController {
     @Autowired
     private BrandService brandService;
	@RequestMapping(value ="/brand/list.do")
	public String list(String name, Integer isDisplay, Integer pageNo ,ModelMap model){
		StringBuilder params = new StringBuilder();
		Brand brand = new Brand();
		if(StringUtils.isNotBlank(name)){  //="" 和" "都為空
			brand.setName(name);
			params.append("name=").append(name);
		}
		if(isDisplay!=null){
		    params.append("&").append("isDisplay=").append(isDisplay);	
			brand.setIsDisplay(isDisplay);
		}else{
		    params.append("&").append("isDisplay=").append(1);	
		    brand.setIsDisplay(1);
		}
		brand.setPageSize(5);
		//如果頁號是null或小於1 則重置為1
		brand.setPageNo(Pagination.cpn(pageNo));
		Pagination pagination = brandService.getBrandListWithPage(brand);
		//分頁展示: /brand/list.do?name=瑜伽樹&isDisplay=1&pageNo=2
		String url = "/brand/list.do";
		pagination.pageView(url, params.toString());
	    model.addAttribute("pagination",pagination);	//本質還是request.setAttribute();
	    model.addAttribute("name",name);
	    model.addAttribute("isDisplay",isDisplay);
		return "brand/list";
		//參數		
	}
}

5.jsp頁面

<table cellspacing="1" cellpadding="0" border="0" width="100%" class="pn-ltable">
	<thead class="pn-lthead">
		<tr>
			<th width="20"><input type="checkbox" onclick="checkBox(‘ids‘,this.checked)"/></th>
			<th>品牌ID</th>
			<th>品牌名稱</th>
			<th>品牌圖片</th>
			<th>品牌描述</th>
			<th>排序</th>
			<th>是否可用</th>
			<th>操作選項</th>
		</tr>
	</thead>
	<tbody class="pn-ltbody">
		<c:forEach items="${pagination.list }" var="entry">
			<tr bgcolor="#ffffff" onmouseout="this.bgColor=‘#ffffff‘" onmouseover="this.bgColor=‘#eeeeee‘">
				<td><input type="checkbox" value="${entry.id }" name="ids"/></td>
				<td align="center">${entry.id }</td>
				<td align="center">${entry.name }</td>
				<td align="center"><img width="40" height="40" src="${entry.allUrl}"/></td>
				<td align="center">${entry.description }</td>
				<td align="center">${entry.sort }</td>
				<td align="center"><c:if test="${entry.isDisplay == 1 }">是</c:if><c:if test="${entry.isDisplay == 0 }">不是</c:if></td>
				<td align="center">
				<a class="pn-opt" href="#">修改</a> | <a class="pn-opt"  href="/brand/delete.do?id=${entry.id }&name=${name}&isDisplay=${isDisplay}">刪除</a>
				</td>
			</tr>
		</c:forEach>
	
	</tbody>
</table>
<div class="page pb15">
	<span class="r inb_a page_b">
		<c:forEach items="${pagination.pageView }" var="page">
			${page }
		</c:forEach>
	</span>
</div>

顯示結果:

技術分享圖片

  

spring分頁