zoukankan      html  css  js  c++  java
  • Spring和Struts2整合

    目的:spring容器管理Action类,代替Servlet

    步骤:主要在配置文件

    Struts2:

    添加支持spring的jar包,

    配置<action class="Action类在容器中的id"


    Action类: 
    定义需要容器注入的属性,也就是定义service,service层也要添加调用DAO的属性。并生成get和set方法。

    Action:

    service:

    DAO:

    //模拟数据库连接
    private String conn;

    spring:

    1.web.xml配置文件:

    alt+/ C 选择ContextLoadListener创建配置
    配置文件的位置和名称
    classpath:spring文件名.xml
    加载容器的监听器

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app.xml</param-value>
        
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <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.添加Action类的bean:
    注入Action类的属性
    scope=“prototype” 多例模式

    <!-- DAO -->
    <bean id="testDAO" class="com.hanqi.test.TestDAO">
    	<property name="conn" value="Oracle"></property>
    </bean>
    
    <!-- service -->
    <bean id="testService" class="com.hanqi.test.TestService">
    	<property name="testDAO" ref="testDAO"></property>
    </bean>
    
    <!-- Action -->
    <!-- scope="prototype"多利模式,Action类的实例不能是单利的 -->
    <bean id="testID" class="com.hanqi.test.TestAction" scope="prototype">
    	<property name="testService" ref="testService"></property>
    </bean>
    

      

  • 相关阅读:
    vue全局启用 emulateJSON 选项
    vue全局配置数据接口的根域名
    CSS实现按钮YES-NO按钮+Jquery获取按钮状态。
    Redis命令
    在js中获取 input checkbox里选中的多个值
    Python中常见字符串去除空格的方法总结
    e.target.value和this的区别
    用脚本来运行scrapy crawl ...
    生成器的两种方式
    python中ord()函数,chr()函数,unichr()函数
  • 原文地址:https://www.cnblogs.com/liuyanzeng/p/6192603.html
Copyright © 2011-2022 走看看