blogjava's web log

          blogjava's web log
          ...

          jsf學習四(添。刪 。改)

          新建個dal層 專門操作數據庫的一些方法,由于是測試學習。。沒有考慮代碼的規范和其他的問題。運行只看效果。


          package?jsftest.dal;

          import?org.hibernate.*;
          import?org.hibernate.cfg.*;
          import?org.apache.log4j.*;
          import?jsftest.vo.ArticleVO;

          import?java.util.*;

          public?class?ArticleDAL?{
          ???org.apache.log4j.Logger?log
          =Logger.getLogger(this.getClass());
          ???
          private?Session?session=null;
          ????
          public?ArticleDAL()?{
          ????}
          ????
          public?void?saveArticle(ArticleVO?vo)
          ????{
          ????????
          try
          ????????{
          ????????????session?
          =?this.getSession();
          ????????????session.saveOrUpdate(vo);
          ????????????session.flush();
          ????????????session.connection().commit();
          ????????????session.close();
          ????????}
          ????????
          catch(Exception?ee)
          ????????{
          ????????????log.error(ee);
          ????????}
          ????}
          ????
          public?void?deleteArticle(int?articleID)
          ????{
          ????????session
          =this.getSession();
          ???????ArticleVO?vo
          =(ArticleVO)session.get(ArticleVO.class,articleID);
          ????????session.delete(vo);
          ????}

          ????
          public?void?deleteArticle(ArticleVO?vo)
          ????{
          ??????session
          =?this.getSession();
          ??????session.delete(vo);
          ??????session.flush();

          ????}
          ????
          public?List?LoadArticleAll()
          ????{
          ????????session
          =this.getSession();
          ???????Query?query
          =session.createQuery("FROM?ArticleVO");
          ???????List?list
          =?query.list();
          ???????session.close();
          ???????
          return?list;
          ????}
          ????
          public?Session?getSession()
          ????{
          ????????
          try
          ????????{
          ????????????Configuration?cfg?
          =?new?Configuration().configure();
          ????????????SessionFactory?sf?
          =?cfg.buildSessionFactory();
          ????????????
          return?sf.openSession();
          ????????}?
          catch(Exception?ee)
          ????????{
          ????????????log.error(
          "error:"?+?ee.getMessage());
          ????????}
          ????????
          return?null;
          ????}
          }


          新建from 類 前途jsf都是掉這類的方法

          package?jsftest.from;

          import?java.util.ArrayList;
          import?java.util.List;

          import?javax.faces.component.UIData;
          import?javax.faces.event.ActionEvent;

          import?jsftest.dal.ArticleDAL;
          import?jsftest.vo.ArticleVO;
          import?java.util.Collection;

          public?class?ArticleForm?{
          ????
          private?int?id=0;
          ????
          private?String?title;
          ????
          private?String?body;
          ????
          private?ArrayList?articles;

          ????
          public?ArticleForm()?{
          ????????loadall();
          ????}
          ????
          private?ArticleDAL?dal=new?ArticleDAL();
          ????
          public?void?save()
          ????{
          ????????ArticleVO?vo
          =new?ArticleVO();
          ????????vo.setBody(
          this.getBody());
          ????????vo.setTitle(
          this.getTitle());
          ????????
          if(this.getId()!=0)
          ????????{
          ????????????vo.setId(
          this.getId());
          ????????}
          ????????dal.saveArticle(vo);
          ????}
          ????
          public?void?edit(ActionEvent?event)
          ????{
          ???????UIData?table?
          =?(UIData)?event.getComponent().getParent().getParent();
          ???????ArticleVO?vo
          =new?ArticleVO();
          ???????vo
          =(ArticleVO)table.getRowData();
          ??????
          this.setBody(vo.getBody());
          ??????
          this.setId(vo.getId());
          ??????
          this.setTitle(vo.getTitle());
          ????}

          ????
          public?void?delete(ActionEvent?event)
          ????{
          ???????UIData?table?
          =?(UIData)?event.getComponent().getParent().getParent();
          ????????ArticleVO?vo
          =(ArticleVO)table.getRowData();
          ????????dal.deleteArticle(vo);
          ????????dal.LoadArticleAll();
          ????}
          ????
          public?void?loadall()
          ????{
          ????????
          this.setArticles((ArrayList)dal.LoadArticleAll());
          ????}

          ????
          public?String?getBody()?{
          ????????
          return?body;
          ????}

          ????
          public?int?getId()?{
          ????????
          return?id;
          ????}

          ????
          public?String?getTitle()?{
          ????????
          return?title;
          ????}

          ????
          public?Collection?getArticles()?{
          ????????
          //this.loadall();
          ????????if(articles==null)
          ????????{
          ????????????articles
          =new?ArrayList();
          ????????}
          ????????
          return?articles;
          ????}

          ????
          public?void?setBody(String?body)?{
          ????????
          this.body?=?body;
          ????}

          ????
          public?void?setId(int?id)?{
          ????????
          this.id?=?id;
          ????}

          ????
          public?void?setTitle(String?title)?{
          ????????
          this.title?=?title;
          ????}

          ????
          public?void?setArticles(ArrayList?articles)?{
          ????????
          this.articles?=?articles;
          ????}

          }

          實體

          package?jsftest.vo;

          public?class?ArticleVO?{
          ????
          private?int?id;
          ????
          private?String?title;
          ????
          private?String?body;
          ????
          public?ArticleVO()?{
          ????}
          //getter?setter


          前臺

          <%@?page?contentType="text/html;?charset=GBK"?%>
          <%@taglib?uri="http://java.sun.com/jsf/html"?prefix="h"%>
          <%@taglib?uri="http://java.sun.com/jsf/core"?prefix="f"%>
          <f:view>
          <head>
          <title>
          jsp1
          </title>
          </head>
          <body?bgcolor="#ffffff">
          <h1>
          JBuilder?Generated?JSP
          </h1>
          <h:form>

          ??
          <div?align="left">
          ????標題??
          <h:inputText?id="title"?value="#{article.title}"?/><br>
          ?????內容?
          <h:inputTextarea?id="currentMessage"?value="#{article.body}"?rows="10"?cols="60"/>
          ?????????
          <h:inputHidden?value="#{article.id}"/>
          ??
          </div>
          ???????
          <div?align="center">
          ??????????
          <h:commandButton?value="save"?action="#{article.save}"/>
          ???????
          </div>
          ??????
          <div?align="center">
          ????????
          <h:commandButton?value="clear"?type="reset"/>
          ??????
          </div>
          ??*************************************************************

          ?
          <h:dataTable?id="table"?rowClasses="list-row"?value="#{article.articles}"?var="articles">
          ??????????????
          <h:column>
          ????????????????
          <h:outputText?styleClass="small"?value="#{articles.id}"/>
          ??????????????
          </h:column>
          ??????????????
          <h:column>
          ????????????????
          <h:commandLink?id="editLink"?actionListener="#{article.edit}">
          ??????????????????
          <h:outputText?value="edit"/>
          ????????????????
          </h:commandLink>
          ??????????????
          </h:column>
          ??????????????
          <h:column>
          ????????????????
          <h:commandLink?id="deleteLink"?actionListener="#{article.delete}">
          ??????????????????
          <h:outputText?value="delete"/>
          ????????????????
          </h:commandLink>
          ??????????????
          </h:column>
          ??????????????
          <h:column>
          ????????????????
          <h:outputText?value="#{articles.title}"/>
          ??????????????
          </h:column>
          ???????????????
          <h:column>
          ????????????????
          <h:outputText?value="#{articles.body}"/>
          ??????????????
          </h:column>
          ????????????
          </h:dataTable>
          ????????
          </h:form>
          </body>

          </f:view>



          faces-config.xml


          <?xml?version="1.0"?encoding="UTF-8"?>
          <!DOCTYPE?faces-config?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?JavaServer?Faces?Config?1.1//EN"?"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
          <faces-config>
          ??
          <managed-bean>
          ????
          <description>this?first?jsf</description>
          ????
          <managed-bean-name>article</managed-bean-name>
          ????
          <managed-bean-class>jsftest.from.ArticleForm</managed-bean-class>
          ????
          <managed-bean-scope>request</managed-bean-scope>
          ??
          </managed-bean>
          </faces-config>



          hibernate 實體配置文件

          <?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="jsftest.vo.ArticleVO"?table="articles"?>
          ????
          <id?name="id"?column="id"?unsaved-value="0">
          ??????
          <generator?class="native"/>
          ????
          </id>
          ?????
          <property?name="title"??column="title"?/>
          ?????
          <property?name="body"?column="body"?/>
          ????
          ??
          </class>
          </hibernate-mapping>


          hibernate 配置文件

          <?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="connection.driver_class">org.gjt.mm.mysql.Driver</property>
          ????
          <property?name="connection.url">jdbc:mysql://localhost:3306/test</property>
          ????
          <property?name="connection.username">root</property>
          ????
          <property?name="connection.password"></property>

          ????
          <property?name="connection.pool_size">1</property>
          ????
          <property?name="dialect">org.hibernate.dialect.MySQLDialect</property>
          ????
          <property?name="show_sql">true</property>
          ????
          <property?name="hibernate.use_outer_join">true</property>
          ????
          <property?name="hibernate.transaction.factory_class">
          ??????org.hibernate.transaction.JDBCTransactionFactory
          ????
          </property>
          ????
          <mapping?resource="ArticleVO.hbm.xml"?/>

          ??
          </session-factory>
          </hibernate-configuration>




          運行。。

          posted on 2006-08-26 09:03 record java and net 閱讀(1427) 評論(3)  編輯  收藏 所屬分類: jsf學習

          評論

          # re: jsf學習四(添。刪 。改) 2006-09-12 17:54 ding

          wo cao  回復  更多評論   

          # re: jsf學習四(添。刪 。改) 2006-11-23 14:04 j[匿名]

          @ding
          ddd  回復  更多評論   

          # re: jsf學習四(添。刪 。改) 2011-03-25 09:21 lxc

          謝謝  回復  更多評論   

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 敦化市| 宁南县| 六安市| 灵宝市| 宜章县| 上饶市| 尼木县| 天津市| 吉林市| 桓台县| 丰都县| 尖扎县| 磐石市| 商河县| 阜阳市| 周至县| 石阡县| 虞城县| 永泰县| 高雄县| 顺平县| 门源| 嘉黎县| 平邑县| 宁阳县| 偏关县| 吴忠市| 三门县| 中卫市| 盘山县| 凤凰县| 内丘县| 星子县| 汉阴县| 太原市| 渝北区| 垫江县| 民乐县| 石林| 昭苏县| 隆尧县|