Spring与Struts框架整合
Struts,用Action处理请求
Hibernate,操作数据库
Spring,负责对象创建
Spring与Struts框架整合的关键点在与:让Struts框架action对象的创建,交给Spring来完成
整合步骤:
1.引入jar文件
- 引入struts相关jar文件
- 引入spring-core相关jar文件
- spring-web支持jar包
spring-web-3.2.5.RELEASE.jar 【Spring源码】
struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
2.dao/service/action代码
dao代码:
public class UserDao {
public void save() {
System.out.println("DB:保存用户");
}
}
service代码:
public class UserService {
//IOC容器注入
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
action代码:
public class UserAction extends ActionSupport {
// springIOC容器注入
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
userService.save();
return SUCCESS;
}
}
3.配置xml
Struts.xml:struts路径与action映射配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" extends="struts-default">
<action name="user" class="userAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
bean.xml:spring ioc容器配置
三层bean.xml分开写:
bean-dao.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.xsd">
<bean id="userDao" class="dao.UserDao"/>
</beans>
bean-service.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.xsd">
<bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
bean-action.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.xsd">
<bean id="userAction" class="action.UserAction">
<property name="userService" ref="userService"/>
</bean>
</beans>
web.xml:要实现两个功能:核心过滤器: 引入struts功能以及初始化spring的ioc容器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--1.struts配置-->
<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>
<!--2.Spring配置-->
<!--以下通过查询源码获得-->
<context-param>
<!--固定值-->
<param-name>contextConfigLocation</param-name>
<!--spring配置文件的位置-->
<param-value>/WEB-INF/classes/bean*.xml</param-value>
</context-param>
<!--实现以上功能的过滤器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
配置项目,运行即可