zoukankan      html  css  js  c++  java
  • 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....

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4     "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 
     6 <struts>
     7     <!-- 创建包 -->
     8     <package name="struts-spring" extends="struts-default">
     9         <action name="user" class="userAction">
    10             <result name="success">success.jsp</result>
    11         </action>
    12     
    13     </package>
    14    
    15 
    16 </struts>

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

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/context
    10         http://www.springframework.org/schema/context/spring-context.xsd">
    11      
    12      
    13      <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 -->
    14      <bean id="userDao" class="com.bie.dao.UserDao"></bean>
    15      
    16 </beans>     
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/context
    10         http://www.springframework.org/schema/context/spring-context.xsd">
    11      
    12      
    13      <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 -->
    14      
    15      <bean id="userService" class="com.bie.service.UserService">
    16          <property name="userDao" ref="userDao"></property>
    17      </bean>
    18 </beans>     
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/context
    10         http://www.springframework.org/schema/context/spring-context.xsd">
    11      
    12      
    13      <!-- IoC容器的配置,也叫控制反转,要创建的所有的对象都配置在这里 -->
    14      
    15      <bean id="userAction" class="com.bie.action.UserAction">
    16          <property name="userService" ref="userService"></property>
    17      </bean>
    18 </beans>     

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

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

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

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>Spring_Struts2_20170313</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   
    13   <!-- web.xml分两部分进行配置 -->
    14   <!-- struts2配置 -->
    15   <filter>
    16       <filter-name>struts2</filter-name>
    17       <!-- 如何找到这块,ctrl+shift+t  搜索StrutsPrepareAndExecuteFilter -->
    18       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    19   </filter>
    20   <filter-mapping>
    21       <filter-name>struts2</filter-name>
    22       <url-pattern>/*</url-pattern>
    23   </filter-mapping>
    24   
    25   <!-- 1:spring配置 ,在spring-framework-3.2.5.RELEASEdocsspring-framework-referencehtmlsingle
    26                              搜索context-param找到下面这段话即可。记得就该param-value的值,如下所示;
    27          2:param-value的值最好使用bean,这样方便引用如/WEB-INF/classes/bean-*.xml   
    28   -->
    29   <context-param>
    30     <param-name>contextConfigLocation</param-name>
    31     <param-value>/WEB-INF/classes/bean-*.xml</param-value>
    32   </context-param>
    33   <listener>
    34     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    35   </listener>
    36   
    37   
    38 </web-app>

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

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

     1 package com.bie.dao;
     2 /** 
     3 * @author BieHongLi 
     4 * @version 创建时间:2017年3月13日 下午1:44:27 
     5 * 
     6 */
     7 public class UserDao {
     8 
     9     public void daoTest(){
    10         System.out.println("struts-spring整合的第一个项目");
    11     }
    12 }
     1 package com.bie.service;
     2 
     3 import com.bie.dao.UserDao;
     4 
     5 /** 
     6 * @author BieHongLi 
     7 * @version 创建时间:2017年3月13日 下午1:44:37 
     8 * 
     9 */
    10 public class UserService {
    11 
    12     private UserDao userDao;
    13     public void setUserDao(UserDao userDao) {
    14         this.userDao = userDao;
    15     }
    16     
    17     public void serviceTest(){
    18         userDao.daoTest();
    19     }
    20 }
     1 package com.bie.action;
     2 
     3 import com.bie.service.UserService;
     4 import com.opensymphony.xwork2.ActionSupport;
     5 
     6 /** 
     7 * @author BieHongLi 
     8 * @version 创建时间:2017年3月13日 下午1:43:42 
     9 * 
    10 */
    11 public class UserAction extends ActionSupport{
    12 
    13     
    14     private static final long serialVersionUID = 1L;
    15     private UserService userService;
    16     public void setUserService(UserService userService) {
    17         this.userService = userService;
    18     }
    19 
    20     @Override
    21     public String execute() throws Exception {
    22         userService.serviceTest();
    23         return SUCCESS;
    24     }
    25     
    26 }

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

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>struts2-spring第一次整合</title>
     8 </head>
     9 <body>
    10 
    11 <h1>struts2-spring第一次整合</h1>
    12 
    13 </body>
    14 </html>

    效果如下所示:

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

  • 相关阅读:
    插入排序的Python代码实现
    数据结构基础--二叉堆、优先队列
    数据结构基础--二叉树
    数据结构基础--数组、链表、栈、队列、哈希表
    转:数据结构与算法系列--十大排序(附动态图解
    快速排序的Python代码实现
    选择排序的Python代码实现
    csv文件的读取写法 from Udacity
    Linux下测试ZLAN 5800
    冒泡排序的Python代码实现
  • 原文地址:https://www.cnblogs.com/biehongli/p/6542603.html
Copyright © 2011-2022 走看看