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 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">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="connection.username">cwgladm</property>
- <property name="connection.url">jdbc:oracle:thin:@192.168.0.201:1521:cwgl</property>
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <property name="myeclipse.connection.profile">DataConn</property>
- <property name="connection.password">cwglpass</property>
- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <mapping resource="com/hibernate/domain/Admin.hbm.xml" />
- </session-factory>
- 说明:数据库连接信息就不多少了,<mapping resource="com/hibernate/domain/Admin.hbm.xml" />
- </hibernate-configuration>是指定要用到的表对应的配置文件信息.
- HibernateSessionFactory.java,hibernate会话管理,用myeclips自动生成的,只要修改下指向配置文件的路径就可以了
- <pre class="java" name="code">package com.hibernate.sessionFactory;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
- /**
- * Configures and provides access to Hibernate sessions, tied to the
- * current thread of execution. Follows the Thread Local Session
- * pattern, see {@link http://hibernate.org/42.html}.
- */
- public class HibernateSessionFactory {
- /**
- * Location of hibernate.cfg.xml file.
- * NOTICE: Location should be on the classpath as Hibernate uses
- * #resourceAsStream style lookup for its configuration file. That
- * is place the config file in a Java package - the default location
- * is the default Java package.<br><br>
- * Examples: <br>
- * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
- * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
- */
- //指定配置文件的路径,其他没改什么
- private static String CONFIG_FILE_LOCATION = "/com/hibernate/properties/hibernate.cfg.xml";
- /** Holds a single instance of Session */
- private static final ThreadLocal threadLocal = new ThreadLocal();
- /** The single instance of hibernate configuration */
- private static final Configuration cfg = new Configuration();
- /** The single instance of hibernate SessionFactory */
- private static org.hibernate.SessionFactory sessionFactory;
- /**
- * Returns the ThreadLocal Session instance. Lazy initialize
- * the <code>SessionFactory</code> if needed.
- *
- * @return Session
- * @throws HibernateException
- */
- public static Session currentSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- if (session == null || !session.isOpen()) {
- if (sessionFactory == null) {
- try {
- cfg.configure(CONFIG_FILE_LOCATION);
- sessionFactory = cfg.buildSessionFactory();
- } catch (Exception e) {
- System.err
- .println("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- session = (sessionFactory != null) ? sessionFactory.openSession()
- : null;
- threadLocal.set(session);
- }
- return session;
- }
- /**
- * Close the single hibernate session instance.
- *
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- threadLocal.set(null);
- if (session != null) {
- session.close();
- }
- }
- /**
- * Default constructor.
- */
- private HibernateSessionFactory() {
- }
- }
- </pre>
下面是有表生成的对应的bean文件和对应hibernate需要的*.hbm.xml配置文件,都是工具自动生成的,免的敲啊
AbstractAdmin.java抽象类
- package com.hibernate.domain;
- /**
- * AbstractAdmin generated by MyEclipse - Hibernate Tools
- */
- public abstract class AbstractAdmin implements java.io.Serializable {
- // Fields
- private long id;
- private String username;
- private String password;
- private long age;
- private String mail;
- /** default constructor */
- public AbstractAdmin() {
- }
- /** full constructor */
- public AbstractAdmin(String username, String password, long age, String mail) {
- this.username = username;
- this.password = password;
- this.age = age;
- this.mail = mail;
- }
- // Property accessors
- public long getId() {
- return this.id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getUsername() {
- return this.username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return this.password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public long getAge() {
- return this.age;
- }
- public void setAge(long age) {
- this.age = age;
- }
- public String getMail() {
- return this.mail;
- }
- public void setMail(String mail) {
- this.mail = mail;
- }
- }
Admin.java继承抽象类
- package com.hibernate.domain;
- // Generated by MyEclipse - Hibernate Tools
- /**
- * Admin generated by MyEclipse - Hibernate Tools
- */
- public class Admin extends AbstractAdmin implements java.io.Serializable {
- // Constructors
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- /** default constructor */
- public Admin() {
- }
- /** full constructor */
- public Admin(String username, String password, long age, String mail) {
- super(username, password, age, mail);
- }
- }
Admin.hbm.xml配置文件,这个文件主要指定表字段类型对对应的java类型
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse - Hibernate Tools
- -->
- <hibernate-mapping>
- <class name="com.hibernate.domain.Admin" table="ADMIN">
- <id name="id" type="long">
- <column name="ID" precision="3" scale="0" />
- <generator class="sequence">
- <param name="sequence">admin_seq</param>
- </generator>
- </id>
- <property name="username" type="string">
- <column name="USERNAME" length="15" not-null="true" />
- </property>
- <property name="password" type="string">
- <column name="PASSWORD" length="15" not-null="true" />
- </property>
- <property name="age" type="long">
- <column name="AGE" precision="3" scale="0" not-null="true" />
- </property>
- <property name="mail" type="string">
- <column name="MAIL" length="20" not-null="true" />
- </property>
- </class>
- </hibernate-mapping>
- 说明:
- <id name="id" type="long">
- <column name="ID" precision="3" scale="0" />
- <generator class="sequence">
- <param name="sequence">admin_seq</param>
- </generator>
- </id>
- 代表指定主键以序列的方式生成.
- AdminDAOFactory.java,这个类就是我们操作的DAO了,在里面配置好,外部要用就直接调就好了,当然你可以把它封装成service就更好了,辛苦你自己动手把^_^..
- <pre class="java" name="code">package com.hibernate.DAOFactory;
- import java.util.ArrayList;
- import java.util.Collection;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import com.hibernate.domain.Admin;
- import com.hibernate.sessionFactory.HibernateSessionFactory;
- /**
- * @author gjy
- *
- */
- public class AdminDAOFactory //提供方法给action调用
- {
- private Session session;
- private Transaction tx;
- private Query query;
- private Collection aryList;
- public AdminDAOFactory()
- {
- }
- //
- public Session getSession()
- {
- session=HibernateSessionFactory.currentSession(); //获得会话
- return session;
- }
- //
- public Collection selAdmin(String userName)
- {
- try
- {
- Session session=getSession(); //获得会话
- tx=session.beginTransaction(); //开始事务
- String strsql="from Admin";
- if(userName!=null&&!userName.equals("")){
- strsql=strsql+"where userName like '%"+userName+"%'";
- }
- query=session.createQuery(strsql); //执行查询
- aryList=query.list();
- tx.commit(); //提交事务,不提交不行
- System.out.println("****Select Success!!****");
- }
- catch(HibernateException ex)
- {
- throw ex;
- }
- finally
- {
- if(tx!=null)
- {
- tx.rollback(); //回滚
- }
- HibernateSessionFactory.closeSession(); //关闭会话
- }
- return aryList;
- }
- //
- public boolean addAdmin(Admin admin)
- {
- boolean bln=false;
- try
- {
- Session session=getSession();
- tx=session.beginTransaction();
- session.save(admin);
- tx.commit();
- bln=true;
- System.out.println("****Add Success!!****");
- }
- catch(HibernateException ex)
- {
- bln=false;
- throw ex;
- }
- finally
- {
- if(tx!=null)
- {
- tx.rollback();
- }
- HibernateSessionFactory.closeSession();
- }
- return bln;
- }
- //
- public boolean updAdmain(Admin admin)
- {
- boolean bln=false;
- try
- {
- Session session=getSession();
- tx=session.beginTransaction();
- session.update(admin);
- tx.commit();
- bln=true;
- System.out.println("*****update success******");
- }
- catch(HibernateException ex)
- {
- bln=false;
- throw ex;
- }
- finally
- {
- if(tx!=null)
- {
- tx.rollback();
- }
- HibernateSessionFactory.closeSession();
- }
- return bln;
- }
- //
- public boolean delAdmin(Long id)
- {
- boolean bln=false;
- try
- {
- Session session=getSession();
- tx=session.beginTransaction();
- Admin admin=(Admin)session.load(Admin.class,id);
- session.delete(admin);
- tx.commit();
- bln=true;
- System.out.println("*****delete success******");
- }
- catch(HibernateException ex)
- {
- bln=false;
- throw ex;
- }
- finally
- {
- if(tx!=null)
- {
- tx.rollback();
- }
- HibernateSessionFactory.closeSession();
- }
- return bln;
- }
- // public static void main(String[] args)
- // {
- // AdminDAOFactory adminDAOFactory=new AdminDAOFactory();
- /* String userName="";
- ArrayList allAdmin=(ArrayList)adminDAOFactory.selAdmin(userName);
- for(int i=0;i<allAdmin.size();i++)
- {
- System.out.println(((Admin)allAdmin.get(i)).getId());
- System.out.println(((Admin)allAdmin.get(i)).getUsername());
- System.out.println(((Admin)allAdmin.get(i)).getPassword());
- System.out.println(((Admin)allAdmin.get(i)).getAge());
- System.out.println(((Admin)allAdmin.get(i)).getMail());
- }
- */
- /*****/
- /* Admin admin=new Admin();
- admin.setUsername("tt");
- admin.setPassword("tt1234");
- admin.setAge(66);
- admin.setMail("tt@126.com");
- adminDAOFactory.addAdmin(admin);
- */
- /*****/
- /* Admin admin=new Admin();
- admin.setId(24);
- admin.setUsername("pp");
- admin.setPassword("pp1234");
- admin.setAge(22);
- admin.setMail("pp@126.com");
- adminDAOFactory.updAdmain(admin);
- */
- /*****/
- // adminDAOFactory.delAdmin(new Long(25));
- // }
- }
- 下面进去struts部分了,数据库操作都是调AdminDAOFactory.java中的。</pre>
- <pre class="java" name="code">AdminForm.java,运用了struts中的validate框架验证<pre class="java" name="code">//Created by MyEclipse Struts
- // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl
- package com.Hs.struts.form;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts.action.ActionErrors;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.ActionMessage;
- /**
- * @author gjy
- */
- /**
- * MyEclipse Struts
- * Creation date: 01-08-2007
- * XDoclet definition:
- * @struts.form name="AdminForm"
- */
- public class AdminForm extends ActionForm {
- // --------------------------------------------------------- Instance Variables
- private static final long serialVersionUID = 1L;
- private long id;
- /** password property */
- private String password;
- /** userName property */
- private String userName;
- /** age property */
- private long age;
- /** mail property */
- private String mail;
- // --------------------------------------------------------- Methods
- /**
- * Method validate
- * @param mapping
- * @param request
- * @return ActionErrors
- */
- public ActionErrors validate(
- ActionMapping mapping,
- HttpServletRequest request) {
- ActionErrors errors=new ActionErrors();
- if(id==0)
- {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.id"));
- }
- if(userName==null && userName.equals(""))
- {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.userName"));
- }
- if(password==null||password.equals(""))
- {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.password"));
- }
- if(age==0)
- {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.age"));
- }
- if(mail==null||mail.equals(""))
- {
- errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("errors.mail"));
- }
- return errors;
- }
- /**
- * Method reset
- * @param mapping
- * @param request
- */
- public void reset(ActionMapping mapping, HttpServletRequest request) {
- // TODO Auto-generated method stub
- }
- /**
- * Returns the password.
- * @return String
- */
- public String getPassword() {
- return password;
- }
- /**
- * Set the password.
- * @param password The password to set
- */
- public void setPassword(String password) {
- this.password = password;
- }
- /**
- * Returns the userName.
- * @return String
- */
- public String getUserName() {
- return userName;
- }
- /**
- * Set the userName.
- * @param userName The userName to set
- */
- public void setUserName(String userName) {
- this.userName = userName;
- }
- /**
- * Returns the age.
- * @return Integer
- */
- public long getAge() {
- return age;
- }
- /**
- * Set the age.
- * @param age The age to set
- */
- public void setAge(long age) {
- this.age = age;
- }
- /**
- * Returns the mail.
- * @return String
- */
- public String getMail() {
- return mail;
- }
- /**
- * Set the mail.
- * @param mail The mail to set
- */
- public void setMail(String mail) {
- this.mail = mail;
- }
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- }
- </pre>
- </pre>
- <pre class="java" name="code">AddAdminAction.java</pre>
- <pre class="java" name="code"><pre class="java" name="code">//Created by MyEclipse Struts
- // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl
- package com.Hs.struts.action;
- /**
- * @author gjy
- *
- */
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.Hs.struts.form.AdminForm;
- import com.hibernate.DAOFactory.AdminDAOFactory;
- import com.hibernate.domain.Admin;
- /**
- * MyEclipse Struts
- * Creation date: 01-08-2007
- *
- * XDoclet definition:
- * @struts.action path="/addAdmin" name="addAdminForm" input="/admin/addAdmin.jsp" scope="request" validate="true"
- * @struts.action-forward name="success" path="/message.jsp"
- * @struts.action-forward name="error" path="/message.jsp"
- */
- public class AddAdminAction extends Action {
- // --------------------------------------------------------- Instance Variables
- // --------------------------------------------------------- Methods
- /**
- * Method execute
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- AdminForm AdminForm = (AdminForm) form;
- String strForward=new String();
- Admin admin=new Admin();
- AdminDAOFactory adminDAOFactory=new AdminDAOFactory();
- // TODO Auto-generated method stub
- String userName=AdminForm.getUserName();
- String password=AdminForm.getPassword();
- long age=AdminForm.getAge();
- String mail=AdminForm.getMail();
- admin.setUsername(userName);
- admin.setPassword(password);
- admin.setId(age);
- admin.setMail(mail);
- if(adminDAOFactory.addAdmin(admin))
- {
- strForward="success";
- }
- else
- {
- strForward="error";
- }
- return mapping.findForward(strForward);
- }
- }</pre>
- <pre class="java" name="code"> </pre>
- AllAdminAction.java
- </pre>
- <pre class="java" name="code">package com.Hs.struts.action;
- import java.util.ArrayList;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.Hs.struts.form.AdminForm;
- import com.hibernate.DAOFactory.AdminDAOFactory;
- import com.hibernate.domain.Admin;
- /**
- * @author gjy
- *
- */
- public class AllAdminAction extends Action
- {
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response)
- {
- System.out.println("00000000000000000000");
- AdminForm AdminForm = (AdminForm) form;
- // HttpSession session=request.getSession();
- AdminDAOFactory adminDAOFactory=new AdminDAOFactory();
- //
- System.out.println("111111111111111");
- String userName=(String)request.getAttribute("userName");
- ArrayList allAdmin;
- String map="";
- if(userName==null)
- {
- userName="";
- //
- System.out.println("22222222222222");
- }
- //
- allAdmin=new ArrayList();
- allAdmin=(ArrayList)adminDAOFactory.selAdmin(userName);
- for(int i=0;i<allAdmin.size();i++)
- {
- System.out.println(((Admin)allAdmin.get(i)).getUsername());
- }
- if(allAdmin!=null)
- {
- request.setAttribute("allAdmin", allAdmin);
- map="success";
- }
- else
- {
- map="error";
- }
- return mapping.findForward(map);
- }
- }</pre>
- DelAdminAction.java
- <pre class="java" name="code">package com.Hs.struts.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.hibernate.DAOFactory.AdminDAOFactory;
- /**
- * @author gjy
- *
- */
- public class DelAdminAction extends Action
- {
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- // AdminForm AdminForm = (AdminForm) form;
- AdminDAOFactory adminDAOFactory=new AdminDAOFactory();
- String id=(String)request.getParameter("key");
- System.out.println("***ID"+id);
- Long aid=Long.valueOf(id);
- if(adminDAOFactory.delAdmin(aid))
- {
- return mapping.findForward("success");
- }
- else
- {
- return mapping.findForward("error");
- }
- }
- }
- </pre>
UpdateAdminAction.java
- package com.Hs.struts.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.Hs.struts.form.AdminForm;
- import com.hibernate.DAOFactory.AdminDAOFactory;
- import com.hibernate.domain.Admin;
- /**
- * @author gjy
- *
- */
- public class UpdateAdminAction extends Action
- {
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- AdminForm AdminForm = (AdminForm) form;
- AdminDAOFactory adminDAOFactory=new AdminDAOFactory();
- System.out.println("*****ID:"+AdminForm.getId());
- Admin admin=new Admin();
- long id=AdminForm.getId();
- // long aid=Long.parseLong(id);
- admin.setId(id);
- admin.setUsername(AdminForm.getUserName());
- admin.setPassword(AdminForm.getPassword());
- admin.setAge(AdminForm.getAge());
- admin.setMail(AdminForm.getMail());
- if(adminDAOFactory.updAdmain(admin))
- {
- return mapping.findForward("success");
- }
- else
- {
- return mapping.findForward("error");
- }
- }
- }
struts-config.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <data-sources />
- <form-beans >
- <form-bean name="AdminForm" type="com.Hs.struts.form.AdminForm" />
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <action
- attribute="AdminForm"
- input="/message.jsp"
- name="AdminForm"
- path="/addAdmin"
- scope="request"
- type="com.Hs.struts.action.AddAdminAction"
- validate="false">
- <forward name="success" path="/success.jsp" />
- <forward name="error" path="/error.jsp" />
- </action>
- <action
- attribute="AdminForm"
- input="/message.jsp"
- name="AdminForm"
- path="/mainAdmin"
- scope="session"
- type="com.Hs.struts.action.AllAdminAction"
- validate="false">
- <forward name="success" path="/admin/allAdmin.jsp" />
- <forward name="error" path="/error.jsp" />
- </action>
- <action
- attribute="AdminForm"
- input="/message.jsp"
- name="AdminForm"
- path="/updateAdmin"
- scope="request"
- type="com.Hs.struts.action.UpdateAdminAction"
- validate="true">
- <forward name="success" path="/success.jsp" />
- <forward name="error" path="/error.jsp" />
- </action>
- <action
- attribute="AdminForm"
- input="/message.jsp"
- name="AdminForm"
- path="/deleteAdmin"
- scope="request"
- type="com.Hs.struts.action.DelAdminAction"
- validate="false">
- <forward name="success" path="/success.jsp" />
- <forward name="error" path="/error.jsp" />
- </action>
- </action-mappings>
- <message-resources parameter="com.Hs.struts.ApplicationResources" />
- lt;/struts-config>
web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts-config.xml</param-value>
- </init-param>
- <init-param>
- <param-name>debug</param-name>
- <param-value>3</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>3</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- 继续见(二)部分^_^....