Struts、Spring、Hibernate整合
一、创建web工程,搭建Struts框架开发环境:
这里只导入了项目中所需要的重要的jar包,以后根据业务要求继续导入相关的包。
- 步骤1::导入struts框架所需的jar包
- 步骤2:在web.xml中配置struts2.0主过滤器
- 步骤3:导入struts.xml配置文件
web.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
struts.xml
二、搭建Hibernate开发环境:
创建数据库:
这里只导入了项目中所需要的重要的jar包,以后根据业务要求继续导入相关的包。
工程目录:
hibernate.cfg.xml
com.mysql.jdbc.Driver
root
root
jdbc:mysql://localhost:3306/xbmuoa
org.hibernate.dialect.MySQLDialect
update
true
true
Person.hbm.xml(暂时性先写个映射文件的模板)
三、搭建Spring开发环境:
这里只导入了项目中所需要的重要的jar包,以后根据业务要求继续导入相关的包。
applicationContext.xml
以上已经将Struts、Spring、Hibernate开发所需要的jar包及其配置文件已经配置到web工程中了。
接下来,我们两两整合在一起。
四、Spring与Hibernate整合:
让Spring来管理SessionFactory和事务。
工程目录:
applicationContext.xml
jdbcUrl = jdbc:mysql:///xbmuoa
driverClass = com.mysql.jdbc.Driver
username = root
password = root
SpringTest.java
package cn.xbmu.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//测试SessionFactory
@Test
public void testSessionFactory(){
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
}
使用Junit测试,结果:
启动tomcat没有报错,说明以上环境的搭建都完全正确,并进行测试,也没有报错。
User.java
package cn.xbmu.oa.domain;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
User.hbm.xml
记得在hibernate.cfg.xml文件中引入映射文件。
TestService.java
package cn.xbmu.oa.test;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.xbmu.oa.domain.User;
@Service("testService")
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUser(){
Session currentSession = sessionFactory.getCurrentSession();
currentSession.save(new User());
int i = 1 / 0;//这里会抛出异常
currentSession.save(new User());
}
}
SpringTest.java
package cn.xbmu.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//测试SessionFactory
@Test
public void testSessionFactory(){
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
//测试事务
@Test
public void testTransaction(){
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUser();
}
}
测试事务,报错,因为在 int i = 1 / 0这里发生了异常。
查询数据库xbmuoa中的表,没有数据
我们注释掉 int i = 1 / 0;这句代码,重新测试,成功。
查询user表,里面已经有了保存的数据
我们,继续打开抛异常的语句,再测试;再注释抛异常的代码,再测试。查看表中的数据,发现了事务回滚了。
到此为止,我们顺利的将Spring与Hibernate整合在一起了。并测试了SessionFactory与事务,都正常。
五、Spring与Struts整合:
为了说明白,我们在整合之前写个Action,整合之后写个Action,看看他们之间的区别:
整合之前:
TestAction.java
package cn.xbmu.oa.test;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("---------->TestAction execute()");
return "success";
}
}
struts.xml
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
Struts2.0添加成功
运行:
整合之后:
导入jar包,已经完成了整合
TestAction.java
package cn.xbmu.oa.test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("---------->TestAction execute()");
return "success";
}
}
struts.xml
/test.jsp
在浏览器上运行:http://localhost:8080/xbmuoa/test.action
发现,在控制台上报错,信息如下:
严重: ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
org.springframework.web.context.ContextLoaderListener
四月 03, 2016 9:48:27 下午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Dispatcher initialization failed
java.lang.NullPointerException
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:209)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:519)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:490)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:446)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5542)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
四月 03, 2016 9:48:27 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts2
Class: com.opensymphony.xwork2.spring.SpringObjectFactory
File: SpringObjectFactory.java
Method: getClassInstance
Line: 209 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:209:-1
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4854)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5542)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:209)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:519)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:490)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:446)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 18 more
意思就是,没有和web工程整合,没有一个全局的工厂容器对象。
在web.xml中配置信息如下:
web.xml
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext*.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
TestAction.java
package cn.xbmu.oa.test;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("---------->TestAction execute()");
testService.saveTwoUser();
return "success";
}
}
在浏览器中运行,报错,是因为在 int i = 1 / 0出抛出了异常
注释掉这句抛异常的语句,继续运行,成功。
以上关于SSH的整合已经成功介绍完了,所需代码下载地址: http://download.csdn.net/detail/btt2013/9480447