第一篇 struts与spring的融合

本文介绍了如何在Java环境中将Struts与Spring进行融合,包括环境配置、新建工程、创建Action和Bean、配置struts-config.xml和applicationContext.xml文件。通过实际操作,展示了Struts1.1与Spring2.0的集成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Struts+Spring+Hibernate的完美融合之struts与spring的融合

第一篇 struts与spring的融合

第一步:配置环境与技术支持

1、环境:tomcat5.0 + eclipse3.2.2 + myEclipse5.5 + jdk1.5

2、技术:struts1.1+spring2.0

分析:经过多次实验,(初建struts+spring)项目中出现的问题和工具及技术版本没有根本关系,只要在(其他项目运行)已经配置成功的环境下运行就好。这里要注意的是:myEclipse5.0以下的版本不支持spring2.0。小小提示:本人初次在该环境下操作时,多次不成功,最后从新安装配置环境后,struts+spring项目才正常运行,疑与myEclipse有关。

第二步:新建工程SSHProject

1、新建工程

分析:不同版本的eclipse新建项目对话框不同,3.2.2以下版本没有java EE 5.0。这里我们选择J2EE1.4!

2、导入struts包

 

3、导入spring包

导入spring 包

分析:这里我们使用的是spring2.0,如果你的版本不支持2.0,就使用spring1.2版本。spring1.2与struts1.1同样兼容,但spring1.2只支持hibernate3.0以下的版本。如果你选择spring1.2,就不得不使用hibernate3.0或者2.1。小小提示:对于初学者来说,最好把所有的spring包导入项目。提醒:applicationContext.xml我们放在src下,但我们要知道编译部署后,默认在classes文件夹,所以我们在以后的配置中,一定要注意路径问题。

3、新建com.ssh.beans.po和com.ssh.beans.dao两个包已做备用。

好了,工程框架已经搭好,现在我们就可以往里面放东西了。

第三步 创建action和bean

1、在com.ssh.beans.po中创建Customer.java。内容如下:

package com.ssh.beans.po;

public class Customer {
 
 String custId;
 String custName;
 
 public Customer(){
  
 }
 
 public Customer(String custId,String custName){
  this.custId=custId;
  this.custName=custName;
 }
 
 public void setCustId(String custId){
  this.custId=custId;
 }
 
 public String getCustId(){
  return this.custId;
 }
 
 public void setCustName(String custName){
  this.custName=custName;
 }
 
 public String getCustName(){
  return this.custName;
 }
}

2、在com.ssh.beans.dao中创建CustomerDAO.java及其接口ICustomerDAO.java。内容如下:

package com.ssh.beans.dao;

import java.util.ArrayList;
import java.util.List;

import com.ssh.beans.po.Customer;

public class CustomerDAO implements ICustomerDAO {
public List getALLCustomer(){
 List list=new ArrayList();
 Customer c1=new Customer("1","zhang");
 Customer c2=new Customer("2","xiaoling");
 list.add(c1);
 list.add(c2);
 return list;
}
}

 

package com.ssh.beans.dao;

import java.util.List;

public interface ICustomerDAO {
 public List getALLCustomer();
}

 3、创建CustomerAction.java

分析:这里action的创建与我们以前创建action一样,但我们要注意的是,你导入什么版本的struts就要生成什么版本的action,这里是struts1.1。

接下来我们看看struts-config.xml里面的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards />


  <action-mappings >
    <action path="/customer" type="com.ssh.struts.action.CustomerAction">
      <forward name="success" path="/index.jsp" />
    </action>

</action-mappings>

  <message-resources parameter="com.ssh.struts.ApplicationResources" />
</struts-config>

没错,内容和我们以前的东东一样。

我们再给CustomerAction.java一些内容:

package com.ssh.struts.action;

import java.util.ArrayList;
import java.util.List;

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.ssh.beans.dao.CustomerDAO;
import com.ssh.beans.dao.ICustomerDAO;
import com.ssh.beans.po.Customer;

public class CustomerAction extends Action {
 
 ICustomerDAO customerDAO=null;
 public void setCustomerDAO(ICustomerDAO customerDAO){
  this.customerDAO=customerDAO;
 }
 
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  List list=new ArrayList();
  Customer customer=null;
  setCustomerDAO(new CustomerDAO());
  if(customerDAO!=null){
   list=customerDAO.getALLCustomer();
   for(int i=0;i<list.size();i++){
    customer=(Customer)list.get(i);
    System.out.println("OK:"+customer.getCustName());
   }
  }else{
   System.out.println("ERROR or NULL");
  }
  return mapping.findForward("success");
 }
}

 

好的,我们现在测试一下!如果访问http://localhost:8080/SSHProject/customer.do能顺利进入index.jsp页面,并输出用户custName值,说明以上我们的工作是正确的!

第四步 配置stuts-config.xml和applicationContext.xml文件

看到这里,大家可能会认为,这和以前的web工程的创建没有什么两样,和struts与spring融合没有什么关系,不用着急,奥妙就在sturts-config.xml与applicationContext.xml文件配置中。

1、配置stuts-config.xml文件

在这里,我们要做两个工作:第一,将CustomerAction替换成DelegatingActionProxy代理。第二,添加代理插件ContextLoaderPlugIn。修改后的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards />
 
  <action-mappings >
    <action path="/customer"  type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="success" path="/index.jsp" />
    </action>
  </action-mappings>

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

注:粗体字为修改的内容

分析:你一定要做到:1、确保spring-struts.jar导入项目。2、保证applicationContext.xml文件路径正确。我们在导入spring包时就提到,applicationContext.xml文件放在src下,编译部署后,文件默认存放在WEB-INF/classes下。所以上面的配置为:value="/WEB-INF/classes/applicationContext.xml"/>

2、配置applicationContext.xml

在这个文件中,我们的任务是加入我们需要的bean。到目前为止,我们要加入两个bean,即:CustomerDAO与CustomerAction。修改后的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 
 <bean name="/customer" class="com.ssh.struts.action.CustomerAction" >
      <property name="customerDAO"><ref bean="customerDAO"/></property>
 </bean>   
 <bean name="customerDAO" class="com.ssh.beans.dao.CustomerDAO" />

</beans>

注:粗体字为添加的内容

分析:在这个文件中你要确保:第一,CustomerAction bean中<bean>标签的属性name值(name="/customer")一定与struts-config.xml中属性path的值(path="/customer")一致。第二、<property>标签的属性name值在CustomerAction中一定有对应的属性。第三、<ref>标签的属性bean值一定与CustomerDAO bean的<bean>标签的属性name值一致(注:这就是注入目标对象)。第四、CustomerDAO bean中<bean>标签的属性class值一定是一个实现类(不能为接口)。

3、修改CustomerAction.java

这一步很简单,只要把setCustomerDAO(new CustomerDAO());这条语句去掉就好。因为我们已经在applicationContext.xml给customerDAO进行了setter注入。(呵呵,至于什么是setter注入,不了解的朋友先看看spring的IOC)。

好的,到现在为止,我们已经对struts和spring进行了融合。再来测试一下,如果输入同样的内容,就OK!

如果以上操作大家都没有问题,我们就向下看。来吧,一起融合spring与hibernate!

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值