jsf学习四(添。删 。改)

来源:互联网 发布:mac 查看绝对路径 编辑:程序博客网 时间:2024/06/05 03:38
新建个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>




运行。。 
原创粉丝点击