<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    blogjava's web log

    blogjava's web log
    ...

    jsf學習四(添。刪 。改)

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


    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>
    ?????內(nèi)容?
    <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 閱讀(1422) 評論(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.動態(tài)語言

    3.工具箱

    9.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 国产精品亚洲аv无码播放| 亚洲日产2021三区在线| 伊人久久免费视频| 亚洲精品在线视频观看| 国产免费69成人精品视频| 亚洲阿v天堂在线2017免费| 亚洲综合小说久久另类区 | 午夜一级毛片免费视频| 国产乱子伦精品免费视频| 亚洲精品福利网站| 亚洲AV无码一区二三区 | 成年在线网站免费观看无广告| 少妇亚洲免费精品| 亚洲色偷偷av男人的天堂| 亚洲av午夜精品一区二区三区| 久久久久久夜精品精品免费啦| 国产AV无码专区亚洲AV麻豆丫| 亚洲小视频在线观看| 亚洲成av人片天堂网老年人 | 亚洲人成网站观看在线播放| 精品国产无限资源免费观看| 国产免费久久精品丫丫| 亚洲国产精品无码久久九九大片 | 精品熟女少妇aⅴ免费久久| 亚洲国产最大av| 久久青青成人亚洲精品| 四虎永久免费地址在线观看| 亚洲成人免费网站| 中文字幕a∨在线乱码免费看 | 两性刺激生活片免费视频| 中国一级特黄的片子免费| 色欲aⅴ亚洲情无码AV| 亚洲国产成人久久精品app| 久久久久一级精品亚洲国产成人综合AV区 | 亚洲精品国产肉丝袜久久| 亚洲日产无码中文字幕| 免费萌白酱国产一区二区| 成年人在线免费看视频| 亚洲最大免费视频网| 在线美女免费观看网站h| 在线观看免费黄网站|