• springmvc 路由


    工作中MVC是较常使用的web框架,作为研发人员,也习惯了以编写Controller作为项目开始,写好了Controller和对应的方法,加上@RequestMapping注解,我们也就认为一切已经准备就绪,可以开始运行了,那是否我们曾想过MVC是如何根据请求路径找到对应的Controller和Controller中具体的Method呢?

    HandlerMapping

    1. SimpleUrlHandlerMapping

    2. BeanNameUrlHandlerMapping

    3. RequestMappingHandlerMapping

    HandlerMapping就是处理请求path和具体handler映射关系的组件,我们将从最基础到SimpleUrlHandlerMapping到目前MVC中默认优先使用使用的RequestMappingHandlerMapping来进行讲解,如下实验中,我们假设已经在web.xml中配置了DispatcherServlet这个基础的servlet,且每次实验时DispatcherServlet会加载对应实验中的配置文件.

    一、SimpleUrlHandlerMapping
    bean 配置

     1<?xml version="1.0" encoding="UTF-8"?>
    2<beans xmlns="http://www.springframework.org/schema/beans"
    3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5 <bean id="simpleController" class="com.seven.springmvc.controller.SimUrlController"></bean>
    6 <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    7 <property name="mappings">
    8 <props>
    9 <prop key="/simple">simpleController</prop>
    10 </props>
    11 </property>
    12 </bean>
    13</beans>

    Controller 代码

    1public class SimUrlController implements  Controller {
    2 @Override
    3 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    4 PrintWriter writer = response.getWriter();
    5 writer.print("hi simple");
    6 return null;
    7 }
    8}

    运行结果

    1请求:http://localhost:8080/simple
    2输出 hi simple

    二、BeanNameUrlHandlerMapping
    bean 配置

    1<?xml version="1.0" encoding="UTF-8"?>
    2<beans xmlns="http://www.springframework.org/schema/beans"
    3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5 <bean id="/beanNameController" class="com.seven.springmvc.controller.BeanNameController"/>
    6 <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    7</beans>

    Controller 代码

    1public class BeanNameController implements Controller {
    2 @Override
    3 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    4 PrintWriter writer = response.getWriter();
    5 writer.print("hi beanName");
    6 return null;
    7 }
    8}

    运行结果

    1请求:http://localhost:8080/beanNameController
    2输出:hi beanName

    三、RequestMappingHandlerMapping
    bean 配置

    1<?xml version="1.0" encoding="UTF-8"?>
    2<beans xmlns="http://www.springframework.org/schema/beans"
    3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5 <bean id="controller" class="com.seven.springmvc.controller.RequestMappingController"/>
    6 <bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    7</beans>

    Controller 代码

    1@Controller
    2public class RequestMappingController {
    3 @RequestMapping("/index")
    4 @ResponseBody
    5 public String index(){
    6 return "hi requestMapping";
    7 }
    8}

    运行结果

    1请求:http://localhost:8080/requestMapping
    2输出:hi requestMapping

    结论

    1. SimpleUrlHandlerMapping 是根据其mappings属性中配置的key-value来定义映射关系的,根据请求path【作为映射关系中的key】,找到对应的Controller【此时只要找到Controller就可以了,不用到具体方法,因为自定义的Controller必须继承Controller接口,并实现handleRequest方法,最后也是执行handleRequest方法】
    2. BeanNameUrlHandlerMapping 是根据spring容器中配置的bean名称【注意bean的名称需要以'/'开头】和Controller来定义映射关系的, 根据请求路径path去查找beanName和path一致的bean【其中bean必须继承Controller并实现handleRequest方法】,然后调用handleRequest来处理请求
    3. RequestMappingHandlerMapping 是依赖注解RequestMapping来实现path和Hander的映射关系的,具体是根据注解中的value值作为key,注解所在方法作为Handler设置映射关系,然后请求到达时则根据请求path去查找对应的Handler【具体Controller中的Method】

    因为我们项目开发中大多使用注解【RequestMapping】的方式实现路由的映射,也就是用RequestMappingHandlerMapping来实现的,关于RequestMappingHandlerMapping的具体初始化和查询的功能是如何根据注解来完成路由的映射的,我们将在下一篇单独讲解…………………

    微信公众号:宋坤明
    如果您觉得对您有所帮助,记得转发点赞哦
    如有问题或建议,请公众号留言或者直接微信联系我
    下面的是我的公众号二维码图片,欢迎关注我

    图注:宋坤明公众号图注:宋坤明公众号

  • 相关阅读:
    axis2 WebService的发布与调用
    sql语句having子句用法,很多时候你曾忘掉
    linux下tomcat开机自启动
    框架使用的技术主要是SpringMVC 在此基础上进行扩展
    SpringMVC整合Mongodb开发 架构搭建
    解决Linux下3T硬盘分区只有2T(2199G)可用
    ubuntu cp(copy) command
    Linux如何根据UUID自动挂载磁盘分区
    python exec和eval
    在OpenERP报表中使用selection 类型字段
  • 原文地址:https://www.cnblogs.com/skm-blog/p/9164401.html
Copyright © 2020-2023  润新知