struts2+hibernate+spring

1. 建立WEB工程;

2. 引入Struts2的包( 必须加入Struts2-spring-plugin-2.0.11.jar );

3. 在web.xml中配置struts过滤器;
 <filter>
  <!-- 定义核心Filter的名字 -->
  <filter-name>struts2</filter-name>
  <!-- 定义核心Filter的实现类 -->
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>
 
4. 引入spring;( 选择Spring类包的时候,必须勾上Spring 2.0 Web Libraries);
 
5. 在web.xml中配置spring监听器,并指定applicationContext.xml文件位置
 <!--
  添加了一个监听器,它根据applicationContext配置文件来初始化Spring容器
  如果没有配置,会有如下异常:
  java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware
 -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 
 <!--
  定义spring 配置文件
  如果applicationContext.xml文件在WEB-INF目录下不用此配置.
  如果没有contextConfigLocation制定配置文件,Spring会自动查找WEB-INF目录下的applicationContext.xml配置文件
 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 可以用逗号隔开配置多个文件 -->
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 

6. 编写DAO接口、DAO实现类、业务接口、业务实现类;

7. 编写Action,其中包含业务接口型属性,且有set方法;

8. 编写视图;

9. 在applicationContext.xml中配置DAO、业务类和Action
 <bean id="UserInfoDAO" class="demo.dao.UserInfoDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 
 <bean id="UserBiz" class="demo.biz.UserBiz">
  <property name="dao">
   <ref bean="UserInfoDAO" />
  </property>
 </bean>
 
 <!--
  Prototype作用域的bean会导致在每次对该bean请求时都会创建一个新的bean实例。
  根据经验,对所有有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
 -->
 <bean id="loginAction" class="demo.action.LoginAction" scope="prototype">
  <property name="userBiz">
   <ref bean="UserBiz" />
  </property>
 </bean>
 
10. struts.xml中配置action;
 主要是下面配置:
 <!-- 使用spring,将action托管给spring  -->
 <constant name="struts.objectFactory" value="spring" />
 <package name="demo1" extends="struts-default">
  <!--
   class="loginAction"这里的loginAction不再是以前的真正的类;
   因为要交给spring管理,所以这里的class取值要与 applicationContext.xml 中的
   <bean id="loginAction" class="demo.action.LoginAction" scope="prototype">
   的 id="loginAction" 保持一致
   -->
  <action name="login" class="loginAction"> <!--loginAction与applicationContext.xml中的Action的id相同-->
   <result name="success">/success.jsp</result>
   <result name="input">/login.jsp</result>
  </action>
 </package>
 
11. 记着将spring的jar部署到站点.尤其是使用Ant时,容易忘记。

12. 补充
 使用Spring容器之前,必须先完成Spring容器的初始化,为了完成Spring容器的初始化,Struts2利用了Spring所提供的两种初始化方式.
 (1)利用ContextLoaderListener
 Spring提供一个ContextLoaderListener对象,该类可以作为Web应用的Listener使用,它会在Web应用启动时自动查找WEB-INF/下的applicationContext.xml配置文件
 (Spring的配置文件),并且根据该文件来创建Spring容器.
 
 (2)采用load-on-startup Servlet创建ApplicationContext
 1.利用Listener创建Spring容器很简单,但有一个明显的局限,因为Listener是Servlet2.3以后才开始出现的规范.
 2.在低版本的Servlet中只能利用Web应用中的load-on-startup的Servlet而不能利用Listener.
 3.为了使用load-on-startup Servlet来创建Spring容器,Spring提供了一个特殊的Servlet类:ContextLoaderServlet,
   该Servlet在初始化时,会自动查找WEB-INF/下的"applicationContext.xml"文件.
 4.此种方式下,也可以使用<context-param>元素来确定多个配置文件.
 
 共同点:无论是ContextLoaderServlet还是ContextLoaderListener都是通过调用ContextLoader来创建spring容器的.

 

 

 

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <!--
  Prototype作用域的bean会导致在每次对该bean请求时都会创建一个新的bean实例。
  根据经验,对所有有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
 -->
 <bean id="loginAction" class="demo.action.LoginAction"
  scope="prototype">
  <property name="userBiz">
   <ref bean="UserBiz" />
  </property>
 </bean>

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="org.gjt.mm.mysql.Driver">
  </property>
  <property name="url"
   value="jdbc:mysql://localhost:3306/text">
  </property>
  <property name="username" value="root"></property>
  <property name="password" value="dlf"></property>
 </bean>

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.MySQLDialect
    </prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>demo/entity/User.hbm.xml</value></list>
  </property>
 </bean>
 
 <bean id="UserBiz" class="demo.biz.UserBiz">
  <property name="dao">
   <ref bean="UserDAO" />
  </property>
 </bean>
  
 <bean id="UserDAO" class="demo.dao.UserDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean></beans>

 

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>
 <!-- 使用spring,将action托管给spring  -->
 <constant name="struts.objectFactory" value="spring" />
 
 <package name="demo1" extends="struts-default">
  <!--
   class="loginAction"这里的loginAction不再是以前的真正的类;
   因为要交给spring管理,所以这里的class取值要与 applicationContext.xml 中的
   <bean id="loginAction" class="demo.action.LoginAction" scope="prototype">
   的 id="loginAction" 保持一致
   -->
   <!--loginAction与applicationContext.xml中的Action的id相同-->
  <action name="login" class="loginAction">
   <result name="success">/success.jsp</result>
   <result name="input">/login.jsp</result>
  </action>
 </package>
</struts>

 

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <!--
  添加了一个监听器,它根据applicationContext配置文件来初始化Spring容器
   如果没有配置,会有如下异常:
 java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware
 -->
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <!--
 定义spring 配置文件
 如果applicationContext.xml文件在WEB-INF目录下不用此配置.
 如果没有contextConfigLocation制定配置文件,Spring会自动查找WEB-INF目录下的applicationContext.xml配置文件
 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 可以用逗号隔开配置多个文件 -->
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>

 <!-- 定义Struts 2的FilterDispatcher的Filter -->
 <filter>
  <!-- 定义核心Filter的名字 -->
  <filter-name>struts2</filter-name>
  <!-- 定义核心Filter的实现类 -->
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>

 <!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 -->
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!--
  FilterDispatcher过滤器为了防止内存溢出,会自动的清除ActionContext。
  这可能会存在一些问题,在和其它的框架集成时,例如SiteMesh。
  ActionContextCleanUp提供了怎么处理这些问题的一些信息。
 -->
 <filter>
  <filter-name>struts-cleanup</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ActionContextCleanUp
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts-cleanup</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值