现在的位置: 首页 > 综合 > 正文

Struts+Spring+Hibernate搭建过程顺序详解

2013年01月03日 ⁄ 综合 ⁄ 共 4045字 ⁄ 字号 评论关闭

第一篇 strutsspring的融合

第一:配置境与技支持

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

2struts

3spring

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

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

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

第三 actionbean

1、在com.ssh.beans.poCustomer.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.daoCustomerDAO.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();
}

3CustomerAction.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.doindex.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.xmlapplicationContext.xml文件

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

1、配置stuts-config.xml文件

抱歉!评论已关闭.