久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      struts2+Hibernet實現(xiàn)分頁

      好久沒寫過什么東西了,最近自己在學(xué)習(xí)Java WEB方面的東西,自己想用JSP和 Struts2 HIBERNATE來寫一個自己工作室的網(wǎng)站練練手,稍微有些收獲今天寫出來,與大家一起分享一下:STRUTS2+HIBERNATE實現(xiàn)分頁是 JAVA WEB最常用的東西了,我在網(wǎng)站上找了很多東西結(jié)果都沒有很理想的。由于本人是新手所以寫出來的代碼質(zhì)量有限。希望與大家一起進步,但是我從沒有放棄過!我接觸JAVA WEB不到一個月的時間,希望大家見諒寫出來的東西部是很理想。望高手多多指教。

      第一步:建立一個SQL數(shù)據(jù)庫Tb_soft ,數(shù)據(jù)庫表,software(字段:Tsoftware,fSoftname,fListImage,fVar,fFeilname,fUsedSystem,fUpdateTime,fInf,fClassID,fDownTimes  字段

      類型并不重要自己隨便設(shè)置然后直接通過SQL企業(yè)管理器直接在表里輸入內(nèi)容方便測試就是了)

      第二步:建立一個名為productlist JAVA的WEB項目(我用的是NetBeans IDE 6.9,所以在新建項目的時候選擇了Struts2 HIBERNATE框架,新建后會自動生成 hibernate.cfg.xml的配置文件

      )。

      第三步:配置hibernate.cfg.xml文件,主要是對數(shù)據(jù)庫的鏈接設(shè)置以及數(shù)據(jù)表映射文件的設(shè)置

      1、文件名:hibernate.cfg.xml文件代碼:

      <?xml version=”1.0″ encoding=”UTF-8″?>
      <!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
      <hibernate-configuration>
        <session-factory>
          <property name=”hibernate.dialect”>org.hibernate.dialect.SQLServerDialect</property>
          <property name=”hibernate.connection.driver_class”>com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
          <property name=”hibernate.connection.url”>jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Tb_soft </property>
          <property name=”hibernate.connection.username”>sa</property>
          <property name=”hibernate.connection.password”>123456</property>
          <mapping resource=”soft.hbm.xml”/>
        </session-factory>
      </hibernate-configuration>

      注: <mapping resource=”soft.hbm.xml”/> 元素對數(shù)據(jù)庫表software 配置文件soft.hbm.xml的映射配置,注意soft.hbm.xml文件的路徑如果和hibernate.cfg.xml文件沒在同一個目錄必須

      加上路徑例如 <mapping resource=”DatatableXML/soft.hbm.xml”/>

      2、新建 名為:soft.hbm.xml 的映射文件,該文件主要是對數(shù)據(jù)庫表software的映射配置全部代碼如下:

      <?xml version=”1.0″ encoding=”UTF-8″?>

      <!DOCTYPE hibernate-mapping PUBLIC
        “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
        “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

      <hibernate-mapping>
          <class name= “com.bean.soft” table=”Tsoftware”>
              <id name=”id” column=”id” type=”int”>
                  <generator class=”native”/>
              </id>
              <property name=”fSoftname” type=”string” length=”20″>
                  <column name=”fSoftname”/>
              </property>
              <property name=”fListImage” type=”string”  length=”20″>
                  <column name=”fListImage”/>
              </property>
              <property name=”fVar” type=”string” length=”10″>
                  <column name=”fVar”/>
              </property>
              <property name=”fFeilname” type=”string”  length=”30″>
                  <column name=”fFeilname”/>
              </property>
              <property name=”fUsedSystem” type=”string”  length=”30″>
                  <column name=”fUsedSystem”/>
              </property>
              <property name=”fUpdateTime” type=”string”  length=”30″>
                  <column name=”fUpdateTime”/>
              </property>
              <property name=”fInf” type=”string”  length=”2000″>
                  <column name=”fInf”/>
              </property>
              <property name=”fDownTimes” type=”string” length=”10″>
                  <column name=”fDownTimes”/>
              </property>
          </class>
      </hibernate-mapping>

      注: <class name= “com.bean.soft” table=”Tsoftware”>設(shè)置了soft屬于一個javabean,關(guān)于javabean我就不解釋了。關(guān)于這個文件的代碼后面再貼出來。class name=javabean包

      +javabean構(gòu)成, table=”Tsoftware”,其中Tsoftware就是SQL數(shù)據(jù)庫表。有關(guān)property 元素我這里簡單的說一下 例如下:

             <property name=”fDownTimes” type=”string” length=”10″>
                  <column name=”fDownTimes”/>
              </property>
       name,映射文件構(gòu)成表的的字段名,這里的name必須與com.bean.soft 中的熟悉對應(yīng),千萬記住要設(shè)置好type ,這里的type好比SQL的字段類型,具體類型對應(yīng)關(guān)系請查相關(guān)資料我就不詳細解

      釋了。

      第四步:在包com.bean 建立JAVAbean soft 代碼如下:

      package com.bean;

      /**
       *
       * @author Even
       */
      public class soft {
          private String fSoftname;
          private int id;
          private String fListImage;
          private String fVar;
          private String fFeilname;
          private String fUsedSystem;
          private String fUpdateTime;
          private String fInf;
          private String fDownTimes;

          /**
           * @return the fSoftname
           */
          public String getfSoftname() {
              return fSoftname;
          }

          /**
           * @param fSoftname the fSoftname to set
           */
          public void setfSoftname(String fSoftname) {
              this.fSoftname = fSoftname;
          }

          /**
           * @return the id
           */
          public int getId() {
              return id;
          }

          /**
           * @param id the id to set
           */
          public void setId(int id) {
              this.id = id;
          }

          /**
           * @return the fListImage
           */
          public String getfListImage() {
              return fListImage;
          }

          /**
           * @param fListImage the fListImage to set
           */
          public void setfListImage(String fListImage) {
              this.fListImage = fListImage;
          }

          /**
           * @return the fVar
           */
          public String getfVar() {
              return fVar;
          }

          /**
           * @param fVar the fVar to set
           */
          public void setfVar(String fVar) {
              this.fVar = fVar;
          }

          /**
           * @return the fFeilname
           */
          public String getfFeilname() {
              return fFeilname;
          }

          /**
           * @param fFeilname the fFeilname to set
           */
          public void setfFeilname(String fFeilname) {
              this.fFeilname = fFeilname;
          }

          /**
           * @return the fUsedSystem
           */
          public String getfUsedSystem() {
              return fUsedSystem;
          }

          /**
           * @param fUsedSystem the fUsedSystem to set
           */
          public void setfUsedSystem(String fUsedSystem) {
              this.fUsedSystem = fUsedSystem;
          }

          /**
           * @return the fUpdateTime
           */
          public String getfUpdateTime() {
              return fUpdateTime;
          }

          /**
           * @param fUpdateTime the fUpdateTime to set
           */
          public void setfUpdateTime(String fUpdateTime) {
              this.fUpdateTime = fUpdateTime;
          }

          /**
           * @return the fInf
           */
          public String getfInf() {
              return fInf;
          }

          /**
           * @param fInf the fInf to set
           */
          public void setfInf(String fInf) {
              this.fInf = fInf;
          }

          /**
           * @return the fDownTimes
           */
          public String getfDownTimes() {
              return fDownTimes;
          }

          /**
           * @param fDownTimes the fDownTimes to set
           */
          public void setfDownTimes(String fDownTimes) {
              this.fDownTimes = fDownTimes;
          }
      }
      關(guān)于JAVAbean就不解釋了。

      第五步:在com.Hibernate包建立Hibernate的sessionFactory(文件名:NewHibernateUtil.java)用于獲取Session

      NewHibernateUtil.java 代碼:

      package com.Hibernate;

      import org.hibernate.HibernateException;
      import org.hibernate.Session;
      import org.hibernate.cfg.AnnotationConfiguration;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.Configuration;

      /**
       * Hibernate Utility class with a convenient method to get Session Factory object.
       *
       * @author Even
       */
      public class NewHibernateUtil {

          private static SessionFactory sessionFactory = null;
          private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

          static {
              try {
                  sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
              } catch (Throwable ex) {
                  // Log the exception.
                  System.err.println(“對不起數(shù)據(jù)工廠構(gòu)建失敗.” + ex);
                  throw new ExceptionInInitializerError(ex);
              }
          }

          public static SessionFactory getSessionFactory() {
              return sessionFactory;
          }

          public static Session getsession() throws HibernateException {
              Session session = (Session) threadLocal.get();
              if (session == null || !session.isOpen()) {
                  if (sessionFactory == null) {
                      rebuildsessionFactory();
                  }
                  session = (sessionFactory != null) ? sessionFactory.openSession() : null;
                  threadLocal.set(session);
              }
              return session;
          }

          private static void rebuildsessionFactory() {
              try {
                  Configuration cfg = new Configuration().configure();
                  sessionFactory = cfg.buildSessionFactory();
              } catch (Exception e) {
                  System.out.print(“創(chuàng)建工廠會話失??!”);
                  e.printStackTrace();
              }
          }

          public static SessionFactory getsessionFactory() {
              return sessionFactory;
          }

          public static void closesessicon() {
              Session session = (Session) threadLocal.get();
              if (session != null) {
                  session.close();
              }
          }
      }

      這里注意session的關(guān)閉。

      第六步:在包com.dao包建立整個程序的do層;文件名為:softDao.java

      代碼如下:

      package com.dao;

      /**
       *
       * @author Even
       */
      import com.Hibernate.NewHibernateUtil;
      import com.bean.soft;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.ArrayList;
      import java.util.List;
      import org.hibernate.HibernateException;
      import org.hibernate.Query;
      import org.hibernate.Session;

      public class softDao {
          public List<soft> queryByPage(int pageSize, int pageNow, String HQL) {
              Session session = null;
              List sftlist = new ArrayList();
              try {
                  session = NewHibernateUtil.getsession();// 獲得session對象
                  //String hql = “from Employee emp”;// 查詢HQL語句
                  HQL = “from soft sft”;// 條件查詢HQL語句
                  Query q = session.createQuery(HQL);// 執(zhí)行查詢操作
                  q.setFirstResult(pageSize * (pageNow – 1));
                  q.setMaxResults(pageSize);
                  sftlist = q.list();
              } catch (HibernateException e) {
                  e.printStackTrace();
                  System.out.println(“查詢失敗”);
              } finally {
                  NewHibernateUtil.closesessicon();// 關(guān)閉session
              }
              return sftlist;
          }
          //獲得總頁數(shù)的方法有時間可能會單獨使用該工程所以將獲得session的過程也單獨寫出來
          public int getpageCount(int pagesize, String HQL) {
              int pageCount;
              int Datacount = 0;
              Session session = null;
              try {
                  session = NewHibernateUtil.getsession();// 獲得session對象
                  HQL = “from soft sft”;//條件查詢HQL語句,這里注意使用的實例查詢方式,soft是我們建立的javabean
                  Query q = session.createQuery(HQL);// 執(zhí)行查詢操作
                  Datacount = q.list().size();//獲得記錄總數(shù)
              } catch (HibernateException e) {
                  e.printStackTrace();
                  System.out.println(“查詢失敗”);
              } finally {
                  NewHibernateUtil.closesessicon();// 關(guān)閉session
              }
              if (Datacount % pagesize == 0) {
                  pageCount = Datacount / pagesize;
              } else {
                  pageCount = Datacount / pagesize + 1;
              }
          
              return pageCount;

          }
      }

      時間有點晚了所以這里就不多解釋了。

      第七步:在包com.action 建立分頁的Action文件名為:softlistAction.java

      代碼如下:

      package com.action;

      import com.bean.leavetalk;
      import com.bean.soft;
      import com.dao.Dao;
      import java.util.List;
      import com.opensymphony.xwork2.ActionSupport;

      public class softlistAction extends ActionSupport {

          private List<soft> softs;//用于數(shù)據(jù)集合對象并非只是軟件
          private List<soft> downcountlist;//用于顯示下載列表的集合。
          private List<leavetalk> leavatalks;//用于顯示用戶留言列表的集合。
          private int pageNow = 1; //初始化為1,默認從第一頁開始顯示
          private int pageSize = 4; //每頁顯示5條記錄
          private int pageCount;//總頁數(shù)
          private String doing;//標記Action返回的值
          private Dao pageDAO = new Dao();
          private Dao leivetalkdao = new Dao();
          private int id;
          private String fbadcount;
          private String fgoodcount;

          public List<soft> getSofts() {
              return softs;
          }

          public void setSofts(List<soft> softwares) {
              this.softs = softs;
          }

          public int getPageNow() {
              return pageNow;
          }

          public void setPageNow(int pageNow) {
              this.pageNow = pageNow;
          }

          public int getPageSize() {
              return pageSize;
          }

          public void setPageSize(int pageSize) {
              this.pageSize = pageSize;
          }

          public int getPageCount() {
              Dao sa = new Dao();
              sa.getpageCount(pageSize, “”);
              this.pageCount = sa.getpageCount(pageSize, “”);
              return sa.getpageCount(pageSize, “”);
          }

          /**
           * @param pageCount the pageCount to set
           */
          public void setPageCount() {
              Dao sa = new Dao();
              sa.getpageCount(pageSize, “”);
              this.pageCount = sa.getpageCount(pageSize, “”);
          }

          /**
           * @return the doing
           */
          public String getDoing() {
              return doing;
          }

          /**
           * @param doing the doing to set
           */
          public void setDoing(String doing) {
              this.doing = doing;
          }

          //主方法
          public String execute() throws Exception {
              String returnstr = “erro”;
              String HQLstr = “”;
              // softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);
              if (doing.equals(“productlist”)) {
                  returnstr = “productlist”;
                  HQLstr = “from soft sft”;
                  this.setPageSize(4);
                  this.leavatalks = leivetalkdao.getleavetalks(“from leavetalk lt order by lt.id desc”);
                  this.softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);

              } else if (doing.equals(“productshow”)) {
                  returnstr = “productshow”;
                  HQLstr = “from soft sft where sft.id='” + getId() + “‘ order by sft.id asc”;
                  downcountlist = pageDAO.queryByPage(10, pageNow, “from soft sft order by sft.fDownTimes desc”);

              } else if (doing.equals(“index”)) {
                  returnstr = “index”;
                  HQLstr = ” from soft sft where sft.ftype=’T’order by sft.id asc”;
                  this.setPageSize(5);
              } else if (doing.equals(“web”)) {
                  returnstr = “web”;
                  HQLstr = ” from soft sft where sft.ftype=’T’order by sft.id asc”;
                  this.setPageSize(5);
              } else if (doing.equals(“service”)) {
                  returnstr = “service”;
                  //HQLstr = ” from service sr where order by sr.id asc”;
                  // this.setPageSize(1);
              } else if (doing.equals(“Updatefgood”)) {
                  returnstr = “Updatefgood”;
                  HQLstr = “from soft sft where sft.id='” + getId() + “‘ order by sft.id asc”;
                  downcountlist = pageDAO.queryByPage(10, pageNow, “from soft sft order by sft.fDownTimes desc”);
                  pageDAO.UpdatefgoodAndfbad(getId(), “updategood”, fgoodcount);
              } else if (doing.equals(“Updatefbad”)) {
                  returnstr = “Updatefgood”;
                  HQLstr = “from soft sft where sft.id='” + getId() + “‘ order by sft.id asc”;
                  downcountlist = pageDAO.queryByPage(10, pageNow, “from soft sft order by sft.fDownTimes desc”);
                  pageDAO.UpdatefgoodAndfbad(getId(), “updatebad”, fbadcount);
              } else {
                  returnstr = “erro”;
              }
              softs = pageDAO.queryByPage(pageSize, pageNow, HQLstr);
            
              System.out.println(softs.size());
               return returnstr;
          }

          /**
           * @return the id
           */
          public int getId() {
              return id;
          }

          /**
           * @param id the id to set
           */
          public void setId(int id) {
              this.id = id;
          }

          /**
           * @return the downcountlist
           */
          public List<soft> getDowncountlist() {
              return downcountlist;
          }

          /**
           * @param downcountlist the downcountlist to set
           */
          public void setDowncountlist(List<soft> downcountlist) {
              this.downcountlist = downcountlist;
          }

          /**
           * @return the fbadcount
           */
          public String getFbadcount() {
              return fbadcount;
          }

          /**
           * @param fbadcount the fbadcount to set
           */
          public void setFbadcount(String fbadcount) {
              Integer b = Integer.valueOf(fbadcount) + 1;
              this.fbadcount = b.toString();

          }

          /**
           * @return the fgoodcount
           */
          public String getFgoodcount() {
              return fgoodcount;
          }

          /**
           * @param fgoodcount the fgoodcount to set
           */
          public void setFgoodcount(String fgoodcount) {
              Integer b = Integer.valueOf(fgoodcount) + 1;
              this.fgoodcount = b.toString();
          }

          /**
           * @return the leavatalks
           */
          public List<leavetalk> getLeavatalks() {
              return leavatalks;
          }

          /**
           * @param leavatalks the leavatalks to set
           */
          public void setLeavatalks(List<leavetalk> leavatalks) {
              this.leavatalks = leavatalks;
          }
      }

      第八步:配置ACTION 文件STRUTS.xml的代碼如下:

      <!DOCTYPE struts PUBLIC
      “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
      “http://struts.apache.org/dtds/struts-2.0.dtd”>

      <struts>
          <include file=”example.xml”/>
          <!– Configuration for the default package. –>
          <package name=”default” extends=”struts-default”>
              <action name=”softlist” class=”com.action.softlistAction”>
                  <result name=”SUCCESS”>test.jsp</result>
                  <result name=”error”>error.jsp</result>
              </action>
          </package>
      </struts>

      第九步:建立JSP文件 來享受結(jié)果了

      test.jsp代碼如下:
      < import=”java.sql.ResultSet”%>
      < import=”java.util.List”%>
      < contentType=”text/html”%>
      < import=”org.apache.struts2.ServletActionContext”%>
      <%@ page language=”java” pageEncoding=”UTF-8″%>
      <%@ taglib uri=”/struts-tags” prefix=”s”%>
      <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
         “http://www.w3.org/TR/html4/loose.dtd”>
      <script language=”javascript” type=”text/javascript”>
              <%
                          String PageNow = request.getAttribute(“pageNow”).toString();
                          String pageCount = request.getAttribute(“pageCount”).toString();
              %>
                
          </script>
      <html>
          <head>
              <meta http-equiv=”Content-Type” content=”text/html; charset=GBK”>
              <title>分頁測試</title>
          </head>

          <table style=” border: 2px”>
              <s:iterator value=”softs”>
                  <td><s:property value=”fSoftname”/></td>
                  <td><s:property value=”fUsedSystem”/></td>
                  <td><s:property value=”fUpdateTime”/></td>
              </s:iterator>
          </table>
          <div>
                              <ul><li><a href=’softlist.action?pageNow=1′>首頁</a></li>
                                  <li><a href=’softlist.action?pageNow=<s:property value=”%{PageNow-1}”/>’> 上一頁</a></li>
                                  <li><a href=’softlist.action?pageNow=<s:property value=”%{PageNow+1}”/>’>下一頁</a></li>
                                  <li><a href=’softlist.action?pageNow=<%=pageCount%>’>末頁</a></li>
                                  <li><span class=”pageinfo”>第<strong><s:property value=”#request.pageNow”/></strong>頁</span></li>
                                  <li><span class=”pageinfo”>共<strong><s:property value=”#request.pageCount”/></strong>頁</span></li>
                              </ul></div>
          <body>
           
          </body>
      </html>
      第十步:建立link.jsp
      代碼如下:
      <%–
          Document   : link
          Created on : 2011-1-24, 18:42:14
          Author     : Even
      –%>

      < contentType=”text/html” pageEncoding=”GBK”%>
      <!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=GBK”>
              <title>JSP Page</title>
          </head>
          <body>
              <h1><a href=”softlist.action”>單擊瀏覽效果</a></h1>
          </body>
      </html>

      注意:后面的演示我是重新制作的頁面不是我的網(wǎng)站頁面效果所以只是完成了一個原理而已,請高人高抬貴手 本人剛?cè)腴T不到一個月的時間,利用這篇文章來回顧我自己學(xué)的東西同事也分享給大家,網(wǎng)上很多 STRUTS2的分頁文章沒有一個完整的。這里面的分頁原理用的是 session里面的記錄集查詢中的 setFirstResult和setMaxResults,關(guān)于算法大家自己去揣摩就是了很簡單。我正在寫一個工作室的網(wǎng)站雖然用asp.net來寫很快但是我覺得用熟悉JAVA的話會更快,呵呵。以后我會多寫一些東西的。

       

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號