jsp+mybatis+struts+spring+分页

xml文件  分页查询:
<select id="fenyeFind" resultMap="sBookResultMap" parameterType="int">
		select * from sbook order by id 
		<if test="beginResult >= 0">
		<if test="resultSize > 0">
			limit #{beginResult},#{resultSize};
		</if>
		</if>
	</select>


service的实现类:
public List<SBook> fenyeFind(Integer beginResult, Integer resultSize) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("beginResult", beginResult);
		map.put("resultSize", resultSize);
		return bookMapper.fenyeFind(map);
	}

其中的bookMapper是一个接口。

分页action:
/**
	 * 分页的action
	 * @return success;
	 */
	public String fenyeShow() {
		int count = bookServices.findAllBook().size();
		int b = 1;
		if(getBegin() > 0) {
			b = getBegin();//设置当前行数
		}
		resultSize = 3;//每页显示的条数
		total = count / resultSize + (count % resultSize == 0 ? 0 : 1);
		for(int i = 1; i <= total; i ++) {
			pages.add(i);//把总行数添加到集合中
		}
		ActionContext.getContext().put("pagesize", pages);//把集合放到上下文对象中
		int a = (b - 1) * resultSize;//设置开始查询的数据
		bookLst = bookServices.fenyeFind(a, resultSize);
		return "success";
	}


分页jsp1:
<%
				//(int[])request.getAttribute("pagesize");
			List<Integer> pa = (List<Integer>)ActionContext.getContext().get("pagesize");
				
				for(int i = 1; i <= pa.size(); i ++) {
			%>
			
			<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=<%=i %>"><%=i %></a>
			<%} %><br>
			
			<br />
				<jsp:include page="childFenye.jsp"></jsp:include>


分页jsp2:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<script type="text/javascript">
	function gotoFenyeAction() {
		var id = document.getElementById("fenyeForms");
		id.submit();
	}
</script>
	<form action="<%=request.getContextPath()%>/sbook/fenye.action" method="post" id="fenyeForms">
		<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=1">首页</a>
		<c:if test="${begin>1}">
			<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=${begin-1}">上一页</a>
		</c:if>
		跳转到第 <select name="begin" onchange="gotoFenyeAction();">
			<c:forEach begin="1" end="${total}" step="1" var="pageIndex">
				<c:choose>
					<c:when test="${pageIndex eq begin}">
						<option value="${pageIndex}" selected="selected">${pageIndex}</option>
					</c:when>
					<c:otherwise>
						<option value="${pageIndex}">${pageIndex}</option>
					</c:otherwise>
				</c:choose>
			</c:forEach>
		</select>页
		<c:choose>
			<c:when test="${begin == null || begin == 0 }">
				<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=${begin + 2}">下一页</a>
			</c:when>
			<c:when test="${begin < total }">
				<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=${begin + 1}">下一页</a>
			</c:when>
		</c:choose>
		</c:if>
		<a href="<%=request.getContextPath()%>/sbook/fenye.action?begin=${total}">末页</a>
	</form>

你可能感兴趣的:(mysql,mybatis,struts,分页)