176142998

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks
Struts+Spring+Hibernate
  本次配置环境:Myeclipse5.5、MySQL5.0、Struts1.2、Spring2.0、Hibernate3.1
  一、建工程
  略。。。。。。
  二、要使用Struts、Spring、Hibernate必须导入必需的包
  1、Struts(和之前没区别)
  2、Spring
     分别导入Spring 2.0 Core Libraries、Spring 2.0 Web Libraries
     选择把*.jar Copy到工程/WebRoot/WEB-INF/lib下; 点击NEXT
     选择applicationContext.xml的目录,/WebRoot/WEB-INF;点击finish
  3、Hibernate
     在导入Hibernate时,当然先要配置DataSource咯,这里就不再说了
     选择导入Hibernate全选上
     选上复选框:Hibernate 3.1 Core......、Hibernate 3.1 Advanced......、Spring 2.0 ORM/DAO.......
     同样选择把*.jar Copy到工程/WebRoot/WEB-INF/lib下; 点击NEXT
     这里我们选择把hibernate交给spring去管理
  选中单选按钮 Spring configuration file...... 点击NEXT
     选择已存在的applicationContext.xml文件,
     填写SessionFactory ID :sessionFactory 点击NEXT
     这里我们需要填写Bean Id :dataSource
     选择 DB Driver :选择刚才配置的DataSource 点击NEXT
     这里不需要创建 SessionFactory Class 点击Finish
     注意:查看applicationContext.xml的变化
  三、映射VO、数据操作
  首先工程的结构建好,比较简单的结构:
  org.chenwj.dao
  org.chenwj.struts
  org.chenwj.struts.action
  org.chenwj.struts.form
  org.chenwj.vo
  映射表userinfo创建持久类到org.chenwj.vo目录
  在dao下创建数据库操作类 UserDAO 这里只是对数据库进去插入,代码如下:
  private SessionFactory sessionFactory;

      public SessionFactory getSessionFactory() ...{
         return sessionFactory;
      }
      public void setSessionFactory(SessionFactory sessionFactory) ...{
         this.sessionFactory = sessionFactory;
      }
      /**//* 用户注册 */
      public boolean regist(Userinfo user) ...{
         try ...{
             Session session = sessionFactory.openSession();
             Transaction tx = session.beginTransaction();
             session.save(user);
             tx.commit();
             session.close();
             return true;
         } catch (Exception ex) ...{
             ex.printStackTrace();
             return false;
                }
         }
      使用依赖注入,setter设值 sessionFactory
      到此数据层已经完成

  四、配置struts-config.xml
      添加action、form、jsp 略……
      首先在struts-config.xml添加一个插件
      <plug-in
      className="org.springframework.web.struts.ContextLoaderPlugIn">
         <set-property property="contextConfigLocation"
             value="/WEB-INF/applicationContext.xml" />
      </plug-in>
      为什么要添回这个插件呢?
      因为在后面会在applicationContext.xml下配置action,让action交给spring
      去管理,实现了struts的依赖注入机制
      接下来添加cuntroller,这里你可以使用DelegatingActionProxy代理
      <controller processorClass=
      "org.springframework.web.struts.DelegatingRequestProcessor"/>
      Controller取代了struts的RequestProcessor,在定义action里,我们可以省略
  type属性。(我个人比较喜欢用这个)下面让我们看配置好的struts-config.xml:
  <struts-config>
          <data-sources />
          <form-beans>
             <form-bean name="userForm"
             type="org.chenwj.struts.form.UserForm" />
          </form-beans>
      <global-exceptions />
      <global-forwards />
      <action-mappings>
         <action attribute="userForm" input="/index.jsp" name="userForm"
             path="/user" scope="request">
             <forward name="success" path="/success.jsp" />
             <forward name="error" path="/index.jsp" />
         </action><!--type属性可不写-->
      </action-mappings>

      <controller processorClass=
  "org.springframework.web.struts.DelegatingRequestProcessor"/>

       <message-resources
         parameter="org.chenwj.struts.ApplicationResources" />
       <plug-in
      className="org.springframework.web.struts.ContextLoaderPlugIn">
         <set-property property="contextConfigLocation"
             value="/WEB-INF/applicationContext.xml" />
       </plug-in>
  </struts-config>

