<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層 專門操作數據庫的一些方法,由于是測試學習。。沒有考慮代碼的規范和其他的問題。運行只看效果。


    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 閱讀(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.動態語言

    3.工具箱

    9.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 四虎精品免费永久免费视频| 亚洲中文字幕无码中文| 无码日韩人妻AV一区免费l| 四虎成人免费观看在线网址| 亚洲永久在线观看| 一个人免费高清在线观看| 真正全免费视频a毛片| 免费看的成人yellow视频| 亚洲精品无码专区在线| 日韩精品免费一区二区三区| 日韩在线视精品在亚洲| 免费在线不卡视频| 精品无码一级毛片免费视频观看| 亚洲欧洲日产国码高潮αv| 久久国产乱子伦精品免费午夜| 亚洲av综合av一区| 精品无码人妻一区二区免费蜜桃| 久久综合亚洲鲁鲁五月天| 国产成人免费高清激情视频| 亚洲成AV人片在WWW| 精品国产亚洲男女在线线电影 | 日韩精品成人亚洲专区| 黄色网页在线免费观看| 婷婷久久久亚洲欧洲日产国码AV| 亚洲精品在线免费看| 亚洲天堂中文字幕在线| 免费女人高潮流视频在线观看| www.亚洲成在线| 免费人成激情视频| 久久精品私人影院免费看| 亚洲国产日韩视频观看| 亚洲国产人成中文幕一级二级| 日韩视频免费在线观看| 亚洲中文字幕无码爆乳app| 亚洲欧洲久久久精品| 国产在线jyzzjyzz免费麻豆| 羞羞漫画小舞被黄漫免费| 巨胸喷奶水视频www网免费| 免费在线人人电影网| 高清在线亚洲精品国产二区| 久久久国产精品福利免费|