Spring框架+Struts2框架第一次整合

时间:2023-03-09 09:13:28
Spring框架+Struts2框架第一次整合

1:Spring框架和Struts2框架如何整合???

  Spring 负责对象创建

  Struts2 用Action处理请求

2:Spring与Struts2框架整合的关键点:

  让struts2框架action对象的创建,交给Spring完成

3:Spring框架和Struts2框架开发步骤:

  (1):引入Struts2框架的相关jar包

  (2):引入Spring框架的相关jar包

  (3):引入spring-web支持的jar包

      spring-web-3.2.5.RELEASE.jar    【去spring的lib里面找即可】
      struts2-spring-plugin-2.3.4.1.jar 【去struts2的lib里面找即可】

4:配置XML
   (1):struts.xml配置  【struts2路径与action映射配置】

      易错点:注意action的class属性是直接使用spring的IoC容器里面创建的userAction的名称即可。千万别再使用com....

 <?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>
<!-- 创建包 -->
<package name="struts-spring" extends="struts-default">
<action name="user" class="userAction">
<result name="success">success.jsp</result>
</action> </package> </struts>

    (2):applicationContext.xml/bean.xml配置 【spring IoC容器配置】

 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 -->
<bean id="userDao" class="com.bie.dao.UserDao"></bean> </beans>
 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 --> <bean id="userService" class="com.bie.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 --> <bean id="userAction" class="com.bie.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>

  (3):web.xml配置  【一:核心过滤器,引入struts2功能,二:初始化spring IoC容器】 

       web.xml的配置真的很重要,也很容易出错:

       易错点:初始化spring IoC容器的时候param-value的值一定注意路径,不然一直报404~~~

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring_Struts2_20170313</display-name>
<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.xml分两部分进行配置 -->
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 如何找到这块,ctrl+shift+t 搜索StrutsPrepareAndExecuteFilter -->
<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> <!-- 1:spring配置 ,在spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle
搜索context-param找到下面这段话即可。记得就该param-value的值,如下所示;
2:param-value的值最好使用bean,这样方便引用如/WEB-INF/classes/bean-*.xml
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

5:配置好配置文件,基本算是完成了开始准备的工作,下面可以进行开发了,这里简单写了一个例子,如下所示:

  分别实现了dao层,service层,action层,记住都是使用Spring的IoC容器进行初始化对象的。

 package com.bie.dao;
/**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:44:27
*
*/
public class UserDao { public void daoTest(){
System.out.println("struts-spring整合的第一个项目");
}
}
 package com.bie.service;

 import com.bie.dao.UserDao;

 /**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:44:37
*
*/
public class UserService { private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void serviceTest(){
userDao.daoTest();
}
}
 package com.bie.action;

 import com.bie.service.UserService;
import com.opensymphony.xwork2.ActionSupport; /**
* @author BieHongLi
* @version 创建时间:2017年3月13日 下午1:43:42
*
*/
public class UserAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
} @Override
public String execute() throws Exception {
userService.serviceTest();
return SUCCESS;
} }

6:最后写一个简单的成功页面:

 <%@ 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=UTF-8">
<title>struts2-spring第一次整合</title>
</head>
<body> <h1>struts2-spring第一次整合</h1> </body>
</html>

效果如下所示:

Spring框架+Struts2框架第一次整合

学会拆分,学会整合,开发一定要保持清醒的大脑和逻辑性,加油~~~