SSM 分页

JSP

<table width="95%" border="0" cellpadding="0" cellspacing="0"
		class="table02" align="center">
		<tr>
			<td width="37%" align="right"><div align="left">div>td>
			<td width="63%" height="30" align="right"><img
				src="../../images/1.gif" width="4" height="5" align="absmiddle">
				<a href="${pageContext.request.contextPath }/flee/findpage.do?num=1">首页a>
				<img src="../../images/2.gif" width="3" height="5" align="absmiddle">
				<a
				if test="${sessionScope.meetingRoomPage.hasLastPage == true }">href="${pageContext.request.contextPath }/flee/findpage.do?num=${sessionScope.meetingRoomPage.pageNow - 1}"c:if>
				<c:if test="${sessionScope.meetingRoomPage.hasLastPage == false }">href="#"c:if>>上一页a>
				<img src="../../images/2-2.gif" width="3" height="5"
				align="absmiddle"> <a
				if test="${sessionScope.meetingRoomPage.hasNextPage == true }">href="${pageContext.request.contextPath }/flee/findpage.do?num=${sessionScope.meetingRoomPage.pageNow + 1}"c:if>
				<c:if test="${sessionScope.meetingRoomPage.hasNextPage == false }">href="#"c:if>>下一页a>
				<img src="../../images/3.gif" width="4" height="5" align="absmiddle">
				<a
				href="${pageContext.request.contextPath }/flee/findpage.do?num=${sessionScope.meetingRoomPage.totalPage}">末页a>
				第 ${sessionScope.meetingRoomPage.pageNow }/${sessionScope.meetingRoomPage.totalPage }
				页 ${sessionScope.meetingRoomPage.totalCount } 条记录td>
		tr>
	table>
	<table width="95%" border="0" align="center" cellpadding="2"
		cellspacing="0" class="table01">
		<tr>
			<td class="td_top">会议室名称td>
			<td class="td_top">会议室描述td>
			<td width="15%" class="td_top">会议室照片td>
			<td width="10%" class="td_top">修改td>
			<td width="10%" class="td_top">删除td>
		tr>
		<c:forEach items="${sessionScope.meetingRoomPage.data }" var="mr"
			varStatus="vs">
			<c:if test="${empty mr }">
				<tr>
					<td class="td_02">没有查找到相应信息td>
				tr>
			c:if>
			<c:if test="${not empty mr}">
				<tr>
					<td class="td_02">${mr.roomName }td>
					<td class="td_02">${mr.roomContent }td>
					<td class="td_02"><a
						href="${pageContext.request.contextPath }/flee/returnurl.do?url=${mr.roomPic }"><img
							src="../../images/article_common.gif" width="7" height="15">a>td>
					<td class="td_02"><a
						href="${pageContext.request.contextPath }/flee/findmeetingroombyid.do?id=${mr.id }">修改a>td>
					<td class="td_02"><a
						href="${pageContext.request.contextPath }/flee/removemeetingroombyid.do?id=${mr.id }">删除a>td>
				tr>
			c:if>

		c:forEach>

	table>

Page类

public class Page<T> {
	private Integer pageNow;//当前是第几页
	private Integer totalCount;//一共有多少条记录
	private Integer totalPage;//一共有几页
	private Integer pageSize;//一页有多少条记录
	private List<T> data;
	private boolean hasNextPage;//有没有下一页
	private boolean hasLastPage;//有没有上一页
	private Integer beginIndex;
	private Integer endIndex;
	public Integer getBeginIndex() {
		
		return (this.pageNow - 1) * this.pageSize;
	}
	public Integer getEndIndex() {
		return this.pageNow * this.pageSize;
	}
	public boolean isHasLastPage() {
		if(this.pageNow == 1){
			return false;
		}else{
			return true;
		}
	}
	public Integer getPageNow() {
		return pageNow;
	}
	public void setPageNow(Integer pageNow) {
		this.pageNow = pageNow;
	}
	public Integer getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}
	public Integer getTotalPage() {
		if(this.totalCount % this.pageSize == 0){
			return this.totalCount / this.pageSize;
		}else{
			return this.totalCount / this.pageSize + 1;
		}
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public List<T> getData() {
		return data;
	}
	public void setData(List<T> data) {
		this.data = data;
	}
	public boolean isHasNextPage() {
		if(this.pageNow == this.getTotalPage()){
			return false;
		}else{
			return true;
		}
	}
}

在Page类的代码里有一些成员变量是没有实际意义的,只是为了在用编译器补全代码的时候方便,而且可以用来找bug

实体类

public class MeetingRoom {
	private Integer id;
	private String roomName;
	private String roomContent;
	@Override
	public String toString() {
		return "MeetingRoom [id=" + id + ", roomName=" + roomName + ", roomContent=" + roomContent + ", roomPic="
				+ roomPic + "]";
	}
	private String roomPic;
	public String getRoomPic() {
		return roomPic;
	}
	public void setRoomPic(String roomPic) {
		this.roomPic = roomPic;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getRoomName() {
		return roomName;
	}
	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}
	public String getRoomContent() {
		return roomContent;
	}
	public void setRoomContent(String roomContent) {
		this.roomContent = roomContent;
	}
	
	
}

controller

@RequestMapping("/findpage.do")
	public String findPage(int num, HttpServletRequest request){
		Page<MeetingRoom> page = service.findPageByNumber(num);
		request.getSession().setAttribute("meetingRoomPage", page);
		return "redirect:/html/meeting/meetingRoomManage.jsp";
	}

service

public Page<MeetingRoom> findPageByNumber(int num) {
		Page<MeetingRoom> page = new Page();
		page.setPageSize(3);
		page.setPageNow(num);
		List<MeetingRoom> list = dao.selectPageByNumber(page);
		page.setData(list);
		Integer totalCount = dao.selectMeetingRoomCount();
		page.setTotalCount(totalCount);
		return page;
	}

IMeetingRoomDao.xml

<select id="selectPageByNumber" parameterType="Page" resultMap="MeetingRoomMap">
	select ID,room_name,room_content,room_pic from meeting_room limit #{beginIndex},#{pageSize}
select>
	
<select id="selectMeetingRoomCount" resultType="java.lang.Integer">
	select count(ID) from meeting_room
select>

你可能感兴趣的:(ssm)