zoukankan      html  css  js  c++  java
  • Spring与Struts2集成开发

    
    
    Struts2和Spring都是不错的开源框架,Spring与Struts2集成开发,把二者结合在一起使用,开发效果更佳,效率杠杠的。下面介绍一下如何Spring与Struts2集成在一起开发。分七步战略:
    1、添加Struts2 支持jar包  ;注意:加上一个Struts-spring-plugin(集成)插件包
    2、添加spring 支持jar包
    3、编写struts.xml配置文件
    	注意:Action是交由Spring管理;所以action class属性应和spring 中配置的Action  Bean ID一致
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
        <struts>
            <package name="default" namespace="/" extends="struts-default">
                <!-- 配置用户ACTION 
                    Action由Spring维护
                    class就是spring中Action  Bean的ID
                -->
                <action name="useraction*" class="useraction" method="{1}">
                    <result name="success" type="redirect">success.jsp</result>
                    <result name="login" type="redirect">index.jsp</result>
                </action>
                
            </package>
        </struts>
    
    4、在web.xml中配置struts2过滤器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <!-- 配置struts2核心过滤器 -->
        <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>
        
        <!-- 指定spring配置文件所在位置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <!-- 配置sprig监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    
    
    
    
    	
    5、编写spring配置文件applicationContext.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-2.5.xsd">
        
        <!-- 配置持久化层 new userdao()-->
        <bean id="userdao" class="DAO.impl.UserDaoimpl" scope="prototype"></bean>
      
          <!-- 创建业务层 new userbiz() -->
          <bean id="userbiz" class="BIZ.impl.UserBizimpl" scope="prototype">
              <!-- 注入持久化层 DAO
                  IUuserDao userdao = new userdao()
              -->
              <property name="userdao" ref="userdao"></property>
          </bean>
          
          <bean id="useraction" class="Action.UserAction" scope="prototype">
              <!-- 注入业务层BIZ 
                  IUserBiz userbiz = new UserBiz()
              -->
              <property name="userbiz" ref="userbiz"></property>
          </bean>
    
    </beans>
    
    
    
    
    6、在web.xml中配置spring监听器和指定配置文件所在位置(注:如果其文件在WEB-INF/applicatonContext.xml下的话就不需要指定)
        
        <!-- 指定spring配置文件所在位置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <!-- 配置sprig监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    
    
    
    7、编写三层结构;通过spring依次注入各依赖对象
    	BIZ 中注入 DAO层
    	Action 层中注入  BIZ层
  • 相关阅读:
    Delete 语句带有子查询的sql优化
    标量子查询SQL改写
    自定义函数导致的sql性能问题
    Oracle 11G RAC For ASM 利用RMAN COPY进行存储迁移
    WPF 如何控制右键菜单ContextMenu的弹出
    将字符串以用二进制流的形式读入XML文件
    WPF 将数据源绑定到TreeView控件出现界面卡死的情况
    WPF如何实现TreeView节点重命名
    Azure一个Cloud Service支持多个公网地址
    Azure上部署Barracuda WAF集群 --- 2
  • 原文地址:https://www.cnblogs.com/wlx520/p/4732948.html
Copyright © 2011-2022 走看看