struts+hibernate增删改查(一)

struts+hibernate增删改查 (一)

说明:如果你还不清楚struts和hibernate的一些基本原理,希望能先了解一下这方面的相关内容。本篇只是struts+hibernate增删改查的一个演示例子,由于个人能力暂时有限可能有些地方不是很完善,请大家包涵指点。 本例子特点:struts 部分还应用了struts里的validate验证框架,对于主键生成应用了hibernate中序列方式调了oracle中的序列生成主键。  

ps:这个例子最先是我在家做的,用myeclips,在公司用的不是myEclips,所以导进来时开始有几个包冲突,后来包冲突的去掉就好了.

开发环境部署环境:

开发工具:eclips3.1+Nitrox(myEclips一样)

数据库:oracle9

服务器:tomcat5

环境怎么配置就不多说了,可以google下很多比我说的还详细,由于开始是用的myEclips,所以先建个web project,struts和hibernate框架myEclips可以帮你添加进工程中,这样很方便,比如,hibernate中的*.hbm.xml和bean文件都可以根据表自动帮你生成,省了很多体力活,正确性比自己手写的还高。

工程目录结构图:

 

1.从数据库表开始:Admin.sql源码也放在工程目录里了

--建表 /* create table ADMIN ( id           number(2)      primary key not null, username     varchar2(15), password     varchar2(15), age          number(3),   mail         varchar2(20)

); --创建序列 create sequence admin_seq;

*/

--插入初始化数据 /* insert into admin values(admin_seq.nextVal,'gu11','gu11',26,'gu11@126.com'); insert into admin values(admin_seq.nextVal,'gu22','gu22',26,'gu@126.com'); insert into admin values(admin_seq.nextVal,'gu33','gu33',27,'gu33@126.com'); insert into admin values(admin_seq.nextVal,'gu44','gu44',28,'gu44@126.com');

insert into admin values(admin_seq.nextVal,'gu55','gu55',27,'gu55@126.com'); insert into admin values(admin_seq.nextVal,'gu66','gu66',15,'gu66@126.com'); insert into admin values(admin_seq.nextVal,'gu77','gu77',20,'gu77@126.com'); insert into admin values(admin_seq.nextVal,'gu88','gu88',10,'gu88@126.com'); insert into admin values(admin_seq.nextVal,'gu10','gu10',116,'gu10@126.com'); */ --要放在事务里,一定要记得commit,不然程序与数据库就不一致,程序就查不出数据 --commit;

2.表有了就开始了,一切由表开始创建表对应的对象实体bean,然后对表进行相关操作,

当然首先要建持久层,hibernate登场了^_^...

说明下:一下连接数据库这部分图是我拷的网上的,我再从头来便有点时间不允许了,请大家谅解啊

用myeclipse的就方便了 ,配置连接数据库,选中window->Open Perspective->Other…   可以看到现在跳出一个名为Select Perspective的菜单,在里面选中MyEclipse Databases Exporler,可以看到现在到了下面的页面。

  按以上图示输入相关字段后点击Finish便建立了一个数据库连接,在新出现的JDBC for Mysql上点右键(我连的是oracle,对应改下配置就可以了),选择Open connection…,确认用户名和密码正确后点OK,如果一切顺利的话你会看到下面的画面:

  这说明你已经和数据库建立了正确的连接。现在我们再回到window->Open Perspective- >Other…里的MyEclipse,也就是我们刚进来的时候看到的画面。   右键点击你刚建立的工程 test并选择MyEclipse->Add struts Capabilities…在跳出的菜单里按照如下输入并确定:

  好了,现在你已经为你的工程增加了struts,接下来和上面一样在右键工程后选择MyEclipse- >Add Hibernate Capabilities…一路确定下来为你的工程添加Hibernate。(为方便起见我们在选择路径时把HibernateSessionFactory.java放在了src/com下面,其实最好建立个单独的目录如 src/com/hibernate)   为了更好的演示我们不建立通常的登陆页面而是建立个注册页面。选择 src目录下的hibernate.cfg.xml文件。照如下填写并保存。这样hibernate就为你建立了数据库的连接池。

我们转到hibernate。换到刚才我们建立数据库的页面,选择你的admin的表点右键选择Create Hibernate Mapping。选择好打包路径后选择Finish。如图:

  在你刚才选择的路径下(我为方便是src/com/hibernate/domain)下新建立的文件 。

hibernate.cfg.xml配置文件:,两点最重要,1.配置数据库连接信息,2.指定需要引用的对象bean所对应的配置文件*.hbm.xml文件。

[xml]  view plain copy
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2.   
  3. <!DOCTYPE hibernate-configuration PUBLIC  
  4.   
  5.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  6.   
  7.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  8.   
  9. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  10.   
  11. <hibernate-configuration>  
  12.   
  13.     <session-factory>  
  14.   
  15.         <property name="connection.username">cwgladm</property>  
  16.   
  17.         <property name="connection.url">jdbc:oracle:thin:@192.168.0.201:1521:cwgl</property>  
  18.   
  19.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  20.   
  21.         <property name="myeclipse.connection.profile">DataConn</property>  
  22.   
  23.         <property name="connection.password">cwglpass</property>  
  24.   
  25.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  26.   
  27.         <mapping resource="com/hibernate/domain/Admin.hbm.xml" />  
  28.   
  29.     </session-factory>  
[xml]  view plain copy
  1. 说明:数据库连接信息就不多少了,<mapping resource="com/hibernate/domain/Admin.hbm.xml" />  
  2.   
  3. </hibernate-configuration>是指定要用到的表对应的配置文件信息.  
