zoukankan      html  css  js  c++  java
  • spring-mvc dispatcherServlet

    充当前端控制器的servlet,分派所有的请求
    org.springframework.web.servlet.DispatcherServlet

    配置:

        <servlet>
            <servlet-name>seckill-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置springMVC需要加载的配置文件
                spring-dao.xml,spring-service.xml,spring-web.xml
                Mybatis - > spring -> springmvc
             -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/spring-*.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>seckill-dispatcher</servlet-name>
            <!-- 默认匹配所有的请求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    dispatcherServlet 在加载时会需要一个spring-mvc配置文件,默认会去web-inf目录寻找[servlet-name]-servlet.xml,可以通过init-param指定配置文件

    dispatcherservlet会解析配置文件生成一个webApplicationContext容器,创建该容器需要拥有servletContext,所以需要web容器.

    handle

    实现org.springframework.web.servlet.mvc.Controller接口的类,用于处理请求.

    controller接口的方法签名如下:

    ModelAndView handleRequest (HttpServletRequest request,
    HttpServletResponse response) throws Exception

    controller接口的实现类只能处理一个单一请求动作,使用注解可以支持同时处理多个请求动作.无需实现任何接口,更加灵活.

    基于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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!--配置hanle,映射/hello请求-->
        <bean name="/hello"
              class="com.rlx.controller.HelloController"/>
        <!-- 处理映射器会将bean的名字作为url进行查找,需要在配置handle的时候指定name-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        <!-- 处理适配器,所有处理适配器都要实现HandlerAdapter接口-->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!-- 视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
    
    
    </beans>

    基于注解

    配置文件

    <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--自动扫描base-package的和子包的java文件,有注解就注册为spring的bean-->
        <context:component-scan base-package="com.rlx.controller"/>
        <!--配置annotation类型的处理器映射器-->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
        <!--配置annotation类型的处理器适配器-->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
    
    </beans>




  • 相关阅读:
    还需要做恰当的解读,此时你可能需要一些书:
    创业公司的架构演进史
    任务调度平台Cuckoo-Schedule
    ORACLE中死锁
    Action的模型绑定
    三次握手、四次握手、backlog
    Django框架
    扩展BootstrapTable的treegrid功能
    Identity Service
    Linux权限
  • 原文地址:https://www.cnblogs.com/renluxiang/p/006809c1d296d03c1af94bcb09aa8a7d.html
Copyright © 2011-2022 走看看