学习笔记3:Struts2+Spring JDBC+Spring

Spring有许多功能,这里我所使用到的只是用于管理容器和Spring监听器的作用。

Spring用于管理容器时,需要添加applicationContext.xml文档系列
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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <bean id="UserDao" class="org.kingtoon.dao.impl.UserDaoImpl" />
    <bean id="LoginAction" class="org.kingtoon.action.LoginAction"/>
    <bean id="RegisterAction" class="org.kingtoon.action.RegisterAction"/>

</beans>

其中的beans用于类,接口名称的定义,之后struts.xml文档系列中,可直接用名称指代类
struts.xml

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />

    <constant name="struts.multipart.maxSize" value="10000000000" />
    <constant name="struts.multipart.saveDir" value="/tmp" />
    <!-- 登陆 -->
    <include file="struts-example.xml"/>
    <include file="struts-example2.xml"/>
</struts>

struts-examle.xml

<?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="example" namespace="/example" extends="struts-default">
         <action name="register" class="RegisterAction" method="Regist">   
            <result name="success">/Congratulation.jsp</result>   
            <result name="findit">/Login.jsp</result>
            <!-- <result name="input">/Register.jsp</result> -->
         </action>  
    </package>

</struts>

struts-example2.xml

<?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="example2" namespace="/example2" extends="struts-default">
         <action name="login" class="org.kingtoon.action.LoginAction" method="Login">   
            <result name="success">/Welcome.jsp</result>  
            <result name="fail">/Register.jsp</result> 
            <result name="input">/Login.jsp</result>   
         </action>  
    </package>
</struts>

另外,Spring监听器在web.xml中进行配置

<?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">

    <!-- 配置spring的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

     <filter>
      <filter-name>LoginFilter</filter-name>
      <filter-class>org.kingtoon.filter.LoginFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--struts的配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>  
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener> 

    <welcome-file-list>
       <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

这些都是新加入的配置
其他的类都与笔记2中的一样,不需要改变

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值