[xml]  view plain copy
  1. HibernateSessionFactory.java,hibernate会话管理,用myeclips自动生成的,只要修改下指向配置文件的路径就可以了  
[xml]  view plain copy
  1. <pre class="java" name="code">package com.hibernate.sessionFactory;  
  2.   
  3. import org.hibernate.HibernateException;  
  4.   
  5. import org.hibernate.Session;  
  6.   
  7. import org.hibernate.cfg.Configuration;  
  8.   
  9. /**  
  10.   
  11.  * Configures and provides access to Hibernate sessions, tied to the  
  12.   
  13.  * current thread of execution.  Follows the Thread Local Session  
  14.   
  15.  * pattern, see {@link http://hibernate.org/42.html}.  
  16.   
  17.  */  
  18.   
  19. public class HibernateSessionFactory {  
  20.   
  21.     /**   
  22.   
  23.      * Location of hibernate.cfg.xml file.  
  24.   
  25.      * NOTICE: Location should be on the classpath as Hibernate uses  
  26.   
  27.      * #resourceAsStream style lookup for its configuration file. That  
  28.   
  29.      * is place the config file in a Java package - the default location  
  30.   
  31.      * is the default Java package.<br><br>  
  32.   
  33.      * Examples: <br>  
  34.   
  35.      * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".   
  36.   
  37.      * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>   
  38.   
  39.      */  
  40.   
  41.     //指定配置文件的路径,其他没改什么  
  42.   
  43.     private static String CONFIG_FILE_LOCATION = "/com/hibernate/properties/hibernate.cfg.xml";  
  44.   
  45.   
  46.     /** Holds a single instance of Session */  
  47.   
  48.     private static final ThreadLocal threadLocal = new ThreadLocal();  
  49.   
  50.     /** The single instance of hibernate configuration */  
  51.   
  52.     private static final Configuration cfg = new Configuration();  
  53.   
  54.     /** The single instance of hibernate SessionFactory */  
  55.   
  56.     private static org.hibernate.SessionFactory sessionFactory;  
  57.   
  58.     /**  
  59.   
  60.      * Returns the ThreadLocal Session instance.  Lazy initialize  
  61.   
  62.      * the <code>SessionFactory</code> if needed.  
  63.   
  64.      *  
  65.   
  66.      *  @return Session  
  67.   
  68.      *  @throws HibernateException  
  69.   
  70.      */  
  71.   
  72.     public static Session currentSession() throws HibernateException {  
  73.   
  74.         Session session = (Session) threadLocal.get();  
  75.   
  76.   
  77.   
  78.         if (session == null || !session.isOpen()) {  
  79.   
  80.             if (sessionFactory == null) {  
  81.   
  82.                 try {  
  83.   
  84.                     cfg.configure(CONFIG_FILE_LOCATION);  
  85.   
  86.                     sessionFactory = cfg.buildSessionFactory();  
  87.   
  88.                 } catch (Exception e) {  
  89.   
  90.                     System.err  
  91.   
  92.                             .println("%%%% Error Creating SessionFactory %%%%");  
  93.   
  94.                     e.printStackTrace();  
  95.   
  96.                 }  
  97.   
  98.             }  
  99.   
  100.             session = (sessionFactory != null) ? sessionFactory.openSession()  
  101.   
  102.                     : null;  
  103.   
  104.             threadLocal.set(session);  
  105.   
  106.         }  
  107.   
  108.         return session;  
  109.   
  110.     }  
  111.     /**  
  112.   
  113.      *  Close the single hibernate session instance.  
  114.   
  115.      *  
  116.   
  117.      *  @throws HibernateException  
  118.   
  119.      */  
  120.   
  121.     public static void closeSession() throws HibernateException {  
  122.   
  123.         Session session = (Session) threadLocal.get();  
  124.   
  125.         threadLocal.set(null);  
  126.   
  127.         if (session != null) {  
  128.   
  129.             session.close();  
  130.   
  131.         }  
  132.   
  133.     }  
  134.     /**  
  135.   
  136.      * Default constructor.  
  137.   
  138.      */  
  139.   
  140.     private HibernateSessionFactory() {  
  141.   
  142.     }  
  143.   
  144. }  
  145. </pre>  

下面是有表生成的对应的bean文件和对应hibernate需要的*.hbm.xml配置文件,都是工具自动生成的,免的敲啊

AbstractAdmin.java抽象类

