struts2和hibernate的配置我这里就不多说了,先把两个有关的东西说下。一个是所有的包。struts2+hibernate3+spring2.5我包准备放上去给大家下载。
http://pan.baidu.com/share/link?shareid=3160001548&uk=3307409781
放在的我百度云盘里面
闲话不说:开始。
先是struts2和hibernate的配置
web.xml:
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app version=
"2.5"
xmlns=
"http://java.sun.com/xml/ns/javaee"
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">
<!-- structs2的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-
class
>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-
class
>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|
然后在src下添加一个struts.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> </struts>
然后在src下添加一个hibernate.cfg.xml:(这是一些数据库的配置)
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">sa</property> <property name="connection.url"> jdbc:jtds:sqlserver://localhost:1433;DatabaseName=LQQ </property> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <!--<property name="myeclipse.connection.profile"> LoginSystem </property> --> <property name="connection.password">sa</property> <property name="connection.driver_class"> net.sourceforge.jtds.jdbc.Driver </property> <property name="show_sql">true</property> <!-- POJO 类映射配置--> <mapping resource="sedion/xq/ORM/Stuinfo.hbm.xml" /> </session-factory> </hibernate-configuration>
重点讲Spring:
1. 在src下面新建applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 这是个例子待会会用到 可以先不要 --> <bean id="TUser_id" class="test.TUser"> <property name="username" value="你"></property> <property name="allname" value="李"></property> <property name="address" value="温州市"></property> </bean> </beans>
然后在web-xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<!--spring 的配置文件加载路径-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
|
2.Spring基本配置完毕
下面我们测试下:src建个test包
建两个类User和SpringTest类:
public class User implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private String username; private String allname; private String address; public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getAllname() { return this.allname; } public void setAllname(String allname) { this.allname = allname; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; }
public
class
SpringTest {
public
static
void
main(String[] args) {
// 加载spring配置文件,初始化IoC容器
ApplicationContext ac =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
// 从容器 接管Bean
TUser user = (TUser) ac.getBean(
"TUser_id"
);
// 输出欢迎信息
System.out.println(
"Hello:"
+ user.getUsername() +
";u is in "
+ user.getAddress() +
" ; and u is "
+ user.getAllname());
}
}
<br><br>
|
别忘了:在applicationContext.xml中配置一个bean,在xml中增加如下代码:
<!-- 这是个例子待会会用到 可以先不要 -->
<bean id=
"TUser_id"
class
=
"test.TUser"
>
<property name=
"username"
value=
"你"
></property>
<property name=
"allname"
value=
"李"
></property>
<property name=
"address"
value=
"温州市"
></property>
</bean>
|
运行main方法后
控制台出现:Hello:你;u is in 温州市 ; and u is 李
下面开始整合
整合struts2
1.整合struts2配置web.xml文件。在web.xml中加入以下代码:
<!-- Spring与Struts的整合其实就是把Struts的Action类交给Spring来管理 -->
<listener>
<listener-
class
>
org.springframework.web.context.ContextLoaderListener
</listener-
class
>
</listener>
|
2.举个例子:
把struts的action交给spring:
先把
<action name=
"login"
class
=
"test.LoginAction "
>
|
改为
<action name="login" class="loginAction_id">
然后在
在applicationContext.xml中增加如下代码:
<bean id=
"loginAction_id"
class
=
"test.action.LoginAction"
scope=
"prototype"
>
</bean>
|
整合Hibernate
Spring整合Hibernate主要是对hibernate的Session进行管理,包含Session的创建、提交、关闭的整个生命周期。Spring对事务的管理应用了AOP的技术
1) 配置sessionFactory,让spring来创建Session。
我们原来是用HibernateSessionFactory.java来创建Session的,现在删除即可,交给Spring创建。(这样就spring来整合hibernate来创建session)
这里,创建了一个Session工厂类的Bean,其ID为“sessionFactory”.
在applicationContext.xml中增加如下代码:
<!-- 配置sessionFactory -->
<bean id=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<property name=
"configLocation"
>
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
|
还有个例子上的配置:
<bean id=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<property name=
"dataSource"
ref=
"dataSource"
></property>
<property name=
"hibernateProperties"
>
<props>
<prop key=
"hibernate.dialect"
>org.hibernate.dialect.SQLServerDialect</prop>
<prop key=
"hibernate.show_sql"
>
true
</prop>
</props>
</property>
<property name=
"mappingResources"
>
<list>
<value>sedion/xq/bean/User.hbm.xml</value><!--这里是将映射文件加载-->
</list>
</property>
</bean>
|
2.
然后DAO层匹配一个session工厂,<ref bean="sessionFactory"/>这个sessionFactory是指session工厂的ID。
<bean id=
"userDao"
class
=
"sedion.xq.dao.iml.UserDAOImpl"
scope=
"singleton"
>
<property name=
"sessionFactory"
>
<ref bean=
"sessionFactory"
/>
</property>
</bean>
|
3.service层注入DAO层
<bean id=
"userService"
class
=
"sedion.xq.sevice.iml.UserServiceImpl"
>
<property name=
"userDAO"
ref=
"userDao"
></property>
</bean>
|
然后就是各个action注入service层:(随便写个)
<bean id=
"saveUesrAction"
class
=
"sedion.xq.action.user.SaveUserAction"
scope=
"prototype"
>
<property name=
"userService"
ref=
"userService"
></property>
</bean>