<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.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 成人精品视频99在线观看免费| 亚洲视频免费在线看| 国产亚洲中文日本不卡二区| 91麻豆国产免费观看| 亚洲AV福利天堂一区二区三 | 亚洲中文字幕日本无线码 | 精品免费人成视频app| 亚洲天堂中文资源| 亚欧色视频在线观看免费| 亚洲视频免费播放| 人成午夜免费视频在线观看| 国产.亚洲.欧洲在线| 嫩草影院免费观看| 黄色三级三级三级免费看| 亚洲精品国产自在久久 | 亚洲ts人妖网站| 天天干在线免费视频| 亚洲av无码兔费综合| 亚洲国产精品视频| 国内精品免费视频精选在线观看| 亚洲狠狠久久综合一区77777| 精品国产免费人成电影在线观看| 久久夜色精品国产噜噜亚洲a| 免费国产成人高清在线观看麻豆| caoporn国产精品免费| 久久久久亚洲精品无码系列| 麻豆视频免费观看| 日日摸夜夜添夜夜免费视频| 亚洲国产精品无码久久一线| 美女视频黄a视频全免费| 国产区图片区小说区亚洲区| 亚洲欧洲无码AV电影在线观看 | 亚洲国产主播精品极品网红 | 中文字幕无码不卡免费视频| 国产精品亚洲精品爽爽| 亚洲色WWW成人永久网址| 7m凹凸精品分类大全免费| 久久亚洲精品无码gv| 国产精品亚洲片在线观看不卡| 亚欧在线精品免费观看一区| 高h视频在线免费观看|