[java]  view plain copy
  1. package com.hibernate.domain;  
  2.   
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9. /** 
  10.  
  11.  * AbstractAdmin generated by MyEclipse - Hibernate Tools 
  12.  
  13.  */  
  14.   
  15.   
  16.   
  17. public abstract class AbstractAdmin  implements java.io.Serializable {  
  18.   
  19.   
  20.   
  21.     // Fields      
  22.   
  23.      private long id;  
  24.   
  25.      private String username;  
  26.   
  27.      private String password;  
  28.   
  29.      private long age;  
  30.   
  31.      private String mail;  
  32.   
  33.     /** default constructor */  
  34.   
  35.     public AbstractAdmin() {  
  36.   
  37.     }  
  38.   
  39.     /** full constructor */  
  40.   
  41.     public AbstractAdmin(String username, String password, long age, String mail) {  
  42.   
  43.         this.username = username;  
  44.   
  45.         this.password = password;  
  46.   
  47.         this.age = age;  
  48.   
  49.         this.mail = mail;  
  50.   
  51.     }  
  52.   
  53.     // Property accessors  
  54.   
  55.     public long getId() {  
  56.   
  57.         return this.id;  
  58.   
  59.     }  
  60.   
  61.     public void setId(long id) {  
  62.   
  63.         this.id = id;  
  64.   
  65.     }  
  66.   
  67.   
  68.     public String getUsername() {  
  69.   
  70.         return this.username;  
  71.   
  72.     }  
  73.   
  74.     public void setUsername(String username) {  
  75.   
  76.         this.username = username;  
  77.   
  78.     }  
[java]  view plain copy
  1.     public String getPassword() {  
  2.   
  3.         return this.password;  
  4.   
  5.     }  
  6.   
  7.     public void setPassword(String password) {  
  8.   
  9.         this.password = password;  
  10.   
  11.     }  
  12.   
  13.     public long getAge() {  
  14.   
  15.         return this.age;  
  16.   
  17.     }  
  18.   
  19.     public void setAge(long age) {  
  20.   
  21.         this.age = age;  
  22.   
  23.     }  
  24.   
  25.     public String getMail() {  
  26.   
  27.         return this.mail;  
  28.   
  29.     }  
  30.   
  31.     public void setMail(String mail) {  
  32.   
  33.         this.mail = mail;  
  34.   
  35.     }  
  36.   
  37. }  

   Admin.java继承抽象类

[java]  view plain copy
  1. package com.hibernate.domain;  
  2.   
  3. // Generated by MyEclipse - Hibernate Tools  
  4. /** 
  5.  
  6.  * Admin generated by MyEclipse - Hibernate Tools 
  7.  
  8.  */  
  9.   
  10. public class Admin extends AbstractAdmin implements java.io.Serializable {  
  11.   
  12.     // Constructors  
  13.   
  14.     /** 
  15.  
  16.      *  
  17.  
  18.      */  
  19.   
  20.     private static final long serialVersionUID = 1L;  
  21.   
  22.     /** default constructor */  
  23.   
  24.     public Admin() {  
  25.   
  26.     }  
  27.   
  28.     /** full constructor */  
  29.   
  30.     public Admin(String username, String password, long age, String mail) {  
  31.   
  32.         super(username, password, age, mail);          
  33.   
  34.     }   
  35.   
  36. }  

Admin.hbm.xml配置文件,这个文件主要指定表字段类型对对应的java类型

[xml]  view plain copy
  1. <?xml version="1.0"?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.   
  5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  6.   
  7. <!--  
  8.  
  9.     Mapping file autogenerated by MyEclipse - Hibernate Tools 
  10.  
  11. -->  
  12.   
  13. <hibernate-mapping>  
  14.   
  15.     <class name="com.hibernate.domain.Admin" table="ADMIN">  
  16.   
  17.         <id name="id" type="long">  
  18.   
  19.             <column name="ID" precision="3" scale="0" />  
  20.   
  21.             <generator class="sequence">  
  22.   
  23.                 <param name="sequence">admin_seq</param>      
  24.   
  25.             </generator>  
  26.   
  27.         </id>  
  28.   
  29.         <property name="username" type="string">  
  30.   
  31.             <column name="USERNAME" length="15" not-null="true" />  
  32.   
  33.         </property>  
  34.   
  35.         <property name="password" type="string">  
  36.   
  37.             <column name="PASSWORD" length="15" not-null="true" />  
  38.   
  39.         </property>  
  40.   
  41.         <property name="age" type="long">  
  42.   
  43.             <column name="AGE" precision="3" scale="0" not-null="true" />  
  44.   
  45.         </property>  
  46.   
  47.         <property name="mail" type="string">  
  48.   
  49.             <column name="MAIL" length="20" not-null="true" />  
  50.   
  51.         </property>  
  52.   
  53.     </class>  
  54.   
  55. </hibernate-mapping>  
[xml]  view plain copy
  1. 说明:  
  2.         <id name="id" type="long">  
  3.   
  4.             <column name="ID" precision="3" scale="0" />  
  5.   
  6.             <generator class="sequence">  
  7.   
  8.                 <param name="sequence">admin_seq</param>      
  9.   
  10.             </generator>  
  11.   
  12.         </id>  
[xml]  view plain copy
  1. 代表指定主键以序列的方式生成.  
[xml]  view plain copy
  1. AdminDAOFactory.java,这个类就是我们操作的DAO了,在里面配置好,外部要用就直接调就好了,当然你可以把它封装成service就更好了,辛苦你自己动手把^_^..  