五、在applicationContext.xml配置action
      这里我们先在 action类里添加一些业务逻辑,代码如下:
      public class UserAction extends Action ...{

 

      private UserDAO userDao;
      private Userinfo user;

      public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response) ...{
     UserForm userForm = (UserForm) form;
     //封装数据
     user.setName(userForm.getName());
         user.setPassword(userForm.getPassword());
         if(userDao.regist(user))...{
             return mapping.findForward("success");
         }
         return mapping.findForward("error");
      }

      public Userinfo getUser() ...{
         return user;
      }
      public void setUser(Userinfo user) ...{
         this.user = user;
      }
      public UserDAO getUserDao() ...{
         return userDao;
      }
      public void setUserDao(UserDAO userDao) ...{
         this.userDao = userDao;
      }}
      这里使用setter实现依赖注入了两个bean,接下来配置applicationContext.xml
      <beans xmlns="略……">
      <!—- 数据源 -->
      <bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource">
         <property name="driverClassName"
             value="com.mysql.jdbc.Driver">
         </property>
          <property name="url"
  value="jdbc:mysql://localhost:3306/demo"></property>
         <property name="username" value="root"></property>
         <property name="password" value="root"></property>
      </bean>
      <!--  sessionFactory -->
      <bean id="sessionFactory" class=
  "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
             <ref bean="dataSource" />
         </property>
         <property name="hibernateProperties">
             <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
             </props>
         </property>
         <property name="mappingResources">
             <list>
                <value>org/chenwj/vo/Userinfo.hbm.xml</value>
             </list>
         </property>
      </bean>
      <!--  数据库操作类  -->
      <bean id="userDao" class="org.chenwj.dao.UserDAO">
         <property name="sessionFactory">
             <ref local="sessionFactory" />
         </property>
      </bean>
      <!--  action需要注意:这里是name属性不是ID,同时要和struts-config.xml
        对应的 action path属性值相同,斜线也是必需的,通过这个属性scope=
        "prototype" 每次获取bean实例时都会产生新的实例,默认是单例-->
      <bean name="/user" class="org.chenwj.struts.action.UserAction"
         abstract="false" lazy-init="default" autowire="default"
         scope="prototype" dependency-check="default">
         <property name="userDao" ref="userDao" />
         <property name="user" ref="user" />
      </bean>
      <bean id="user" class="org.chenwj.vo.Userinfo" abstract="false"
         lazy-init="default" autowire="default"
           dependency-check="default">
      </bean>
  </beans>
      到此所有的配置已经完成,测试:
      HTTP Status 404 - Servlet action is not available
  The requested resource (Servlet action is not available) is not available
  这个错误是大部初学者整合 SSH 时都会遇到的问题

      首先建议你使用测试类进行测试,这样我们可以很快找到错误所在的地方
  public static void main(String[] args) ...{
      ApplicationContext context = new FileSystemXmlApplicationContext(
                "/WebRoot/WEB-INF/applicationContext.xml");
          UserDAO dao = (UserDAO)context.getBean("userDao");
         Userinfo user = new Userinfo();
         user.setName("aaa");
         user.setPassword("bbb");
         boolean a = dao.regist(user);
         if(a)...{
             System.out.println("OK");
         }
     }
  如果这里没出错,那么请你好好检查你的配置文件,是否写错或少了些什么东东了
      这里出的错误也跟使用的版本有关系,这里报的错一般都是说找不到XX类所报的异常
  那么请检查lib下有没commons-pool-1.2.jar包,如没请导入,这个问题也有可能是包
  之间的****,删除这个包hibernate-annotations.jar

  六、.sql文件、.jsp文件
  create table userinfo(
         id int(10) not null auto_increment,
 name varchar(20),
 password varchar(20),
 PRIMARY KEY  (id))
     <body>
        <html:form action="/user">
            name : <html:text property="name"/><br/>
            password : <html:password property="password"/><br/>
            <html:submit/><html:cancel/>
        </html:form>
     </body>

posted on 2008-06-12 15:56 飞飞 阅读(548) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: