您的位置:首页 > Web前端 > JavaScript

jsp分页组件的使用

2011-04-21 10:16 274 查看
可以单独写一个分页组件 eg:

 
<%
int cp = Integer.parseInt(request.getParameter("cp"));
int ls = Integer.parseInt(request.getParameter("ls"));
int count = Integer.parseInt(request.getParameter("count"));
String URL = request.getParameter("URL");
int allPage = (count -1)/ls +1;
int[] allLs = {2,5,10,20,50};
%>

<center style="font-size:13px; " mce_style="font-size:13px; ">
<form action="<%=URL %>"  id="myform">
<input type="button" value="首页" onclick="changeCp(1);" />
<input type="button" value="上一页" <%=cp == 1 ? "disabled" : ""%> onclick="changeCp(<%=cp-1 %>);" />
<input type="button" value="下一页" <%=cp == allPage ? "disabled" : ""%> onclick="changeCp(<%=cp+1 %>);" />
<input type="button" value="末页" onclick="changeCp(<%=allPage %>);" />
跳转到<input type="text" value="<%=cp%>" name="cp" size="2" id="cp"/>/<%=allPage %>
<input type="submit" value="转到" />
每页显示
<select name="ls" onchange="changeLs();">
<%
for(int i=0;i<allLs.length;i++){
%>
<option value="<%=allLs[i] %>" <%=allLs[i]==ls?"selected":"" %>><%=allLs[i] %></option>
<%
}
%>
</select>
条记录
</form>
</center>
</body>
<script type="text/javascript">

function changeLs() {
document.getElementById("cp").value = 1;
document.getElementById("myform").submit();
}
function changeCp(cp) {
document.getElementById("cp").value = cp;
document.getElementById("myform").submit();
}
</script>
 

 

 

此组件动态包含于含有列表的jsp中。

eg :list.jsp   此页面中要定义出cp,ls,count,其中count需要在EmpDAOImpl中获得,传进参数cp,ls使用数据库

真分页查找 (sqlserver中用top关键字),在页面中注意动态包含之前也有 cp=request.getParameter("cp")

..一定要注意,否则无法跳转,因为cp,ls,有默认值了

 

 

为了复习方便,把list.jsp贴出来

 

<table style="BORDER-COLLAPSE: collapse; font-size:13px;" mce_style="BORDER-COLLAPSE: collapse; font-size:13px;" bordercolor="#FF6600" height=40 cellPadding=1 width=600 align=center border=1>
<tr bgcolor="#F6F6F6" align="center" style=" font-weight:bold;" mce_style=" font-weight:bold;" >
<td>职工号</td>
<td>姓  名</td>
<td>性  别</td>
<td>年  龄</td>
<td>工  资</td>
<td>住  址</td>
<td>删 除</td>
<td>更  新</td>
</tr>

<%
int cp = 1;
int ls = 5;
try{
cp = Integer.parseInt(request.getParameter("cp"));
ls = Integer.parseInt(request.getParameter("ls"));
}catch(NumberFormatException e) {
}
Map map = ServiceFactory.getEmpServiceInstance().findAllSplit(cp,ls);
List<Emp> allEmp = (List)map.get("allEmp");
int count = (Integer)map.get("count");

%>
<jsp:include page="split.jsp">
<jsp:param value="<%=cp %>" name="cp"/>
<jsp:param value="<%=ls %>" name="ls"/>
<jsp:param value="<%=count %>" name="count"/>
<jsp:param value="select.jsp" name="URL"/>
</jsp:include>
<%
if(allEmp != null) {
Iterator<Emp> it = allEmp.iterator();
while(it.hasNext()) {
Emp emp = it.next();
%>
<tr align="center">
<td><%=emp.getEno()%></td>
<td><%=emp.getEname()%></td>
<td><%=emp.getEsex()%></td>
<td><%=emp.getEage()%></td>
<td><%=emp.getEwage()%></td>
<td><%=emp.getElocal()%></td>
<td><a href="delete.jsp?=<%=emp.getEno() %>" onclick="return window.confirm('确认删除?');">删除</a></td>
<td><a href="update.jsp?=<%=emp.getEno() %>">修改</a></td>
</tr>
<%
}
}
%>
</table><br />
</div>
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息