[xml]  view plain copy
  1. <pre class="java" name="code">package com.hibernate.DAOFactory;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import java.util.Collection;  
  6.   
  7. import org.hibernate.HibernateException;  
  8.   
  9. import org.hibernate.Query;  
  10.   
  11. import org.hibernate.Session;  
  12.   
  13. import org.hibernate.Transaction;  
  14.   
  15. import com.hibernate.domain.Admin;  
  16.   
  17. import com.hibernate.sessionFactory.HibernateSessionFactory;  
  18.   
  19. /**  
  20.   
  21.  * @author gjy  
  22.   
  23.  *  
  24.   
  25.  */  
  26.   
  27. public class AdminDAOFactory                  //提供方法给action调用  
  28.   
  29. {  
  30.   
  31.     private Session session;  
  32.   
  33.     private Transaction tx;  
  34.   
  35.     private Query query;  
  36.   
  37.     private Collection aryList;  
  38.   
  39.     public AdminDAOFactory()  
  40.   
  41.     {  
  42.   
  43.     }  
  44.     //  
  45.   
  46.     public Session getSession()  
  47.   
  48.     {  
  49.   
  50.         session=HibernateSessionFactory.currentSession();           //获得会话  
  51.   
  52.         return session;  
  53.   
  54.     }  
  55.     //  
  56.   
  57.     public Collection selAdmin(String userName)  
  58.   
  59.     {  
  60.   
  61.         try  
  62.   
  63.         {  
  64.   
  65.             Session session=getSession();               //获得会话  
  66.   
  67.             tx=session.beginTransaction();              //开始事务  
  68.   
  69.             String strsql="from Admin";  
  70.   
  71.             if(userName!=null&&!userName.equals("")){  
  72.   
  73.                 strsql=strsql+"where userName like '%"+userName+"%'";             
  74.   
  75.             }  
  76.   
  77.             query=session.createQuery(strsql);          //执行查询  
  78.   
  79.             aryList=query.list();     
  80.   
  81.             tx.commit();                                //提交事务,不提交不行              
  82.   
  83.             System.out.println("****Select Success!!****");  
  84.   
  85.         }  
  86.   
  87.         catch(HibernateException ex)  
  88.   
  89.         {  
  90.   
  91.             throw ex;  
  92.   
  93.         }  
  94.   
  95.         finally  
  96.   
  97.         {  
  98.   
  99.             if(tx!=null)  
  100.   
  101.             {  
  102.   
  103.                 tx.rollback();                              //回滚  
  104.   
  105.             }  
  106.   
  107.             HibernateSessionFactory.closeSession();         //关闭会话  
  108.   
  109.         }  
  110.   
  111.       
  112.   
  113.           
  114.   
  115.         return aryList;  
  116.   
  117.     }  
  118.   
  119.     //  
  120.   
  121.     public boolean addAdmin(Admin admin)  
  122.   
  123.     {  
  124.   
  125.         boolean bln=false;  
  126.   
  127.         try  
  128.   
  129.         {  
  130.   
  131.             Session session=getSession();  
  132.   
  133.             tx=session.beginTransaction();  
  134.   
  135.             session.save(admin);  
  136.   
  137.             tx.commit();      
  138.   
  139.             bln=true;  
  140.   
  141.             System.out.println("****Add Success!!****");  
  142.   
  143.         }  
  144.   
  145.         catch(HibernateException ex)  
  146.   
  147.         {  
  148.   
  149.             bln=false;  
  150.   
  151.             throw ex;  
  152.   
  153.         }  
  154.   
  155.         finally  
  156.   
  157.         {  
  158.   
  159.             if(tx!=null)  
  160.   
  161.             {  
  162.   
  163.                 tx.rollback();  
  164.   
  165.             }  
  166.   
  167.             HibernateSessionFactory.closeSession();  
  168.   
  169.         }  
  170.   
  171.         return bln;  
  172.   
  173.     }  
  174.   
  175.     //  
  176.   
  177.     public boolean updAdmain(Admin admin)  
  178.   
  179.     {  
  180.   
  181.         boolean bln=false;  
  182.   
  183.         try  
  184.   
  185.         {  
  186.   
  187.             Session session=getSession();  
  188.   
  189.             tx=session.beginTransaction();  
  190.   
  191.             session.update(admin);  
  192.   
  193.             tx.commit();      
  194.   
  195.             bln=true;  
  196.   
  197.             System.out.println("*****update success******");  
  198.   
  199.         }  
  200.   
  201.         catch(HibernateException ex)  
  202.   
  203.         {  
  204.   
  205.             bln=false;  
  206.   
  207.             throw ex;  
  208.   
  209.         }  
  210.   
  211.         finally  
  212.   
  213.         {  
  214.   
  215.             if(tx!=null)  
  216.   
  217.             {  
  218.   
  219.                 tx.rollback();  
  220.   
  221.             }  
  222.   
  223.             HibernateSessionFactory.closeSession();  
  224.   
  225.         }  
  226.   
  227.         return bln;  
  228.   
  229.     }  
  230.   
  231.     //  
  232.   
  233.     public boolean delAdmin(Long id)  
  234.   
  235.     {  
  236.   
  237.         boolean bln=false;  
  238.   
  239.         try  
  240.   
  241.         {  
  242.   
  243.             Session session=getSession();  
  244.   
  245.             tx=session.beginTransaction();  
  246.   
  247.             Admin admin=(Admin)session.load(Admin.class,id);  
  248.   
  249.             session.delete(admin);  
  250.   
  251.             tx.commit();  
  252.   
  253.             bln=true;  
  254.   
  255.             System.out.println("*****delete success******");  
  256.   
  257.         }  
  258.   
  259.         catch(HibernateException ex)  
  260.   
  261.         {  
  262.   
  263.             bln=false;  
  264.   
  265.             throw ex;  
  266.   
  267.         }  
  268.   
  269.         finally  
  270.   
  271.         {  
  272.   
  273.             if(tx!=null)  
  274.   
  275.             {  
  276.   
  277.                 tx.rollback();  
  278.   
  279.             }  
  280.   
  281.             HibernateSessionFactory.closeSession();  
  282.   
  283.         }     
  284.   
  285.         return bln;  
  286.   
  287.     }     
  288.   
  289. //  public static void main(String[] args)  
  290.   
  291. //  {  
  292.   
  293. //      AdminDAOFactory adminDAOFactory=new AdminDAOFactory();  
  294.   
  295. /*      String userName="";  
  296.   
  297.         ArrayList allAdmin=(ArrayList)adminDAOFactory.selAdmin(userName);  
  298.   
  299.         for(int i=0;i<allAdmin.size();i++)  
  300.   
  301.         {  
  302.   
  303.             System.out.println(((Admin)allAdmin.get(i)).getId());  
  304.   
  305.             System.out.println(((Admin)allAdmin.get(i)).getUsername());  
  306.   
  307.             System.out.println(((Admin)allAdmin.get(i)).getPassword());  
  308.   
  309.             System.out.println(((Admin)allAdmin.get(i)).getAge());  
  310.   
  311.             System.out.println(((Admin)allAdmin.get(i)).getMail());       
  312.   
  313.         }  
  314.   
  315. */  
  316.   
  317.         /*****/  
  318.   
  319. /*      Admin admin=new Admin();  
  320.   
  321.         admin.setUsername("tt");  
  322.   
  323.         admin.setPassword("tt1234");  
  324.   
  325.         admin.setAge(66);  
  326.   
  327.         admin.setMail("tt@126.com");  
  328.   
  329.         adminDAOFactory.addAdmin(admin);  
  330.   
  331. */  
  332.   
  333.         /*****/  
  334.   
  335. /*      Admin admin=new Admin();  
  336.   
  337.         admin.setId(24);  
  338.   
  339.         admin.setUsername("pp");  
  340.   
  341.         admin.setPassword("pp1234");  
  342.   
  343.         admin.setAge(22);  
  344.   
  345.         admin.setMail("pp@126.com");  
  346.   
  347.         adminDAOFactory.updAdmain(admin);  
  348.   
  349. */  
  350.   
  351.         /*****/  
  352.   
  353. //      adminDAOFactory.delAdmin(new Long(25));  
  354.       
  355.   
  356. //  }     
  357.   
  358. }  
  359. 下面进去struts部分了,数据库操作都是调AdminDAOFactory.java中的。</pre>  
  360. <pre class="java" name="code">AdminForm.java,运用了struts中的validate框架验证<pre class="java" name="code">//Created by MyEclipse Struts  
  361.   
  362. // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl  
  363.   
  364. package com.Hs.struts.form;  
  365.   
  366. import javax.servlet.http.HttpServletRequest;  
  367.   
  368. import org.apache.struts.action.ActionErrors;  
  369.   
  370. import org.apache.struts.action.ActionForm;  
  371.   
  372. import org.apache.struts.action.ActionMapping;  
  373.   
  374. import org.apache.struts.action.ActionMessage;  
  375.   
  376. /**  
  377.   
  378.  * @author gjy  
  379.   
  380.  */  
  381.   
  382. /**   
  383.   
  384.  * MyEclipse Struts  
  385.   
  386.  * Creation date: 01-08-2007  
  387.   
  388.  * XDoclet definition:  
  389.   
  390.  * @struts.form name="AdminForm"  
  391.   
  392.  */  
  393.   
  394. public class AdminForm extends ActionForm {  
  395.     // --------------------------------------------------------- Instance Variables  
  396.   
  397.     private static final long serialVersionUID = 1L;  
  398.   
  399.     private long id;  
  400.   
  401.     /** password property */  
  402.   
  403.     private String password;  
  404.   
  405.     /** userName property */  
  406.   
  407.     private String userName;  
  408.   
  409.     /** age property */  
  410.   
  411.     private long age;  
  412.   
  413.     /** mail property */  
  414.   
  415.     private String mail;  
  416.     // --------------------------------------------------------- Methods  
  417.   
  418.     /**   
  419.   
  420.      * Method validate  
  421.   
  422.      * @param mapping  
  423.   
  424.      * @param request  
  425.   
  426.      * @return ActionErrors  
  427.   
  428.      */  
  429.   
  430.     public ActionErrors validate(                  
  431.   
  432.         ActionMapping mapping,  
  433.   
  434.         HttpServletRequest request) {  
  435.   
  436.   
  437.   
  438.         ActionErrors errors=new ActionErrors();  
  439.   
  440.         if(id==0)  
  441.   
  442.         {  
  443.   
  444.             errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.id"));  
  445.   
  446.         }     
  447.   
  448.         if(userName==null && userName.equals(""))  
  449.   
  450.         {  
  451.   
  452.             errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.userName"));  
  453.   
  454.         }  
  455.   
  456.         if(password==null||password.equals(""))  
  457.   
  458.         {  
  459.   
  460.             errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.password"));  
  461.   
  462.         }  
  463.   
  464.         if(age==0)  
  465.   
  466.         {  
  467.   
  468.             errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.age"));  
  469.   
  470.         }  
  471.   
  472.         if(mail==null||mail.equals(""))  
  473.   
  474.         {  
  475.   
  476.             errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.mail"));  
  477.   
  478.         }  
  479.   
  480.         return errors;  
  481.   
  482.     }  
  483.   
  484.     /**   
  485.   
  486.      * Method reset  
  487.   
  488.      * @param mapping  
  489.   
  490.      * @param request  
  491.   
  492.      */  
  493.   
  494.     public void reset(ActionMapping mapping, HttpServletRequest request) {  
  495.   
  496.   
  497.   
  498.         // TODO Auto-generated method stub  
  499.   
  500.     }  
  501.     /**   
  502.   
  503.      * Returns the password.  
  504.   
  505.      * @return String  
  506.   
  507.      */  
  508.   
  509.     public String getPassword() {  
  510.   
  511.         return password;  
  512.   
  513.     }  
  514.     /**   
  515.   
  516.      * Set the password.  
  517.   
  518.      * @param password The password to set  
  519.   
  520.      */  
  521.   
  522.     public void setPassword(String password) {  
  523.   
  524.         this.password = password;  
  525.   
  526.     }  
  527.     /**   
  528.   
  529.      * Returns the userName.  
  530.   
  531.      * @return String  
  532.   
  533.      */  
  534.   
  535.     public String getUserName() {  
  536.   
  537.         return userName;  
  538.   
  539.     }  
  540.     /**   
  541.   
  542.      * Set the userName.  
  543.   
  544.      * @param userName The userName to set  
  545.   
  546.      */  
  547.   
  548.     public void setUserName(String userName) {  
  549.   
  550.         this.userName = userName;  
  551.   
  552.     }  
  553.     /**   
  554.   
  555.      * Returns the age.  
  556.   
  557.      * @return Integer  
  558.   
  559.      */  
  560.   
  561.     public long getAge() {  
  562.   
  563.         return age;  
  564.   
  565.     }  
  566.     /**   
  567.   
  568.      * Set the age.  
  569.   
  570.      * @param age The age to set  
  571.   
  572.      */  
  573.   
  574.     public void setAge(long age) {  
  575.   
  576.         this.age = age;  
  577.   
  578.     }  
  579.   
  580.     /**   
  581.   
  582.      * Returns the mail.  
  583.   
  584.      * @return String  
  585.   
  586.      */  
  587.   
  588.     public String getMail() {  
  589.   
  590.         return mail;  
  591.   
  592.     }  
  593.   
  594.     /**   
  595.   
  596.      * Set the mail.  
  597.   
  598.      * @param mail The mail to set  
  599.   
  600.      */  
  601.   
  602.     public void setMail(String mail) {  
  603.   
  604.         this.mail = mail;  
  605.   
  606.     }  
  607.   
  608.     public long getId() {  
  609.   
  610.         return id;  
  611.   
  612.     }  
  613.   
  614.     public void setId(long id) {  
  615.   
  616.         this.id = id;  
  617.   
  618.     }  
  619. }  
  620.   
  621. </pre>  
  622. </pre>  
  623. <pre class="java" name="code">AddAdminAction.java</pre>  
  624. <pre class="java" name="code"><pre class="java" name="code">//Created by MyEclipse Struts  
  625.   
  626. // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl  
  627.   
  628. package com.Hs.struts.action;  
  629.   
  630. /**  
  631.   
  632.  * @author gjy  
  633.   
  634.  *  
  635.   
  636.  */  
  637.   
  638. import javax.servlet.http.HttpServletRequest;  
  639.   
  640. import javax.servlet.http.HttpServletResponse;  
  641.   
  642. import org.apache.struts.action.Action;  
  643.   
  644. import org.apache.struts.action.ActionForm;  
  645.   
  646. import org.apache.struts.action.ActionForward;  
  647.   
  648. import org.apache.struts.action.ActionMapping;  
  649.   
  650. import com.Hs.struts.form.AdminForm;  
  651.   
  652. import com.hibernate.DAOFactory.AdminDAOFactory;  
  653.   
  654. import com.hibernate.domain.Admin;  
  655.   
  656. /**   
  657.   
  658.  * MyEclipse Struts  
  659.   
  660.  * Creation date: 01-08-2007  
  661.   
  662.  *   
  663.   
  664.  * XDoclet definition:  
  665.   
  666.  * @struts.action path="/addAdmin" name="addAdminForm" input="/admin/addAdmin.jsp" scope="request" validate="true"  
  667.   
  668.  * @struts.action-forward name="success" path="/message.jsp"  
  669.   
  670.  * @struts.action-forward name="error" path="/message.jsp"  
  671.   
  672.  */  
  673.   
  674. public class AddAdminAction extends Action {  
  675.     // --------------------------------------------------------- Instance Variables  
  676.   
  677.     // --------------------------------------------------------- Methods  
  678.   
  679.     /**   
  680.   
  681.      * Method execute  
  682.   
  683.      * @param mapping  
  684.   
  685.      * @param form  
  686.   
  687.      * @param request  
  688.   
  689.      * @param response  
  690.   
  691.      * @return ActionForward  
  692.   
  693.      */  
  694.   
  695.     public ActionForward execute(  
  696.   
  697.         ActionMapping mapping,  
  698.   
  699.         ActionForm form,  
  700.   
  701.         HttpServletRequest request,  
  702.   
  703.         HttpServletResponse response) {  
  704.   
  705.         AdminForm AdminForm = (AdminForm) form;  
  706.   
  707.         String strForward=new String();   
  708.   
  709.         Admin admin=new Admin();  
  710.   
  711.         AdminDAOFactory adminDAOFactory=new AdminDAOFactory();        
  712.   
  713. //       TODO Auto-generated method stub  
  714.   
  715.         String userName=AdminForm.getUserName();  
  716.   
  717.         String password=AdminForm.getPassword();  
  718.   
  719.         long age=AdminForm.getAge();   
  720.   
  721.         String mail=AdminForm.getMail();      
  722.   
  723.         admin.setUsername(userName);  
  724.   
  725.         admin.setPassword(password);  
  726.   
  727.         admin.setId(age);  
  728.   
  729.         admin.setMail(mail);      
  730.   
  731.         if(adminDAOFactory.addAdmin(admin))  
  732.   
  733.         {         
  734.   
  735.             strForward="success";  
  736.   
  737.         }  
  738.   
  739.         else  
  740.   
  741.         {  
  742.   
  743.             strForward="error";  
  744.   
  745.         }  
  746.   
  747.       
  748.         return mapping.findForward(strForward);  
  749.   
  750.     }  
  751.   
  752.   
  753.   
  754. }</pre>  
  755. <pre class="java" name="code"> </pre>  
  756. AllAdminAction.java  
  757. </pre>  
