??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩大胆人体,国产不卡免费视频,成人免费看片网站http://www.aygfsteel.com/Crying/category/26081.htmlZ(jin)自己zh-cnSat, 22 Sep 2007 13:12:58 GMTSat, 22 Sep 2007 13:12:58 GMT60hibernate分页1http://www.aygfsteel.com/Crying/articles/147398.htmlCryingCryingSat, 22 Sep 2007 05:53:00 GMThttp://www.aygfsteel.com/Crying/articles/147398.htmlhttp://www.aygfsteel.com/Crying/comments/147398.htmlhttp://www.aygfsteel.com/Crying/articles/147398.html#Feedback0http://www.aygfsteel.com/Crying/comments/commentRss/147398.htmlhttp://www.aygfsteel.com/Crying/services/trackbacks/147398.html  目的Q?/span>

学习(fn)使用struts+hibernate实现一个通用的分늨序?/span>

内容Q?/span>

    分页E序是网设计经帔R要实现的基本功能。但有的分页E序直接嵌在jsp面上,不仅工作量较大,代码也难以重用。本ơ试验将使用struts+hibernate来实现通用的分늨序。不同功能的代码量分离Q以满通用性要求?/span>

    首先需要徏立一个表Q如下Product,各字D设计如?可自由更?Q?/span>

    ID int primary key,

    Typeid varchar(20),

Name   varchar(50),

Price    varchar(20),

Memo   varchar(100).

步骤Q?/span>

建立web工程Q名字ؓ(f)”Fenye”.

2 dhibernateQ生?/span>Product表的.hbm.xml?/span>pojocR这个很单,不再赘述?/span>

3 接着写数据访问层Q我们将其分?/span>Dao?/span>ProDao两个cR其?/span>Dao是个公共的基c;ProDaol承它,q与action通信取得参数。这栯计的好处是:(x)如果需要对Userq个表进行分|C,只需d一?/span>UserDaoc,q让它?/span>DaocR?/span>

Daocȝ代码如下Q可以根据需要添加:(x)头文件自己导入?/span>

public class Dao {

        private Session session=null;

        public Dao() {

           }

          

           public Session getSession()

        {

             session = HibernateSessionFactory.getSession();

               return session;       

        }

          public int getCount(String pojo)

        {

             String sql="select count(*) from "+pojo ;

             this.getSession();

           try {

           Query q = getSession().createQuery(sql);

             List cc = q.list();

             Integer rows = (Integer) cc.get(0);

           return rows.intValue();

           } catch (HibernateException ex) {

               System.out.print("ex::"+ex.getMessage());

               return 0;

           }

        }

        public List getlist(Query query, String pojo,int pagesize,int currow) throws HibernateException

       {

             List list = null;

             this.getSession();

            query.setFirstResult(currow);

             query.setMaxResults(pagesize);

             list=query.list();

             //session.flush();

             if(session!=null)

                     session.close();     

             return list;

        }

        public List getlist(Query query,int pagesize,int currow)

              {                  

                      String[] str = query.getQueryString().split("from");       

                      String[] table =str[1].trim().split(" ");        

                      System.out.println("table:"+table[0]);

                      return getlist(query,table[0],pagesize,currow);         

              }

}

ProDaocȝ代码如下Q?/span>

public class PinfoDao extends Dao{

       private Session session;

       public PinfoDao() {

              super();

    }

       public List list(int pagesize,int currow)

       {

             

              Query query =getSession().createQuery("from PInfo");

              List li=getlist(query, pagesize, currow);

              return li;

             

       }

        public Session getSession()

        {

             // Configuration config=null;

          

             session = HibernateSessionFactory.getSession();

               return session; 

        }

       public int getCount()

       {

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

              Query q = getSession().createQuery(sql);

              List cc = q.list();

              Integer a = (Integer) cc.get(0);    

              System.out.println("count:"+a.intValue());

              return a.intValue();

       }    

}

4         下面写页面控制程序,同样代码分Mؓ(f)两个c?/span>PagecdPagehelpcR?/span>

