ssh整合之五struts和spring整合
1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了
另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么保证我们的spring容器能创建呢?
我们可以配合监听器来创建我们的spring容器,然后我们怎么实现我们的监听器呢?
当ServletContext创建成功,就说明tomcat正常启动了,我们使用监听器监听我们的ServletContext,如果创建成功,加载配置文件,创建spring容器
2.我们先在我们的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>ssh_spring</display-name> <!-- 配置spring的监听器 加载spring的配置文件,根据配置文件创建我们的spring对象 默认加载的spring配置文件位置:WEB-INF下的applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 修改spring默认加载的配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>ssh</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>ssh</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>
3. 在我们的struts中根据容器获取对象
第一种是使用struts的 插件包(这种方式使用的比较少)
第二种是把struts的动作类交给spring进行管理
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?> <!-- spring的配置文件:导入约束 --> <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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 自定义的java对象交给spring进行管理 --> <bean id="customerAction" scope="prototype" class="com.itheima.action.CustomerAction"> <property name="customerService" ref="customerService"></property> </bean> <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"></property> </bean> <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 第三方的jar包中的java对象交给spring进行管理 --> <!-- 创建一个hibernateTemplate对象 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 创建sessionFactory对象 --> <!-- 当spring和hibernate整合时,这个sessionFactory的实现类是由spring提供的:LocalSessionFactoryBean, 创建sessionFactory是根据hibernate的核心配置文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!--配置hibernate的事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务管理通知 --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置事务的AOP --> <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
在struts.xml中配置
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="customer" extends="struts-default" namespace="/customer"> <!-- 1.class: 全限定类名 反射的请示创建动作类对象 class:需要修改为容器中动作类的bean的唯一标识 --> <action name="addCustomerUI" class="customerAction" method="addCustomerUI"> <result name="success">/jsp/customer/add.jsp</result> </action> <action name="getAllCustomer" class="customerAction" method="getAllCustomer"> <result name="success">/jsp/customer/list.jsp</result> </action> </package> </struts>
我们在CustomerAction中
package com.itheima.action; import java.util.List; import com.itheima.entity.Customer; import com.itheima.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { private Customer customer = new Customer(); private CustomerService customerService; private List<Customer> customers; public void setCustomers(List<Customer> customers) { this.customers = customers; } public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public List<Customer> getCustomers() { return customers; } public Customer getModel() { return customer; } //进入添加页面 public String addCustomerUI(){ return this.SUCCESS; } //进入列表页面 public String getAllCustomer(){ customers = customerService.getAllCustomer(); return this.SUCCESS; } }
最后.测试我们的结果,访问http://localhost:8080/ssh_spring/
每天进步一点点,今天就到这儿吧!