[xml]  view plain copy
  1. <pre class="java" name="code">package com.Hs.struts.action;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6.   
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.struts.action.Action;  
  10.   
  11. import org.apache.struts.action.ActionForm;  
  12.   
  13. import org.apache.struts.action.ActionForward;  
  14.   
  15. import org.apache.struts.action.ActionMapping;  
  16.   
  17. import com.Hs.struts.form.AdminForm;  
  18.   
  19. import com.hibernate.DAOFactory.AdminDAOFactory;  
  20.   
  21. import com.hibernate.domain.Admin;  
  22.   
  23. /**  
  24.   
  25.  * @author gjy  
  26.   
  27.  *  
  28.   
  29.  */  
  30.   
  31. public class AllAdminAction extends Action   
  32.   
  33. {  
  34.   
  35.     public ActionForward execute(  
  36.   
  37.             ActionMapping mapping,  
  38.   
  39.             ActionForm form,  
  40.   
  41.             HttpServletRequest request,  
  42.   
  43.             HttpServletResponse response)   
  44.   
  45.     {  
  46.   
  47.         System.out.println("00000000000000000000");  
  48.   
  49.         AdminForm AdminForm = (AdminForm) form;   
  50.   
  51. //      HttpSession session=request.getSession();  
  52.   
  53.         AdminDAOFactory adminDAOFactory=new AdminDAOFactory();    
  54.   
  55.         //  
  56.   
  57.         System.out.println("111111111111111");  
  58.   
  59.         String  userName=(String)request.getAttribute("userName");  
  60.   
  61.         ArrayList allAdmin;  
  62.   
  63.         String map="";  
  64.   
  65.           
  66.   
  67.         if(userName==null)  
  68.   
  69.         {  
  70.   
  71.             userName="";  
  72.   
  73.             //  
  74.   
  75.             System.out.println("22222222222222");  
  76.   
  77.         }  
  78.   
  79.         //    
  80.   
  81.             allAdmin=new ArrayList();  
  82.   
  83.             allAdmin=(ArrayList)adminDAOFactory.selAdmin(userName);  
  84.   
  85.       
  86.   
  87.             for(int i=0;i<allAdmin.size();i++)  
  88.   
  89.             {  
  90.   
  91.                 System.out.println(((Admin)allAdmin.get(i)).getUsername());  
  92.   
  93.             }             
  94.   
  95.           
  96.   
  97.             if(allAdmin!=null)  
  98.   
  99.             {  
  100.   
  101.                 request.setAttribute("allAdmin", allAdmin);  
  102.   
  103.                 map="success";  
  104.   
  105.             }  
  106.   
  107.             else  
  108.   
  109.             {  
  110.   
  111.                 map="error";  
  112.   
  113.             }     
  114.   
  115.         return mapping.findForward(map);  
  116.   
  117.     }     
  118.   
  119. }</pre>  
  120.  DelAdminAction.java  