Pagecd理页面相关的一些设|,如一|C多条记录Q计共有多页Q共有多记录,当前늠{?/span>

PageHelpcL?/span>jsp面传来的参敎ͼq调?/span>Pagecd理?/span>

PagecM码:(x)

public class Page {

        private int totalRows; //总行?/span>

           private int pageSize = 3; //每页昄的行?/span>

           private int currentPage; //当前号

           private int totalPages; //总页?/span>

           private int startRow; //当前在数据库中的v始行

           public Page(int totalRows1) {

            totalRows = totalRows1;

            totalPages=totalRows/pageSize;

            int mod=totalRows%pageSize;

            if(mod>0){

              totalPages++;

            }

            currentPage = 1;

            startRow = 0;

          }

          public int getStartRow() {

            return startRow;

          }

          public int getTotalPages() {

            return totalPages;

          }

          public int getCurrentPage() {

            return currentPage;

          }

          public int getPageSize() {

            return pageSize;

          }

          public void setTotalRows(int totalRows) {

            this.totalRows = totalRows;

          }

          public void setStartRow(int startRow) {

            this.startRow = startRow;

          }

          public void setTotalPages(int totalPages) {

            this.totalPages = totalPages;

          }

          public void setCurrentPage(int currentPage) {

            this.currentPage = currentPage;

          }

          public void setPageSize(int pageSize) {

            this.pageSize = pageSize;

          }

          public int getTotalRows() {

            return totalRows;

          }

          public void first() {

            currentPage = 1;

            startRow = 0;

          }

          public void previous() {

            if (currentPage == 1) {

              return;

            }

            currentPage--;

            startRow = (currentPage - 1) * pageSize;

          }

          public void next() {

            if (currentPage < totalPages) {

              currentPage++;

            }

            startRow = (currentPage - 1) * pageSize;

          }

          public void last() {

            currentPage = totalPages;

            startRow = (currentPage - 1) * pageSize;

          }

          public void refresh(int _currentPage) {

            currentPage = _currentPage;

            if (currentPage > totalPages) {

              last();

            }

          }

}

PagehelpcM码如下:(x)

public class Pagehelp {

        public static Page getPager(HttpServletRequest httpServletRequest,int totalRows) {

      //定义pager对象Q用于传到页?/span>

      Page pager = new Page(totalRows);

      //?/span>Request对象中获取当前页?/span>

      String currentPage = httpServletRequest.getParameter("currentPage");

      //如果当前号为空Q表CZؓ(f)首次查询该页

      //如果不ؓ(f)I,则刷?/span>page对象Q输入当前页L(fng)信息

      if (currentPage != null) {

        pager.refresh(Integer.parseInt(currentPage));

      }

      //获取当前执行的方法,首页Q前一,后一,N?/span>

      String pagerMethod = httpServletRequest.getParameter("pageMethod");

      if (pagerMethod != null) {

        if (pagerMethod.equals("first")) {

          pager.first();

        } else if (pagerMethod.equals("previous")) {

          pager.previous();

        } else if (pagerMethod.equals("next")) {

          pager.next();

        } else if (pagerMethod.equals("last")) {

          pager.last();

        }

      }

      return pager;

    }

}

5         一切准备工作结束后Q下面写action来调用这些类?/span>

List list = null;//用于输出到页面的记录集合

               int totalRows;//记录总行?/span>

               PinfoDao dao=new PinfoDao();

               totalRows=dao.getCount();

               System.out.print("总行?/span>=="+totalRows);

               Page page=Pagehelp.getPager(request,totalRows);

               try {

                      list= dao.list(page.getPageSize(), page.getStartRow());

               } catch (HibernateException ex) {

                   System.out.print("action里的错误="+ex.getMessage());

               }

               request.setAttribute("page",page);

               request.setAttribute("list",list);

               return mapping.findForward("list");

6         最后是昄面plist。在struts-config.xml文g中添?/span>forward语句Q?/span><forward name="list" path="/plist.jsp"></forward>?/span>

plist.jsp面部分代码如下Q?/span>

<table align="center" border="1">

   <tr>

   <td>产品cd</td>

   <td>产品名称</td>

   <td>产品h</td>

   <td>产品备注</td>

   </tr>

<!?/span>下面打印list中的各属?/span>-->

       <tr >

   <td colspan="4">

W?/span><bean:write name="page" property="currentPage"/>?/span>

?/span><bean:write name="page" property="totalPages" />?/span>

<html:link action="/page.do?pageMethod=first"

paramName="page" paramProperty="currentPage"

paramId="currentPage">首页</html:link>

   <html:link action="/page.do?pageMethod=previous"

paramName="page" paramProperty="currentPage"

paramId="currentPage">上一?/span></html:link>

   <html:link action="/page.do?pageMethod=next"

paramName="page" paramProperty="currentPage"

paramId="currentPage">下一?/span></html:link>

   <html:link action="/page.do?pageMethod=last"

paramName="page" paramProperty="currentPage"

paramId="currentPage">N</html:link>

</td>

</tr>        

</table>

7 试Q?/span>

按照上述步骤完成后,在浏览器中输?/span>http://localhost:8080/Fenye/page.do查看面?/span>



Crying 2007-09-22 13:53 发表评论
]]>
jsp分页http://www.aygfsteel.com/Crying/articles/147397.htmlCryingCryingSat, 22 Sep 2007 05:51:00 GMThttp://www.aygfsteel.com/Crying/articles/147397.htmlhttp://www.aygfsteel.com/Crying/comments/147397.htmlhttp://www.aygfsteel.com/Crying/articles/147397.html#Feedback0http://www.aygfsteel.com/Crying/comments/commentRss/147397.htmlhttp://www.aygfsteel.com/Crying/services/trackbacks/147397.html<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*" %>

