读书人

怎么用taglib(自定义标签)分页显示

发布时间: 2012-02-29 16:44:10 作者: rapoo

请教高手如何用taglib(自定义标签)分页显示
package bank.based.network.taglib;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

import bank.based.network.bean.JDBCCon;

public class TranLog implements BodyTag
{

public void getPage()
{
Connection con = null;
ResultSet rs = null;
int pagesize = 10;//每页显示十条记录

try
{
Statement st = con.createStatement();

con = JDBCCon.getConnection();

String sql = "select count(*) from transfermoney_log ";

rs = st.executeQuery(sql);

int pagesum = rs.getRow();//查询出数据库中的总记录

int page = pagesum/pagesize;//的到多少页
      






}
catch(Exception e)
{
e.printStackTrace();
}
}
public int doEndTag() throws JspException
{
// TODO Auto-generated method stub
return 0;
}

public int doStartTag() throws JspException
{
// TODO Auto-generated method stub
return 0;
}
public void doInitBody() throws JspException
{
// TODO Auto-generated method stub

}

public void setBodyContent(BodyContent arg0)
{
// TODO Auto-generated method stub

}

public int doAfterBody() throws JspException
{
// TODO Auto-generated method stub
return 0;
}

public Tag getParent()
{
// TODO Auto-generated method stub
return null;
}

public void release()
{
// TODO Auto-generated method stub

}

public void setPageContext(PageContext arg0)
{
// TODO Auto-generated method stub

}

public void setParent(Tag arg0)
{
// TODO Auto-generated method stub

}

}

我的问题就是:

查询出数据库中的几百条数据

每十条为一页

int page = pagesum/pagesize得到的不一定整数页数
怎么保持显示的一致性


不在JSP里写JAVA代码
都写在taglib(自定义标签)里

请教高手这个程序应该怎么改


[解决办法]
直接使用JSTL
[解决办法]
我这边是用structs+taglib做的翻页,其中数据显示用structs标签,而当前页数的显示,及上一页,下一页的控制都用taglib做的自定义标签.jsp代码如下:
<logic:present name= "List1 " scope= "request ">
<logic:iterate name= "List1 " id= "List1 " type= "structs.query.QueryForm ">
<tr bgcolor= "#ffffff ">
<td> <bean:write name= "List1 " property= "fid1 "/> </td>


<td> <bean:write name= "List1 " property= "fckid "/> </td>
<td> <bean:write name= "List1 " property= "frefsite "/> </td>
<td> <bean:write name= "List1 " property= "fentertime "/> </td>
<td> <bean:write name= "List1 " property= "fremoteip "/> </td>
<td> <bean:write name= "List1 " property= "farea "/> </td>
<td> <bean:write name= "List1 " property= "farea2 "/> </td>
<td> <bean:write name= "List1 " property= "furl "/> </td>
</tr>
</logic:iterate>
</logic:present>
<tr bgcolor= "#ffffff ">
<td colspan= "10 ">
<a href= "# " onclick= "pagego( ' <ht:page type= "previous "/> ') "> <bean:message key= "prepage "/> </a>
   
<a href= "# " onclick= "pagego( ' <ht:page type= "next "/> ') "> <bean:message key= "nextpage "/> </a>
   
<bean:message key= "currentpage "/> : <ht:page type= "current "/>
   
<bean:message key= "totalsnum "/> : <ht:totals type= "currentdaylog "/>
   
<input size= "2 " class= "bd " name= "jp " value= '1 '>
<input type=button onclick= "pagego(document.all.jp.value) " class= "bd " value= ' <bean:message key= "jumpto "/> '>
</td>
</tr>

<ht:page的是自定义标签,控制翻页的.
public class Page extends TagSupport {
private String type = null;

public void setType(String type) {
this.type = type;
}

public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
String page = pageContext.getRequest().getParameter( "page ");
String pages = " ";
if (page != null) {
if (Integer.parseInt(page) < 1) {
page = "1 ";
}
if (type.equals( "previous ")) {
pages = Integer.parseInt(page) - 1 + " ";
} else if (type.equals( "next ")) {
pages = Integer.parseInt(page) + 1 + " ";
} else if (type.equals( "current ")) {
pages = Integer.parseInt(page) + " ";
}
} else {
if (type.equals( "previous ")) {
pages = "1 ";
} else if (type.equals( "next ")) {
pages = "2 ";
} else if (type.equals( "current ")) {
pages = "1 ";
}
}
out.print(pages);
} catch (Exception e) {
throw new JspException(e);
}
return EVAL_PAGE;
}
}

读书人网 >Java Web开发

热点推荐