[xml]  view plain copy
  1. <pre class="java" name="code">package com.Hs.struts.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. import org.apache.struts.action.Action;  
  8.   
  9. import org.apache.struts.action.ActionForm;  
  10.   
  11. import org.apache.struts.action.ActionForward;  
  12.   
  13. import org.apache.struts.action.ActionMapping;  
  14.   
  15. import com.hibernate.DAOFactory.AdminDAOFactory;  
  16.   
  17. /**  
  18.   
  19.  * @author gjy  
  20.   
  21.  *  
  22.   
  23.  */  
  24.   
  25. public class DelAdminAction extends Action   
  26.   
  27. {  
  28.   
  29.     public ActionForward execute(  
  30.   
  31.             ActionMapping mapping,  
  32.   
  33.             ActionForm form,  
  34.   
  35.             HttpServletRequest request,  
  36.   
  37.             HttpServletResponse response) {  
  38.   
  39. //      AdminForm AdminForm = (AdminForm) form;   
  40.   
  41.         AdminDAOFactory adminDAOFactory=new AdminDAOFactory();    
  42.   
  43.         String id=(String)request.getParameter("key");  
  44.   
  45.         System.out.println("***ID"+id);       
  46.   
  47.         Long aid=Long.valueOf(id);  
  48.   
  49.         if(adminDAOFactory.delAdmin(aid))  
  50.   
  51.         {  
  52.   
  53.             return mapping.findForward("success");  
  54.   
  55.         }  
  56.   
  57.         else  
  58.   
  59.         {  
  60.   
  61.             return mapping.findForward("error");  
  62.   
  63.         }         
  64.   
  65.     }         
  66.   
  67. }  
  68. </pre>  