<html>
 <head>
  <title>|上书店留言?lt;/title>
 </head>
 <body>
  <a href="say.html">我要留言</a><br>
  <%
   Context ctx=new InitialContext();
            DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
            Connection conn=ds.getConnection();
           
            //创徏可滚动的l果集?br />    Statement stmt=conn.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
   ResultSet rs=stmt.executeQuery("select * from guestbook order by gst_time desc");
   
   //Ud游标到结果集的最后一行?br />    rs.last();
   
   //得到当前行的行数Q也得C(jin)数据库中留言的L?br />    int rowCount=rs.getRow();
   if(rowCount==0)
   {
    out.println("当前没有M留言!");
    return;
   }
   
   String strCurPage=request.getParameter("page");
   
   //表示当前的页数?br />    int curPage;
   
   if(strCurPage==null)
    curPage=1;
   else
    curPage=Integer.parseInt(strCurPage);
   
   //定义每页昄的留a数?br />    int countPerPage=5;
   
   //计算昄所有留a需要的总页数?br />    int pageCount=(rowCount+countPerPage-1)/countPerPage;
   
   //Ud游标到结果集中指定的行。如果显C的是第一,curPage=1Q?br />    //游标Ud到第1行?br />    rs.absolute((curPage-1)*countPerPage+1);
   
      //如果是第1,则显CZ带链接的文字Q如果不是第1,
      //则给用户提供跌{到第一和上一늚链接?br />    if(curPage==1)
   { 
  %>
          W一?amp;nbsp;&nbsp;&nbsp;&nbsp;
          上一?amp;nbsp;&nbsp;&nbsp;&nbsp;
  <%
   }
   else
   {
  %>
          <a href="index.jsp?page=<%=1%>">W一?lt;/a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a href="index.jsp?page=<%=curPage-1%>">上一?lt;/a>
          &nbsp;&nbsp;&nbsp;&nbsp;
  <%
   }
   //如果当前|最后一,则显CZ带链接的文字Q如果不是最后一,
   //则给用户提供跌{到最后一和下一늚链接?br />    if(curPage==pageCount)
   {
   
  %>
          下一?amp;nbsp;&nbsp;&nbsp;&nbsp;
          最后页&nbsp;&nbsp;&nbsp;&nbsp;
  <%
   }
   else
   {
  %>
          <a href="index.jsp?page=<%=curPage+1%>">下一?lt;/a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a href="index.jsp?page=<%=pageCount%>">最后页</a>
          &nbsp;&nbsp;&nbsp;&nbsp;
  <%
   }
   
   int i=0;
   
   //以@环的方式取出每页要显C的数据Q因为在前面针对要显C的|Q?br />    //调用?jin)rs.absolute((curPage-1)*countPerPage+1);
   //所以是从游标所在的位置取出当前要昄的数据?br />    while(i<countPerPage && !rs.isAfterLast())
   {
    out.println("<hr color=\"blue\" size=\"2\"><br>");
    out.println("用户名:(x)"+rs.getString("gst_user"));
    out.println("&nbsp;&nbsp;");
    
    Timestamp ts=rs.getTimestamp("gst_time");
    long lms=ts.getTime();
    Date date=new Date(lms);
    Time time=new Time(lms);
    
    out.println("留言旉Q?+date+" "+time);
    
    out.println("&nbsp;&nbsp;");
    out.println("用户IPQ?+rs.getString("gst_ip")+"<br>");
    out.println("主题Q?+rs.getString("gst_title")+"<br>");
    out.println("内容Q?+rs.getString("gst_content"));
    i++;
    rs.next(); 
   }
   rs.close();
   stmt.close();
   conn.close();
  %>
 </body>
</html>



Crying 2007-09-22 13:51 发表评论
]]>
վ֩ģ壺 ˳| ף| | | ɽ| | ī| ̽| | ī񹤿| | ʲ| ʡ| ػ| | | ɽ| | ̫| ʡ| Ϫ| | | ľ˹| | | Ϫ| | ݸ| ͨ| ׿| ¡| ɽ| ˳| | | | ӳ| | | |