ssh 分页

  1、.java  文件

package test.s2sh;

public class Page {
private int start=0;
private int count=5;
private int total;

public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}

public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}


public String toString() {

StringBuffer sb = new StringBuffer();
sb.append("<a href=?page.start=0>first</a>");
sb.append("&nbsp;");
sb.append("<a href=?page.start="
+ ((start - count) <= 0 ? 0 : (start - count)) + ">pre</a>");
sb.append("&nbsp;");
sb.append("<a href=?page.start=" + ((start + count)>=(total-(total % count ))?(total-(total % count )):(start + count)) + ">next</a>");
sb.append("&nbsp;");
sb.append("<a href=?page.start=" + (total-(total % count )) + ">last</a>");

return sb.toString();
}


}


要说出的是,这个有个小bug,当total能整除count时,最后页什么都没有。


没有bug的可以参考这个


package page;

public class ForPage {



public static String splitPage(int start ,int countPage){
StringBuffer sb = new StringBuffer();
if(1 != start){
sb.append("<a href='?start="+1+"'>首页</a>");
sb.append("&nbsp;");
sb.append("<a href='?start="+(start-1)+"'>上一页</a>");
sb.append("&nbsp;");
}else{
sb.append("首页&nbsp;上一页");
sb.append("&nbsp;");
}
sb.append("第"+start+"/"+countPage+"页");
sb.append("&nbsp;");
if(countPage != start){
sb.append("<a href='?start="+(start+1)+"'>下一页</a>");
sb.append("&nbsp;");
sb.append("<a href='?start="+countPage+"'>尾页</a>");
sb.append("&nbsp;");
}else{
sb.append("下一页&nbsp;尾页");
sb.append("&nbsp;");
}
sb.append("跳转到");
sb.append("<select id='page' onChange='change()'>");
for (int i = 1; i <= countPage; i++) {
sb.append("<option ");
if(start == i){
sb.append("selected");
}
sb.append(" value='"+i+"'>");
sb.append("第"+i+"页");
}
sb.append("</select>");
return sb.toString();
}

public static String splitPage(int id ,int start ,int countPage){
StringBuffer sb = new StringBuffer();
if(1 != start){
sb.append("<a href='?id="+id+"&start="+1+"'>首页</a>");
sb.append("&nbsp;");
sb.append("<a href='?id="+id+"&start="+(start-1)+"'>上一页</a>");
sb.append("&nbsp;");
}else{
sb.append("首页&nbsp;上一页");
sb.append("&nbsp;");
}
sb.append("第"+start+"/"+countPage+"页");
sb.append("&nbsp;");
if(countPage != start){
sb.append("<a href='?id="+id+"&start="+(start+1)+"'>下一页</a>");
sb.append("&nbsp;");
sb.append("<a href='?id="+id+"&start="+countPage+"'>尾页</a>");
sb.append("&nbsp;");
}else{
sb.append("下一页&nbsp;尾页");
sb.append("&nbsp;");
}
sb.append("跳转到");
sb.append("<input type='hidden' id='userId' value='"+id+"' />");
sb.append("<select id='page' onChange='change()'>");
for (int i = 1; i <= countPage; i++) {
sb.append("<option ");
if(start == i){
sb.append("selected");
}
sb.append(" value='"+i+"'>");
sb.append("第"+i+"页");
}
sb.append("</select>");
return sb.toString();
}
}



2、贴 jsp页面。(其他配置及相关struts.xml ,applicationContext.xml等未贴出。)

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="GBK"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>s2sh</title>

<style type="text/css">
td{
width:100px;
text-align:center
}
a.adduser{
position:relative;
left:700px
}

</style>




</head>
<body align="center">

<div>
<table  align="center" cellspacing="0" border="1">


<tr bgcolor="#aaaaaa">

<td>userName</td>
<td>sex</td>
<td>address</td>
<td>age</td>
<td>update</td>
<td>delete</td>

</tr>

<s:iterator value="users">
<tr>

   <td><s:property value="userName" /></td>
   <td><s:property value="sex" /></td>
   <td><s:property value="address" /></td>
   <td><s:property value="age" /></td>
   <td><a href="show?user.id=<s:property value="id" />">update</a></td>
   <td><a href="delete?user.id=<s:property value="id" />">delete</a></td>  
   </tr>
</s:iterator>

<tr>
<td align="center" colspan="6"><s:property  escape="false" value="page" /></td>
</tr>

</table>

<br/>
<br/>
<br/>
<a class="adduser" href="add">Add User</a>

</div>

<s:debug/>
</body>
</html>











今天完成了ssh的增删改查。


很多代码还要多写,多练。























你可能感兴趣的:(ssh)