1.Spring2.5+Hibernate3.3+struts2整合
1.首先引入jar文件:
2.工程的目录结构:
![](https://img.haomeiwen.com/i9857208/e1a9ef8340482e8d.png)
3.spring的beans.xml文件的配置(这里面就包含了对hibernate的配置,将hibernate的sessionFactory交给了spring去管理):
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--基于注解方式声明切面 -->
<!-- <aop:aspectj-autoproxy/> -->
<!--配置自动扫描 -->
<context:component-scan base-package="com.gaoyuan"/>
<!--当连接数据库的信息写在properties文件中时,配置下面的标签,也就是官方说法,配置占位符 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}"/>
</bean>
<!-- 把hibernate中的sessionFactory交给spring去管理-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/><!--数据源的用的是上面配置的dataSource -->
<property name="mappingResources">
<list>
<!--这里面存放hibernate中定义实体对应的xml -->
<value>com/gaoyuan/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<!--配置hibernate的方言-->
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
<!--配置hibernate的二级缓存-->
<!--说明是否使用hibernate的二级缓存-->
hibernate.cache.use_second_level_cache=true
<!-- 是否开启查询缓存 -->
hibernate.cache.use_query_cache=false
<!-- 缓存产品的驱动类 -->
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<!-- 配置spring专门为hibernate提供的事务管理org.springframework.orm.hibernate3.HibernateTransactionManager这个类时针对hibernate的-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--打开注解的支持,然后在程序中就可以用注解的方式配置事务,transaction-manager属性的意思配置事务管理器,将上面配置的txManager事务管理器交给它。 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personList" class="com.gaoyuan.action.PersonAction"></bean>
</beans>
4.struts的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>
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 说明,下面定义的action是由spring来负责创建的,这是spring与struts整合的一个关键配置。 -->
<constant name="struts.objectFactory" value="spring" />
<package name="person" namespace="/" extends="struts-default">
<!--定义了一个全局的信息 -->
<global-results>
<result name="message">/WEB-INF/page/message.jsp</result>
</global-results>
<!--解读一下:当地址栏访问的时候,例如写的:http://localhost:8080/项目名/namespace的名称/action_XXX.do
其实在后面配置method="{1}",表示访问的方法名是name="action_*"中的第一个星号的名称。
-->
<action name="action_*" class="personList" method="{1}">
<result name="list">/WEB-INF/page/personlist.jsp</result>
</action>
</package>
</struts>
5.上面的几个xml文件都配置好之后,需要将上配置的配置信息告诉服务器,因此现在就开始配置web.xml文件,让服务器在启动的时候,就将spring容器该加载的内容都加载上,还有对struts的一个拦截,将这三个框架融合到一起。Web.xml文件的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SSH2</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value><!--配置读取beans.xml -->
</context-param>
<!-- 在服务器启动时就对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring中提供的解决乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring提供的解决,hibernate中因session关闭导致的延迟加载例外问题 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
6.编写PersonServiceimpl.java,PersonService.java和Person.java,person.hbm.xml都和spring和hibernate整合时写的一样,此处省略;
7.编写action
package com.gaoyuan.action;
import java.util.List;
import javax.annotation.Resource;
import com.gaoyuan.bean.Person;
import com.gaoyuan.service.PersonService;
public class PersonAction {
@Resource
private PersonService personService;
private String message;
private List<Person> persons;
//编写的list方法
public String list(){
this.persons = personService.getPersons();
return "list";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
8.编写return之后的jsp页面
![](https://img.haomeiwen.com/i9857208/40afb41a4ed46acd.png)
网友评论