1.导入E:/proGTool/struts/struts-1.3.8-all/struts-1.3.8/lib目录下的包
2.要把struts的acton交给spring管理要用到spring和struts是整合的下包!
在E:/proGTool/spring/spring-framework-2.5.5-with-dependencies
/spring-framework-2.5.5/dist/modules目录下找到
spring-webmvc-struts.jar倒进lib
3.Struts通过servelt来配置入口点:
4.在应用里启动spring容器:
方法一:使用监听器,在web.xml配置:
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>shop_2</display-name>
<!-- 内容参数 --> <!-- 指定spring配置文件所在的地方 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 读取spring配置文件,对spring容器进行实例化,
会把指向spring容器的引用放入web应用的application范围 -->
<listener>
<listener-class>org.springframework.web.context.
ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
方法二:通过spring插件启动
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action path="/control/product/type/list">
<forward name="list" path="/test.jsp"/>
</action>
</action-mappings>
<!-- controller由spring提供,用于将struts请求的action交给
controller处理,控制器会根据action当前的path去寻找跟path同名的bean
-->
<controller>
<set-property property="processorClass"value="org.
springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
</struts-config>
Beans.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: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">
<!-- omponent-scan提供一个扫描机制,会在你的类路径下寻找带有spring特殊标志的类(如com.itcast包下的类),会将这些类自动纳入到spring容器中进行管理 -->
<context:component-scan base-package="com.itcast" />
<!-- property-placeholder是一个属性遍历器,定位一个属性文件,属性文件存放的是jdbc一些连接数据 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据源采用的是apache开源的dbcp数据源产品,采用依赖注入的方式设置属性值,要删除bean是调用close方法,默认在容器初始化的时候将bean创建出来 -->
<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}" />
<property name="maxIdle" value="${maxIdle}" />
<property name="minIdle" value="${minIdle}" />
</bean>
<!-- 实体管理器,由spring容器提供 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 类中有几个属性,采用依赖注入,应用上面定义的dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- 持久化配置文件persistence.xml -->
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.
classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 事务管理器,引用entityManagerFactory对象,并对它进行管理 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<!-- Activates @Transactional for DefaultImageDatabase -->
<!-- 使用注解方式来声明事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
测试:新建ProductTypeAction.java
package com.itcast.web.action.product;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;
import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
//声明这个类是属于控制层,根据action配置的path将这个bean纳入spring管理
@Controller("/control/product/type/list")
public class ProductTypeAction extends Action {
/**
* 采用注解注入有两种方式:
* 方法一:一种注解是按名称匹配原则(类的简单名称)
* 方法二:自动装配,按类型来匹配
*/
@Resource(name="productTypeServiceBean")
private ProductTypeService productTypeService;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductType productType = productTypeService.find(ProductType.class,4);
request.setAttribute("productType", productType);
return mapping.findForward("list");
}
}
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>产品列表</title>
</head>
<body>
${productType.name }
</body>
</html>