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>




  • 相关阅读:
    让treeview控件的滚动条移动到顶部的位置
    用javascript 来将word 转成 html 更加简单,三行代码搞定
    在asp.net中,添加itemtempert 项模板时,如果在项模板里有其它控件,如何控件这些控件的属性?
    C#格式化数值结果表 数字的格式化输出
    直接创建一个XmlDocument文档
    实现 网页的 数据加载中.... 效果,很简单哦
    c#编写XML读写删改功能,算是比较全面的介绍XML操作的文章了。
    如何获取 电脑 的一些硬件信息。用于软件的加密等算法。
    关于正则表达式的使用一例。在Textbox 对话框内限制只能输入数字,如果输入出错,则清空内容。
    WPF中UI及时更新,如何在处理长时间工作时,保持界面的持续更新
  • 原文地址:https://www.cnblogs.com/renluxiang/p/006809c1d296d03c1af94bcb09aa8a7d.html
Copyright © 2011-2022 走看看