struts2、spring和mybatis整合理解

1.web.xml配置文件中要配置StrutsPrepareAndExecuteFilter类的过滤器:

     <filter>

        <filter-name>struts2filter</filter-name>

        <filter-class>

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

   </filter-class>

     </filter>

     <filter-mapping>

        <filter-name>struts2filter</filter-name>

        <url-pattern>/*</url-pattern>

     </filter-mapping>

注:在浏览器输入相应的访问地址----》把请求发送给tomcat---》tomcat判断应该把请求交给哪个application应用---》读取

webapplication应用下的web.xml配置文件---》执行到struts2filter过滤器,交给StrutsPrepareAndExecuteFilter类的doFilter()方法,读取struts.xml文件,根据用户的请求地址找到对应的package、action以及result---》将对应的页面或数据返回给浏览器

   2.web.xml中context-param标签:

     <context-param><!—spring启动加载配置:配置文件地址-->

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath*:/applicationContext.xml</param-value>

     </contex-param>

     <listener><!--spring启动监听:ContextLoaderListener类将spring的applicationContext.xml配置文件载入-->

        <listener-class>

          org.springframework.web.context.ContextLoaderListener

        </listener-class>

     </listener>

   注:①<param-name>标签中的名称不可更改,即任何情况下都必须是contextConfigLocation;

     ②ContextLoaderListener的作用:在web启动时,自动装载spring的applicationContext.xml配置文件。因为它实现了

        ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在

ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成

     ③spring在web中的启动是由ContextLoaderListener开始的,ContextLoaderListener实现了ServletLoaderListener

        接口,并继承了ContextLoader类

   3.struts.xml配置文件:

     ①include节点是struts2中组件化的方式,可以将每个功能模块独立到一个xml配置文件中,然后用include节点引用

        <include file=”struts-default.xml”/>

②package提供了将多个action组织为一个模块的方式,package的名字必须是唯一的;当一个package扩展自另一个package

时,该package会在本身配置的基础上加入扩展的package配置,父package必须在子package前配置

name:package名称

extends:继承的package名称

abstract:设置package的属性为抽象的,抽象的package不能定义action,值为true|false

namespace:定义package命名空间,该命名空间影响到url的地址,如此命名空间为”/test”,访问的地址为:localhost:8080/

   struts/test/xxx.action

<package name=”struts” extends=”struts-default” namespace=”/test”>

...

</package>

     ③定义默认的拦截器,每个action都会自动引用,如果action引用了其他的拦截器,默认的拦截器将无效

        <default-interceptor-ref name=”myStack”></default-interceptor-ref>

     ④全局result配置

        <global-results>

          <result name=”input”>/error.jsp</result>

        </global-results>

     ⑤一个action可以多次被映射(只要action中的name不同)

        name:action 名称

        class:action对应的类路径

        method:调用action中的方法名

posted @ 2017-03-02 16:34  子衿悠悠  Views(1692)  Comments(0Edit  收藏  举报