//接口的定义:
public interface contactbean {
public int getavailablecount()throws exception;
public int getavailablecount(int id)throws exception;
public pagebean listdata(string page)throws exception ;
public pagebean listdata(string page,int id)throws exception ;
public collection getresult()throws exception;
}
//pagebean的定义:
public class pagebean {
public int curpage;//当前的页值。
public int maxpage;//最大的页数。
public int maxrowcount;//最大的行数。
public static int rowsperpage=10; //控制分页的行数。
public java.util.collection data; //所要显示的数据的集合。
public pagebean() {
}
public void countmaxpage(){
if(this.maxrowcount%this.rowsperpage==0){
this.maxpage=this.maxrowcount/this.rowsperpage;
}else{
this.maxpage=this.maxrowcount/this.rowsperpage+1;
}
}
public collection getresult(){
return this.data;
}
public pagebean(contactadapter bean,int id)throws exception{
this.maxrowcount=bean.getavailablecount(id);
if(maxrowcount==0)
maxrowcount=1;
system.out.println(maxrowcount);
this.data=bean.getresult();
this.countmaxpage();
}
public pagebean(contactadapter bean)throws exception{
this.maxrowcount=bean.getavailablecount();
this.data=bean.getresult();
this.countmaxpage();
}
}
//对contactbean接口进行adapter模式的封装。方便实用。
public class contactadapter implements contactbean{
public contactadapter() { }
public int getavailablecount()throws exception{
return 0;
}
public int getavailablecount(int id)throws exception{
return 0;
}
public pagebean listdata(string page)throws exception {
return null;
}
public pagebean listdata(string page,int id)throws exception {
return null;
}
public collection getresult()throws exception{
return null;
}
}
在使用中你可以实现com.cn.interfaces.contactbean 或继承 com.cn.interfaces.contactadapter都可以。
实现方法:
实现一:其中不需要键入任何参数。
public int getavailablecount()throws exception;
public pagebean listdata(string page)throws exception ;
public collection getresult()throws exception;
实现二:需要键入参数。
public int getavailablecount(int id)throws exception;
public pagebean listdata(string page,int id)throws exception ;
public collection getresult()throws exception;
例如:
public class testcontact extends contactadapter{
private collection data;
public int getavailablecount(int unitid) throws exception{
bsiresultset rs = new dbutil().getresult(“
select count(*) num from tabletest where unit_id ="+id);
return rs.getint(num);
}
public pagebean listdata(string page,int incept_sort_id)throws exception {
pagebean p = new pagebean(this,incept_sort_id);
int pagenum = integer.parseint(page);
bsiresultset rs = con.getresult(
sql, ( pagenum -1)* p.rowsperpage,, pagenum*p.rowsperpage );
p.curpage = pagenum;
p.data = this.gettestbean(rs);
//此方法是把查询的数据封装在一个collection中。
return p;
}
public collection getresult()throws exception{
return this.data;
}
}
其中testbean是封装好的数据bean。
如果没有参数的话。可以使用没有参数的一组接口。
例如:在jsp中如何是用借口。
<jsp:usebean id="pagectl" class="com.cn.page.pagebean" scope="request"/>
<%
vector c=(vector)pagectl.getresult();
iterator it=c.iterator();
while(it.hasnext()){
testbean d=(testbean)it.next();//的到每一个单独的数据。
//可以取出此bean的属性值。
%>
。。。。。//属性使用。业务逻辑。
<form action="test.do" method="post" name="pageform">
<%out.print(viewpage.getview(pagectl));%>
</form>
//显示分页的表单。
加入javascript的page.js.在jsp页面上加入:
<script language="javascript" src="js/page.js">即可。
function jumping(){
document.pageform.submit();
return;
}
function gotopage(pagenum){
document.pageform.jumppage.value=pagenum;
document.pageform.submit();
return;
闽公网安备 35060202000074号