KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > formbean > DlogActionForm


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.formbean;
17
18 import java.io.Serializable JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25
26 import web.struts.ActionFormExtend;
27
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Session;
30 import net.sf.hibernate.SessionFactory;
31
32 /**
33  * Dlog系统的扩展ActioForm
34  * Dlog系统中的所有的formBean都必须扩展该类而不是ActionForm
35  * 2004-2-29 21:23 修正了时间显示12小时制为24小时制
36  * @author Liudong
37  */

38 public abstract class DlogActionForm extends ActionFormExtend implements Serializable JavaDoc{
39
40     public final static DateFormat JavaDoc df1 = new SimpleDateFormat JavaDoc("yyyy-M-d H:mm:ss");
41     public final static DateFormat JavaDoc df2 = new SimpleDateFormat JavaDoc("yyyy-M-d");
42     public final static DateFormat JavaDoc df3 = new SimpleDateFormat JavaDoc("H:mm:ss");
43
44     protected boolean isToday(Date JavaDoc date){
45         return df2.format(new Date JavaDoc()).equals(df2.format(date));
46     }
47     /**
48      * 获取Hibernate持久层的操作实例
49      * @return
50      */

51     protected Session getSession() throws SQLException JavaDoc{
52         ServletContext JavaDoc context = servlet.getServletContext();
53         SessionFactory sessions = (SessionFactory)context.getAttribute(dlog4j.Globals.HIBERNATE_SESSIONS_KEY);
54         return sessions.openSession(getConnection());
55     }
56     /**
57      * 提交Hibernate操作
58      * @param session
59      * @throws SQLException
60      * @throws HibernateException
61      */

62     protected void commitSession(Session session, boolean close) throws SQLException JavaDoc, HibernateException{
63         session.flush();
64         session.connection().commit();
65         if(close)
66             session.close();
67     }
68     /**
69      * 关闭session
70      * @param session
71      * @throws SQLException
72      * @throws HibernateException
73      */

74     protected void closeSession(Session session) throws SQLException JavaDoc,HibernateException{
75         session.connection().close();
76         session.close();
77     }
78     
79     public static void main(String JavaDoc[] args) {
80         System.out.println(df1.format(new Date JavaDoc()));
81         System.out.println(df2.format(new Date JavaDoc()));
82         System.out.println(df3.format(new Date JavaDoc()));
83     }
84     
85 }
86
Popular Tags