UpdateAdminAction.java  

[java]  view plain copy
  1. package com.Hs.struts.action;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. import org.apache.struts.action.Action;  
  8.   
  9. import org.apache.struts.action.ActionForm;  
  10.   
  11. import org.apache.struts.action.ActionForward;  
  12.   
  13. import org.apache.struts.action.ActionMapping;  
  14.   
  15. import com.Hs.struts.form.AdminForm;  
  16.   
  17. import com.hibernate.DAOFactory.AdminDAOFactory;  
  18.   
  19. import com.hibernate.domain.Admin;  
  20.   
  21. /** 
  22.  
  23.  * @author gjy 
  24.  
  25.  * 
  26.  
  27.  */  
  28.   
  29. public class UpdateAdminAction extends Action   
  30.   
  31. {  
  32.   
  33.     public ActionForward execute(  
  34.   
  35.             ActionMapping mapping,  
  36.   
  37.             ActionForm form,  
  38.   
  39.             HttpServletRequest request,  
  40.   
  41.             HttpServletResponse response) {  
  42.               
  43.   
  44.         AdminForm AdminForm = (AdminForm) form;   
  45.   
  46.         AdminDAOFactory adminDAOFactory=new AdminDAOFactory();        
  47.   
  48.         System.out.println("*****ID:"+AdminForm.getId());  
  49.   
  50.         Admin admin=new Admin();  
  51.   
  52.         long id=AdminForm.getId();  
  53.   
  54.     //  long aid=Long.parseLong(id);          
  55.   
  56.         admin.setId(id);  
  57.   
  58.         admin.setUsername(AdminForm.getUserName());  
  59.   
  60.         admin.setPassword(AdminForm.getPassword());  
  61.   
  62.         admin.setAge(AdminForm.getAge());  
  63.   
  64.         admin.setMail(AdminForm.getMail());  
  65.   
  66.           
  67.   
  68.         if(adminDAOFactory.updAdmain(admin))  
  69.   
  70.         {  
  71.   
  72.             return mapping.findForward("success");  
  73.   
  74.         }  
  75.   
  76.         else  
  77.   
  78.         {  
  79.   
  80.             return mapping.findForward("error");  
  81.   
  82.         }  
  83.   
  84.     }     
  85.   
  86. }  

 struts-config.xml文件

