• Spring MVC笔记(八) 控制器类名称处理映射


    ControllerClassNameHandlerMapping类是基于约定的处理程序映射类,它将URL请求映射到配置中提到的控制器的名称。这个类接受控制器名称并将其转换为带有前导“/”的小写形式。 例如HelloController映射到URL: “/hello*”。

    创建springMVC工程:

    ControllerNameHandlerMapping-servlet.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <context:component-scan base-package="com.bwy" />
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <bean
            class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    
        <bean class="com.bwy.controller.HelloController" />
        <bean class="com.bwy.controller.WelcomeController" />
    
    </beans>

    根据如上配置,对于/hello*.*的URL,DispatcherServlet会将请求转发到HelloController类;对于/welcome*.*的URL,DispatcherServlet会将请求转发到WelcomeController类。

    HelloController.java

    package com.bwy.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    public class HelloController extends AbstractController {
    
      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
        ModelAndView model = new ModelAndView("hello");
        model.addObject("message", "Hello World!");
        return model;
      }
    
    }

    WelcomeController.java

    package com.bwy.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    public class WelcomeController extends AbstractController {
    
      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
        ModelAndView model = new ModelAndView("welcome");
        model.addObject("message", "welcome!");
        return model;
      }
    
    }

    运行tomcat,并访问http://localhost:8080/ControllerNameHandlerMapping/welcome2.d,结果如下所示:

  • 相关阅读:
    查看Mysql版本
    Day03_SpringCloud2
    Day01_SpringBoot
    【Java面试题】如何判断一个字符串中某个字符出现的次数?
    你以为这样写Java代码很6,但我看不懂
    smart-socket实战:玩转心跳消息
    JVM 对象分配过程
    Spring Cloud基于Redis实现的分布式锁
    Python10行以内代码能有什么高端操作
    会话技术之Cookie详解
  • 原文地址:https://www.cnblogs.com/weyoung1987/p/8137966.html
Copyright © 2020-2023  润新知