首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp分页困惑,该怎么处理

2012-01-03 
jsp分页困惑------自定义标签类packagecom.item.tagimportjava.sql.Connectionimportjava.sql.ResultSet

jsp分页困惑
------自定义标签类

package   com.item.tag;

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

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

import   com.item.bean.ConnectionDatabase;

public   class   page   implements   BodyTag
{
HttpServletRequest   request   ;
PageContext     context   ;
BodyContent   body   ;

public   void   getPage()
{
Connection   con   =   null;
Statement   st   =   null;
ResultSet   rs   =   null;
ResultSetMetaData   rms   =   null;
JspWriter   out   =   context.getOut();
int   j   =   0;

int   pagesize   =   6;//每页显示的行数
int   pagesum   =   0;//总页数
int   dangqianpage   =   0;
int   sumrow   =   0;//总行数
con   =   ConnectionDatabase.conn();

String   dq   =   request.getParameter( "page ");

if(dq   ==   null)
dangqianpage   =   1;
else
dangqianpage   =   Integer.parseInt(dq);
if(dangqianpage   <   1)
dangqianpage   =   1;

String   sql   =   "select   *   from   [firstItem].[dbo].[userManager] ";
try
{
st   =   con.createStatement();
rs   =   st.executeQuery(sql);
int   cols   =   rms.getColumnCount();

sumrow   =   rs.getRow();//huo   qu   zong   hang   shu  

pagesum   =   (sumrow   +   pagesize   -   1   )/   pagesize   ;//huo   qu   zong   ye   shu  

//如过当前显示的页大于总页数就把当前页设为总页数
if   (dangqianpage   >   pagesum)
dangqianpage   =   pagesum;

out.print( " <table   border   =   2   <tr> ");
for   (int   i   =   1;   i   <=   cols;   i++)
{
out.print( " <th> "   +   rms.getCatalogName(i)   +   " </th> ");

}
out.print( " </tr> ");

if   (dangqianpage   <   0)

//将记录指针定位到待显示页的第一条记录上  
rs.absolute((dangqianpage   -   1   *   dangqianpage   +   1));

while(j   <   dangqianpage   &&   !rs.isAfterLast())
{
out.print( " <tr> ");
for   (int   i   =   1;   i   <   cols   +   1;   i++)
{
out.print( " <td> "   +   rs.getString(i)   +   " </td> ");
}
out.print( " </tr> ");
}
out.print( " </table> ");

rs.next();
j++;

out.print( "第 "   +   dangqianpage   +   "共 "   +   pagesum   +   " <br/> ");
out.print( " <a   href   =   page1.jsp   ?   page   =   1> 第一页 </a> ");


if   (dangqianpage   >   1)
out.print( " <a   href   =   page1.jsp   ?   page   =   "   +(   dangqianpage   -   1)
+   "   > "+ "上一页 ");
if   (dangqianpage   <   pagesum)
out.print( " <a   href   =   page1.jsp   ?   page   =   "   +(   dangqianpage   +   1)
+   "   > "+ "下一页 ");

out.print( " <a   href   =   page1.jsp   ?   page   =   pagesum   > 最后一页 </a> ");


rs.close();
st.close();
con.close();
}
catch(Exception   e)
{
e.printStackTrace();
}
}


public   int   doStartTag()   throws   JspException
{
getPage();

return   this.SKIP_BODY;
}  
public   int   doEndTag()   throws   JspException
{

return   this.EVAL_PAGE;
}

public   void   setPageContext(PageContext   arg0)
{
this.context   =   arg0;
}

public   void   setBodyContent(BodyContent   arg0)
{
this.body   =   arg0;

}

public   void   doInitBody()   throws   JspException
{
//   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   setParent(Tag   arg0)
{
//   TODO   Auto-generated   method   stub

}

}


---JSP文件

<%@   page   language= "java "   import= "java.util.* "   pageEncoding= "ISO-8859-1 "%>
<%@   taglib   uri   =   "www.kengni.com1 "   prefix   =   "page "   %>
<%
String   path   =   request.getContextPath();
String   basePath   =   request.getScheme()+ ":// "+request.getServerName()+ ": "+request.getServerPort()+path+ "/ ";
%>

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">
<html>
    <head>
        <base   href= " <%=basePath%> ">
       
        <title> My   JSP   'page1.jsp '   starting   page </title>
       
<meta   http-equiv= "pragma "   content= "no-cache ">
<meta   http-equiv= "cache-control "   content= "no-cache ">
<meta   http-equiv= "expires "   content= "0 ">        
<meta   http-equiv= "keywords "   content= "keyword1,keyword2,keyword3 ">
<meta   http-equiv= "description "   content= "This   is   my   page ">


<!--
<link   rel= "stylesheet "   type= "text/css "   href= "styles.css ">
-->

    </head>
   
    <body>
          <form   name   =   "from2 "   action   =   "# "   >
          <page:page> </page:page>
</form>
    </body>
</html>

---配置文件

<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<taglib   version= "2.0 "   xmlns= "http://java.sun.com/xml/ns/j2ee "   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "   xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd   ">
    <tlib-version> 1.1 </tlib-version>
    <short-name> NMTOKEN </short-name>
      <uri> www.kengni.com1 </uri>
     
     
          <tag>
      <name> page </name>
      <tag-class> com.item.tag.page </tag-class>
      <body-content> JSP </body-content>
      </tag>
     
</taglib>

---运行是出现好多异常
有于发布长度有限只给出几个异常
--严重:   Javac   exception  
Compile   failed;   see   the   compiler   error   output   for   details.
...
...
--严重:   Env:   Compile:   javaFileName=/C:/tomcat5.0/work/Catalina/localhost/MyFirstItem//org/apache/jsp\fenye_jsp.java
--com.microsoft.sqlserver.jdbc.SQLServerException:   Incorrect   syntax   near   '? '.
...



[解决办法]
帮顶
[解决办法]
代码实在是太长了
[解决办法]
是不是对每个数据集都要写一个标签呢?有的数据有不同的显示方式,不一定是按顺序输出
也不一定全部输出,感觉用标签不爽。
[解决办法]
为什么要写专门的标签呢
自己写个算法不就好了吗 呵呵 真不明白

热点排行