1,创建一个maven工程,在选择Archetype时选择webapp:
2,下一步配置maven的pom.xml文件,获取依赖的jar包:
org.apache.struts
struts2-core
2.3.1.2
org.apache.struts
struts2-spring-plugin
2.3.1.2
org.apache.struts
struts2-json-plugin
2.3.1.2
org.hibernate
hibernate-core
3.6.10.Final
org.aspectj
aspectjweaver
1.7.3
cglib
cglib
2.2
org.slf4j
slf4j-api
1.7.5
compile
org.springframework
spring
2.5.6
jar
org.springframework
spring-webmvc
3.2.3.RELEASE
jar
compile
log4j
log4j
1.2.16
compile
javax.servlet.jsp
jsp-api
2.1
provided
c3p0
c3p0
0.9.1.2
javax.servlet
servlet-api
2.5
provided
mysql
mysql-connector-java
5.1.26
compile
如果没使用maven,eclipse默认会去bin目录找class文件,如果使用了maven,则会去target\test-classes目录下去找class文件。刚好我的打包脚本中包含了mvn clean命令,将target\test-classes目录下的文件清空了,在target\test-classes目录还没有对应的class文件,所以会抛ClassNotFoundException!Project-clean操作让eclipse重新编译,刚好可以解决这个问题。
3,在src下创建目录webapp/WEB-INF/,并在WEB-INF下创建web.xml文件。
一,spring环境搭建+测试
在web.xml文件中添加spring的启动监听器ContextLoaderListener
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
这样在web容器启动的时候,会自动装配Spring的位于/WEB-INF/下的applicationContext.xml配置文件一般情况下,由于需要配置的bean较多,所以都是将bean按业务分类,在不同的配置文件中进行配置。然后在applicationContext.xml文件中导入分类的配置文件的。
但是现在测试的时候会直接在applicationContext.xml文件下进行bean配置:
测试:
@Test
public void testSpringEnv(){
//加载Spring的配置文件,得到ApplicationContext对象
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
//获取bean对象
Person person = (Person) context.getBean("person");
System.out.println(person.getAge());
}
二,Spring和Hibernate的整合+测试
SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作。
在Hibernate的核心配置文件hibernate.cfg.xml中配置:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
123456
root
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/eclipseweb
org.hibernate.dialect.MySQLDialect
true
update
true
在Hibernate3.6版本中的dtd文有变化,改成上面的,不然会提示错误。
编写Person的映射文件Person.hbm.xml(位于和Person类相同的目录下)。配置实体类和数据库表的映射关系。
为防止该配置文件找不到,最好将该文件复制到src/main/resources下
src/main/resources/us/xuhang/project/domain/
测试:
@Test
public void testHibernateEnv(){
//加载指定目录下的配置文件,得到configuration对象
Configuration cfg = new Configuration().configure("hibernate/hibernate.cfg.xml");
//根据configuration对象得到session工厂对象
SessionFactory factory = cfg.buildSessionFactory();
//使用工厂类打开一个session
Session session = factory.openSession();
//开启事务
Transaction tx = session.beginTransaction();
//创建待插入数据库的对象
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Person p = (Person) context.getBean("person");
p.setPid(UUID.randomUUID().toString());
p.setName("xxx");
//保存对象
session.save(p);
//提交事务
tx.commit();
//关闭资源
session.close();
factory.close();
}
以上是Hibernate自己管理SessionFactory的创建,下面将SessionFactory的创建过程交给Spring容器:
如果关于数据库的连接信息等仍然在Hibernate的配置文件中配置,即由Hibernate管理数据库的连接,则只需要在Spring的配置文件中配置SessionFactory的bean时注入configLocation,值为Hibernate的配置文件路径:classpath:hibernate/hibernate.cfg.xml
classpath:hibernate/hibernate.cfg.xml
还有一种方法是有Spring来管理数据库的连接,这里使用c3p0来帮助管理数据源
classpath:us/xuhang/project/domain
org.hibernate.dialect.MySQL5InnoDBDialect
true
update
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
将SessionFactory对象的创建交给Spring容器来管理,在Spring的配置文件中配置SessionFactory的bean。
如果SessionFactory交给Hibernate管理,可以直接在Hibernate的配置文件hibernate.cfg.xml中使用配置数据库的连接信息,然后在Spring的配置文件中导入Hibernate的配置文件:
classpath:hibernate/hibernate.cfg.xml
同时在中加入持久化类和数据库表的映射文件路径:
如果SessionFactory交给Spring容器管理,其中在Spring的配置文件中给SessionFactory注入数据源DataSource,数据源可以使用JDBC数据源,也可以使用第三方的C3P0数据源管理。
如果使用JDBC数据源,只需将DataSource的bean的class属性值改为org.springframework.jdbc.datasource.DriverManagerDataSource,然后给该类注入相应的属性。
如果使用C3P0数据源,则将DataSource的bean的class属性值改为对应的类com.mchange.v2.c3p0.ComboPooledDataSource,然后注入该类的相应的属性。
之后将数据源注入到SessionFactory中,同时加入映射文件的路径和数据库的方言等配置
classpath:us/xuhang/project/domain
org.hibernate.dialect.MySQL5InnoDBDialect
true
update
三,Spring和Struts2的整合
在web.xml中加入struts2的核心过滤器:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
struts的配置文件struts.xml需放在classpath路径下,然后按照业务不同分类配置struts,然后再在struts中include:
struts/struts-person.xml:
index.jsp
spring的配置文件中管理action的bean:
struts和Spring整合之后就会发现struts的映射文件中action的class值应为Spring中相应action的id。
由于struts的action是多例的,所以在spring容器管理action对象创建的时候需要指明一下,设置action的scope属性值为prototype。
SSH零配置整合
web.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring/applicationContext.xml
hibernate Session 过滤器
openSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSessionInViewFilter
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
applicationContext.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
proxy-target-class="true" />
expression="execution(* us.xuhang.project.service.*.*(..))" />
id="myMethod" />
struts.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
class="us.xuhang.project.interceptor.AuthorityInterceptor" />
class="us.xuhang.project.interceptor.ExceptionInterceptor" />
.*\\u0023.*
/index.jsp
/error.jsp
hibernate.cfg.xml:
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
${hibernate.temp.use_jdbc_metadata_defaults}
${hibernate.hbm2ddl.auto}
${hibernate.cache.provider_class}
${hibernate.cache.use_query_cache}
${hibernate.cache.use_second_level_cache}
false
thread
hibernate.properties:
## hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.temp.use_jdbc_metadata_defaults=false
## hibernate cache
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true
## C3P0 configuration
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.initialPoolSize=1
hibernate.connection.minPoolSize=1
hibernate.connection.maxPoolSize=3