第一篇 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值,说明以上我们的工作是正确的!
第四步 配置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
分享到:
相关推荐
Struts+Spring融合工程实例 纯手工打造,绝对经典入门.不使用Myeclipse自动加载.
java web 网上订餐 hibernate struts spring
Struts+Spring+Hibernate的完美融合BlogJava.mht
使用struts2+spring方式开发,就必须要struts2-spring-plugin-2.1.6.jar这个包,因为它是两者融合的插件,如果没有肯定不会出错
一个三种框架融合的实列,动态action 动态form spring依赖注入,sql server2000数据库,很简单的一个学习实例。自己做的
全教程中融合了每个框架的核心知识,单独讲解了struts,spring hibernate框架,最后还系统的分析了三个框架之间的关系,以及如果运用,对大部分人适用!!!
这是一个融合了SSH技术的精典网站,和大家共享
实例内包含spring2.5+hibernate3+struts2三大框架的技术融合开发以及数据库数据
您肯定已经听说过控制反转(IOC) 设计模式,因为很长一段时间以来一直在流传关于它的信息。如果您在任何功能 ...在本文中,我利用这一原理把一个Struts 应用程序注入Spring 框 架,您将亲身体会到IOC 模式的强大。
SpringMVC精品资源--基于maven的多框架和多视图融合技术(Struts1、Struts2、Spring、
本书是《轻量级J2EE企业应用实战》的第二版,同时还融合了《整合Struts+Hibernate+Spring应用开发详解》理论部分。实际上,本书凝结了前两本书的精华部分。 本书介绍了Java EE领域的三个开源框架:Struts 2、Spring...
Hibernate部分用到了hibernateSynchronizer来生成持久层的代码,Struts还是版本1的(没融合webwork的版本),用到了jstl和displaytag标签库。后台数据库是MySQL(工程文件里附上了建表的sql文件),开发工具是...
详细介绍了java web开发的流行框架SSH的融合过程,文章简单易懂,值得一看
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是...
里面融合了spring+struts+ibatis用到的配置文件模板,这样我们就可以不用到处去找了。。
该源码包融合了多种主流技术,包括JSP、Struts、Hibernate、Spring和Oracle数据库,以提供高效、稳定且易于扩展的车辆管理服务。 在技术架构方面,该系统采用MVC设计模式,实现了业务逻辑、数据访问与界面展示的...
最新版struts-2.3.20+hibernate-4.3.8+spring-4.1.4集成,运行时需要修改db.properties里面的用户名和密码以及数据库名称
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是...
Maven框架项目该项目不再更新维护,请移步该项目基于maven3.0构建的,项目中融合了Struts1 , Struts2 , Spring , SpringMVC , Hibernate , Ibatis , MyBatis , Spring Data JPA , Spring JDBC , Spring DWR...
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是...