struts2+spring+ibatis配置 学习笔记

1,web.xml中配置struts2

1.1,使用默认的struts2配置文件:src/struts.xml

web.xml

    <filter>  
        <filter-name>struts2</filter-name>  <!-- 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>

1.2,使用指定的struts2配置文件

web.xml

    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
        <init-param>  
            <param-name>config</param-name> <!-- param-name不可自定义 -->
            <param-value>
                struts-plugin.xml,          <!-- 自己指定struts2配置文件时,struts-plugin.xml和struts-default.xml必须显示指定 -->
                struts-default.xml,
                ../struts.xml,              <!-- 配置文件名可自定义 -->
                ../struts-login.xml         <!-- 必须采用相对\WEB-INF\classes的相对路径,且不能用通配符,如../struts-*.xml -->
            </param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

等同于下面的web.xml + struts.xml

web.xml

    <filter>  
        <filter-name>struts2</filter-name>  
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>  
            <param-name>config</param-name>
            <param-value>
                ../struts.xml,             
                ../struts-login.xml
            </param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>struts2</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

struts.xml

    <include file="struts-plugin.xml"/>   <!-- <include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。 -->
    <include file="struts-default.xml"/>  <!-- 用<include>引用的xml文件也必须是完成,有头有尾。 -->
2,web.xml中配置spring

2.1,使用spring默认配置文件:WEB-INF/applicationContext.xml

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
2.2,使用自己指定的spring配置文件:

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name> <!-- param-name名字不可改变 -->
        <param-value>               
            /WEB-INF/spring.xml,    <!-- 可以使用spring的 <importresource=""/> 标签来进行配置文件嵌套 --> 
            /WEB-INF/spring-*.xml   <!-- 配置文件名字可自定义,可以使用通配符 -->
        </param-value>
  </context-param>
3,spring配置文件中配置ibatis

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/TRAINING</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
    </bean>   
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>
                /WEB-INF/ibatis.xml   <!-- 配置文件名字可自定义 -->
            </value>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
4,ibatis配置文件中配置sqlMapConfig

<sqlMapConfig>
<sqlMap resource="com/dao/ibaties/map/User.xml" /> <!-- 路径和xml名由自己定义-->
</sqlMapConfig>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值