第一篇 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包
分析:这里我们使用的是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值,说明以上我们的工作是正确的!
注:这里要先部署一下tomcat :在tomcat路径下“ F:/Program Files/Apache Software Foundation/Tomcat 5.5/conf/Catalina/localhost”新建一个SSHProject.xml文件,里面加入如下代码:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:/workspace/SSHProject/WebRoot"
path="/SSHProject" reloadable="true"
workDir="D:/workspace/SSHProject/work/org/apache/jsp">
</Context>
第四步 配置stuts-config.xml和applicationContext.xml文件
看到这里,大家可能会认为,这和以前的web工程的创建没有什么两样,和struts与spring融合没有什么关系,不用着急,奥妙就在sturts-config.xml与applicationContext.xml文件配置中。
1、配置stuts-config.xml文件