SSM众筹网站手写分页,同步

jsp/main.jsp,发出请求

<a href="${APP_PATH}/user/index.do"><i class="glyphicon glyphicon-user"></i> 用户维护</a> 

 

1.Page类

import java.util.List;

public class Page {

	private Integer pageno;
	private Integer pagesize;
	private List datas;
	private Integer totalsize;
	private Integer totalno;

	public Page(Integer pageno, Integer pagesize) {
		if (pageno <= 0) {
			this.pageno = 1;
		} else {
			this.pageno = pageno;
		}
		if (pagesize <= 0) {
			this.pagesize = 10;
		} else {
			this.pagesize = pagesize;
		}
	}

	public Integer getPageno() {
		return pageno;
	}

	public void setPageno(Integer pageno) {
		this.pageno = pageno;
	}

	public Integer getPagesize() {
		return pagesize;
	}

	public void setPagesize(Integer pagesize) {
		this.pagesize = pagesize;
	}

	public List getDatas() {
		return datas;
	}

	public void setDatas(List datas) {
		this.datas = datas;
	}

	public Integer getTotalsize() {
		return totalsize;
	}

	public void setTotalsize(Integer totalsize) {
		this.totalsize = totalsize;
		this.totalno = (totalsize % pagesize) == 0 ? (totalsize / pagesize) : (totalsize / pagesize + 1);
	}

	public Integer getTotalno() {
		return totalno;
	}

	private void setTotalno(Integer totalno) {   //setTotalno,private不允许设置,totalno由totalsize计算得出
		this.totalno = totalno;
	}

	public Integer getStartIndex() {
		return (this.pageno - 1) * pagesize;
	}

}

2.UserController

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping("/index")
	//防止空指针异常,给默认值
	public String index(@RequestParam(value="pageno",required=false,defaultValue="1") Integer pageno,
			@RequestParam(value="pagesize",required=false,defaultValue="10") Integer pagesize,Map map){
		
		Page page = userService.queryPage(pageno, pagesize); //pageno当前页数,pagesize每页的数量
		
		map.put("page", page);  //自动放入session中供取出
		
		return "user/index";
	}

}

3.UserServiceImpl

	@Override
	public Page queryPage(Integer pageno, Integer pagesize) {
		
		Page page = new Page(pageno,pagesize);
		Integer startIndex = page.getStartIndex();
		
		List<User> datas = userMapper.queryList(startIndex, pagesize);
		
		page.setDatas(datas);
		
		Integer totalsize = userMapper.queryCount();
		page.setTotalsize(totalsize);
		
		return page;
	}

4.UserMapper.java

	List<User> queryList(@Param("startIndex") Integer startIndex, @Param("pagesize")  Integer pagesize);//多个参数需要指定mybatis才能注入

	Integer queryCount();

5.UserMapper.xml

	<select id="queryList" resultMap="BaseResultMap ">
		select id, loginacct, userpswd,
		username, email, createtime
		from t_user limit #{startIndex},#{pagesize}
	</select>

	<select id="queryCount" resultType="int">
		select count(*)
		from t_user 
	</select>

6.user/index.jsp

(1)表格显示数据


<thead>
	<tr>
		<th width="30">#</th>
		<th width="30"><input type="checkbox"></th>
		<th>账号</th>
		<th>名称</th>
		<th>邮箱地址</th>
		<th width="100">操作</th>
	</tr>
</thead>
<tbody>
	<!-- status当前状态,status.count当前值的位次,从1开始,status.index从0开始 -->
	<c:forEach items="${page.datas }" var="user" varStatus="status">
		<tr>
			<td>${status.count}</td>
			<td><input type="checkbox"></td>
			<td>${user.loginacct }</td>
			<td>${user.username }</td>
			<td>${user.email }</td>
			<td>
				<button type="button" class="btn btn-success btn-xs">
					<i class=" glyphicon glyphicon-check"></i>
				</button>
				<button type="button" class="btn btn-primary btn-xs">
					<i class=" glyphicon glyphicon-pencil"></i>
				</button>
				<button type="button" class="btn btn-danger btn-xs">
					<i class=" glyphicon glyphicon-remove"></i>
				</button>
			</td>
		</tr>
	</c:forEach>
</tbody>

(2)导航条,上一页,点击页数,下一页,当前页高亮


<tfoot>
	<tr>
		<td colspan="6" align="center">
			<ul class="pagination">
				<c:if test="${page.pageno==1 }">
					<li class="disabled"><a href="#">上一页</a></li>
				</c:if>
				<c:if test="${page.pageno!=1 }">
					<li><a href="#" onclick="pageChange(${page.pageno-1})">上一页</a></li>
    				</c:if>
                <%-- begin="1" end="${page.totalno }" var="num"
	 从第一个数值开始,迭代到totalno结束,num临时变量,当前页颜色标中
	 --%>
				<c:forEach begin="1" end="${page.totalno }" var="num">
					<li
						<c:if test="${page.pageno==num }">
                                                           class="active"
                                                        </c:if>><a
						href="#" onclick="pageChange(${num})">${num }</a></li>
				</c:forEach>

				<c:if test="${page.pageno==page.totalno }">
					<li class="disabled"><a href="#">下一页</a></li>
				</c:if>
				<c:if test="${page.pageno!=page.totalno }">
					<li><a href="#" onclick="pageChange(${page.pageno+1})">下一页</a></li>
				</c:if>

			</ul>
		</td>
	</tr>

</tfoot>

(3) function pageChange( pageno)

function pageChange( pageno ) {
    window.location.href = "${APP_PATH}/user/index.do?pageno="+pageno;
}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值