Spring 联合Hibernate ,Struts开发之基本配置

来源:互联网 发布:手机声音增大软件 编辑:程序博客网 时间:2024/04/29 00:23

Myeclipse中添加插件顺序为:
Spring -》Hibernate-》Struts。顺序一定不能变哦。

本次主要介绍哈如何把Spring ,Hibernate ,Struts三者加在一起使用。

下面就先写个简单的Spring+Hibernate示例:
当然请先新建一个项目,然后添加相应包,这些步骤就不赘述了哈
(新手注意要先加Spring,再加Hibernate哦,如果还有Struts,当然她是最后在加的)。

开始之前先告诉大家一点点和以前不同之处就是:
    在spring框架中当编写需要操作数据库的类时,只需要继承HibernateDaoSupport父类就可以
    进行增删改查了。而不再需要编写DAO了。
例如:
先写一个vo类:
package com.ssh;

public class Person {
 private int id ;
 private String name ;
 private String password ;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
}
然后就是写实现类,记住不再需要DAO类了饿,直接继承HibernateDaoSuppor就可:
package com.ssh;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PersonDAOImpl extends HibernateDaoSupport {
 public void insert(Person person)
 {
  this.getSession().save(person);
  this.getSession().beginTransaction().commit();
  this.getSession().close();
 }
}


接下来就是配置了:

大家肯定都注意到了,上面的实现类和以往不同的地方就是继承了一个HibernateDaoSuppor类,
这是肯定的。我们继续,因为继承了这个类,而要使得这个类发挥作用,在Spring中呢就还必须
得依靠HibernateTemplate的支持,所以呢,这里我们首先就的配置这个HibernateTemplate模板。
说白了其实就是通过配置使得实现类可以使用sessionFactory,以便对数据库进行操作。具体配置
如下:
 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean id="persondaoImpl" class="com.ssh.PersonDAOImpl">
  <property name="hibernateTemplate" ref="hibernateTemplate">
  </property>
 </bean>
剩下的就是创建Person.hbm.xml的Hibernate映射文件了,这一步也不用说了吧,前头搞忘了说建立数据库表person了
看到这里你该不会不知道我的person表结构吧??(参看前面的vo类:Person )。

最后写个测试类测试一下:
package com.ssh;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
 public static void main(String[] args) {
  ApplicationContext ctx = null;
  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
  PersonDAOImpl pdi = (PersonDAOImpl)ctx.getBean("persondaoImpl");
  Person per = new Person();
 
  per.setName("陈霞");
  per.setPassword("1588");
  pdi.insert(per);
 }
}
运行一下能成功插入的哦。这样一个完整的简单的操作就完成了哈。
如果你想向在Hibernate中那样显示sql语句,只需在Spring配置文件中加入如下内容:
 <prop key="hibernate.dialect">
  org.hibernate.dialect.MySQLDialect
 </prop>
 <prop key="hibernate.show_sql">true</prop>
看懂了在哪加了不???没动就再比对比对哈。

也许细心的读者注意到了 ,Struts呢 怎么不见Struts啊 哈哈  是的哈 没有忘记,下面就继续往项目中添加Struts支持,
但是此步骤跟直接添加Struts支持没啥特别的地方,所以你就直接加吧,我也懒的说了哦。
添加完Struts支持之后呢,
接下来还是继续配置哈,不过就比前边复杂的多了哦 细心点哈。

第一步,配置Spring支持;
打开项目中的web.xml文件添加Spring支持:
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
 <servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>
  org.springframework.web.context.ContextLoaderServlet
 </servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
上面配了个监听器,主要是用于监听程序出错,因为Spring是比较容易出错的,但是并不是出错到不好用那中哈。不要误会。
Struts联合Spring配置只有一种方式,但是实现有两种方式:
先是写好action:

package com.sshDemo.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ssh.Person;
import com.ssh.PersonDAOImpl;
import com.sshDemo.struts.form.PersonForm;

public class PersonAction extends DispatchAction {

 private PersonDAOImpl persondaoImpl;
 //persondaoImpl需要Spring注入到Struts中去。即是修改Struts配置文件。详情请看后边。

 public ActionForward insert(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  PersonForm personForm = (PersonForm) form;// TODO Auto-generated method stub
  Person per = new Person();
  per.setName(personForm.getName());
  per.setPassword(personForm.getPassword());
  persondaoImpl.insert(per);
  return null;
 }
 public PersonDAOImpl getPersondaoImpl() {
  return persondaoImpl;
 }
 public void setPersondaoImpl(PersonDAOImpl persondaoImpl) {
  this.persondaoImpl = persondaoImpl;
 }
}
如何把persondaoImpl通过Spring注入到Struts中去呢?
首先添加一个Struts-Plugin插件:
新建》myeclipse》web-Struts》Struts1.2 Plugin
Plugin class 选择为:org.springframework.web.struts.ContextLoaderPlugIn。
set properties设置为:
 Property:contextConfigLocation
 value:    /WEB-INF/classes/applicationContext.xml
添加完这个插件之后点完成。
好,现在讲:
第一种实现方式:
把<action
      attribute="personForm"
      input="/form/person.jsp"
      name="personForm"
      parameter="status"
      path="/person"
      scope="request"
      type="com.sshDemo.struts.action.PersonAction" />
替换成Spring中专门解决Struts集成代理问题,
<action
      attribute="personForm"
      input="/form/person.jsp"
      name="personForm"
      parameter="status"
      path="/person"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy" />
还没完,接着修改Spring的配置文件,在其中添加如下内容:
 <bean name="/person" class="org.lxh.struts.action.PersonAction">
  <property name="persondao">
   <ref bean="persondao"/>
  </property>
 </bean>
现在主要工作基本做完了。
现在建立一个带Struts标签的jsp页面,然后建立如下表单:
<html:form action="person.do" method="post">
      name:<html:text property="name"></html:text>
      password:<html:text property="password"></html:text>
      <input type="hidden" name="status" value="insert"/>
      <p><br></p>
      <html:submit value="提交"></html:submit>
     </html:form>
最后启动服务器,写入表单值,提交,成功插入数据。ok,结束。

上面介绍的这种方式呢有个问题就是需要修改action中的type值,有没有一种方法不去修改该type值,而同样达
到被Spring管理的目的呢?
答案是可以:通过DelegatingRequestProcessor 》来完成。

实现方式二:
第一步,把type修改为原来的值:com.sshDemo.struts.action.PersonAction
第二步,在Struts配置文件中添加一个处理器元素:
 <controller
  processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
 </controller>

然后在进入刚才建立的jsp表单页面进行验证,成功插入。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wzwjr/archive/2009/03/18/3999567.aspx

原创粉丝点击