[xml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
  4.   
  5. <struts-config>  
  6.   
  7.   <data-sources />  
  8.   
  9.   <form-beans >  
  10.   
  11.     <form-bean name="AdminForm" type="com.Hs.struts.form.AdminForm" />  
  12.   
  13.   
  14.   </form-beans>  
  15.   
  16.   <global-exceptions />  
  17.   
  18.   <global-forwards />  
  19.   
  20.   <action-mappings >  
  21.   
  22.     <action  
  23.   
  24.       attribute="AdminForm"  
  25.   
  26.       input="/message.jsp"  
  27.   
  28.       name="AdminForm"  
  29.   
  30.       path="/addAdmin"  
  31.   
  32.       scope="request"  
  33.   
  34.       type="com.Hs.struts.action.AddAdminAction"  
  35.   
  36.       validate="false">  
  37.   
  38.       <forward name="success" path="/success.jsp" />  
  39.   
  40.       <forward name="error" path="/error.jsp" />  
  41.   
  42.     </action>  
[xml]  view plain copy
  1. <action  
  2.   
  3.      attribute="AdminForm"  
  4.   
  5.      input="/message.jsp"  
  6.   
  7.      name="AdminForm"  
  8.   
  9.      path="/mainAdmin"  
  10.   
  11.      scope="session"  
  12.   
  13.      type="com.Hs.struts.action.AllAdminAction"  
  14.   
  15.      validate="false">  
  16.   
  17.      <forward name="success" path="/admin/allAdmin.jsp" />  
  18.   
  19.      <forward name="error" path="/error.jsp" />  
  20.   
  21.    </action>    
  22.   
  23. <action  
  24.   
  25.      attribute="AdminForm"  
  26.   
  27.      input="/message.jsp"  
  28.   
  29.      name="AdminForm"  
  30.   
  31.      path="/updateAdmin"  
  32.   
  33.      scope="request"  
  34.   
  35.      type="com.Hs.struts.action.UpdateAdminAction"  
  36.   
  37.      validate="true">  
  38.   
  39.      <forward name="success" path="/success.jsp" />  
  40.   
  41.      <forward name="error" path="/error.jsp" />  
  42.   
  43.    </action>  
  44.   
  45.   <action  
  46.   
  47.      attribute="AdminForm"  
  48.   
  49.      input="/message.jsp"  
  50.   
  51.      name="AdminForm"  
  52.   
  53.      path="/deleteAdmin"  
  54.   
  55.      scope="request"  
  56.   
  57.      type="com.Hs.struts.action.DelAdminAction"  
  58.   
  59.      validate="false">  
  60.   
  61.      <forward name="success" path="/success.jsp" />  
  62.   
  63.      <forward name="error" path="/error.jsp" />  
  64.   
  65.    </action>  
  66.   
  67.  </action-mappings>  
  68.   
  69.  <message-resources parameter="com.Hs.struts.ApplicationResources" />  
  70.   
  71. lt;/struts-config>  

 web.xml文件

[xml]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  4.   
  5.   <servlet>  
  6.   
  7.     <servlet-name>action</servlet-name>  
  8.   
  9.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  10.   
  11.     <init-param>  
  12.   
  13.       <param-name>config</param-name>  
  14.   
  15.       <param-value>/WEB-INF/struts-config.xml</param-value>  
  16.   
  17.     </init-param>  
  18.   
  19.     <init-param>  
  20.   
  21.       <param-name>debug</param-name>  
  22.   
  23.       <param-value>3</param-value>  
  24.   
  25.     </init-param>  
  26.   
  27.     <init-param>  
  28.   
  29.       <param-name>detail</param-name>  
  30.   
  31.       <param-value>3</param-value>  
  32.   
  33.     </init-param>  
  34.   
  35.     <load-on-startup>0</load-on-startup>  
  36.   
  37.   </servlet>  
  38.   
  39.   <servlet-mapping>  
  40.   
  41.     <servlet-name>action</servlet-name>  
  42.   
  43.     <url-pattern>*.do</url-pattern>  
  44.   
  45.   </servlet-mapping>  
  46.   
  47.     <welcome-file-list>  
  48.   
  49.         <welcome-file>index.jsp</welcome-file>  
  50.   
  51.     </welcome-file-list>  
  52.   
  53. </web-app>  
[xml]  view plain copy
  1. 继续见(二)部分^_^....  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值