• 【SpringMVC】XML配置说明


    springmvc流程:前端控制器(DispatcherServlet)-->映射器HandlerMapping-->适配器HandlerAdapter-->视图解析器ViewResolver这四个流程缺一不可

    真正内涵: 

    控制器通过映射器得到url所对应的bean,进而去调用适配器(被调用)来执行bean(被执行),得到的调用结果,通过视图解析器来处理。


    下面是springmvc xml配置的内容,看配置文件时,大脑中联想着这四个流程:

    前端控制器(web.xml中已写出)-->映射器(注解/非注解)->适配器(注解/非注解)->视图解析器(只有一个)

    说明:对于映射器、适配器,要保证注解时同时用注解。 

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <!-- 配置handler -->
        <bean id="userController" name="/stu.do" class="com.z.controller.UserController"/>
        <bean name="/userRequest.do" class="com.z.controller.UserRequestHandler"/>
        
        <!-- 对于注解的Handler可以单个配置 在实际开发中使用组件扫描
            可以扫描controller、service...
            这里扫描controller,指定controller包
         -->
        <context:component-scan base-package="com.z.controller"/>
        
        <!-- 处理器映射器1  把BEAN的NAME当成url进行查找,需要在配置handler时指定beanname就是url-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        
        <!-- 处理器映射器2  简单url映射 -->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/items1.do">userController</prop>
                    <prop key="/items2.do">userController</prop>
                </props>
            </property>
        </bean>
        
        <!-- 处理器映射器3 注解开发 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
        
        <!-- 处理器适配器1  非注解 所有适配器实现了HandlerAdapter接口 执行查找到的处理器 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        
        <!-- 处理器适配器2 非注解 -->
        <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
        
        <!-- 处理器适配器3 注解  spring-webmvc-4.3.4  since3.1 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
        
        <!--mvc:annotation-driven代替上边注解映射器和注解适配器
            mvc-annotation-driven默认加载很多参数绑定方法
            比如json解析器就默认加载了,
            如果使用mvn-annotation-driven就不用配置 注解开发映射器 注解开发适配器
         -->
        <mvc:annotation-driven/>
        
        <!-- 注解的映射器和适配器要配对使用 -->
        
        <!-- 视图解析器 解析jsp,默认使用jstl标签,需要引入jstl包-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

    精简版:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
        <!-- 简化版 全部使用注解开发
            对于注解的Handler可以单个配置 在实际开发中使用组件扫描
            可以扫描controller、service...
            这里扫描controller,指定controller包
         -->
        <context:component-scan base-package="com.z"/>
        <!-- mvn:annotation-driven取代了注解的映射器和适配器,并且可以处理json数据 -->
        <mvc:annotation-driven/>
        <!-- 视图解析器 解析jsp,默认使用jstl标签,需要引入jstl包-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>


  • 相关阅读:
    合并二叉树
    剑指 Offer 68
    剑指 Offer 42. 连续子数组的最大和
    C语言 递归实现迷宫寻路问题
    C语言数据结构 统计英文文字每个“单词”出现次数
    C语言 双链表的频度排序管理
    C语言 插入排序使链表递增
    c语言 简单的天数计算器
    数据结构 链表的头插法逆置
    C语言 删除指定的单词
  • 原文地址:https://www.cnblogs.com/zhengwenqiang/p/6804682.html
Copyright © 2020-2023  润新知