zoukankan      html  css  js  c++  java
  • spring 动态设置 RequestMapping

    原理:自定义 HandlerMapping ,把请求都集中到一个controller中

    定义自己的 HandlerMapping

    package com.example.demo;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.servlet.handler.AbstractHandlerMapping;
    
    /**
     * @author siqi
     */
    public class MyHandlerMapping extends AbstractHandlerMapping {
    
        public WelcomeController welcomeController;
    
        @Override
        protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
            return welcomeController;
        }
    }

    定义controller

    package com.example.demo;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    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 {
    
            System.out.println("simple url======");
    
            response.getWriter().write("hello");
            return null;
        }
    
    }

    添加到容器中

    @Bean
        public WelcomeController simpleUrlController() {
            return new WelcomeController();
        }
    
        @Bean
        public MyHandlerMapping myHandlerMapping() {
            MyHandlerMapping myHandlerMapping = new MyHandlerMapping();
            //注意优先级
            myHandlerMapping.setOrder(Integer.MAX_VALUE - 10);
            myHandlerMapping.welcomeController = simpleUrlController();
            return myHandlerMapping;
        }

    完成

    BeanNameUrlHandlerMapping

      根据bean的名字即为url地址,需要以"/"开头,支持通配符

    @Configuration
    public class BeanNameUrlHandlerMappingConfig {
        @Bean
        BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
            return new BeanNameUrlHandlerMapping();
        }
     
        @Bean("/beanNameUrl")
        public WelcomeController welcome() {
            return new WelcomeController();
        }
    }

    可以在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 https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <value>
                    /simpleurl=simpleUrlController
                </value>
            </property>
            <property name="order" value="1"/>
    
        </bean>
        <bean id="simpleUrlController" class="com.example.demo.WelcomeController"/>
    
    
    </beans>
    package com.example.demo;
    
    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 {
    
            System.out.println("simple url======");
    
            response.getWriter().write("hello");
    
            return null;
        }
    
    }

    SimpleUrlHandlerMapping

      更加灵活,可以在url和bean之间随意指定

    @Configuration
    public class SimpleUrlHandlerMappingConfig {
     
        @Bean
        public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
            SimpleUrlHandlerMapping simpleUrlHandlerMapping
              = new SimpleUrlHandlerMapping();
            
            Map<String, Object> urlMap = new HashMap<>();
            urlMap.put("/simpleUrlWelcome", welcome());
            simpleUrlHandlerMapping.setUrlMap(urlMap);
            
            return simpleUrlHandlerMapping;
        }
     
        @Bean
        public WelcomeController welcome() {
            return new WelcomeController();
        }
    }

    同等xml

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /simpleUrlWelcome=welcome
                /*/simpleUrlWelcome=welcome
            </value>
        </property>
    </bean>
    <bean id="welcome" class="com.baeldung.WelcomeController" />

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/simpleUrlWelcome">welcome</prop>
                <prop key="/*/simpleUrlWelcome">welcome</prop>
            </props>
        </property>
    </bean>

    WelcomePageHandlerMapping

      默认页面的映射,在静态资源路径下 找 index.html 文件

    @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
    public class ResourceProperties {
    
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
                "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
    
        /**
         * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
         * /resources/, /static/, /public/].
         */
        private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

    WebMvcAutoConfiguration.java

            @Bean
            public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
                    FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
                WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
                        new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
                        this.mvcProperties.getStaticPathPattern());
                welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
                welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
                return welcomePageHandlerMapping;
            }

    参考:https://www.baeldung.com/spring-handler-mappings

         接口版本控制

  • 相关阅读:
    把office文档转换为html过程中的一些坑
    Win10内置应用恢复初始状态
    HTTP 错误 500.23
    关于Application.Lock和Lock(obj) 转 http://www.cnblogs.com/yeagen/archive/2012/03/01/2375610.html
    正则表达式 细节
    Python常用内置函数总结
    将Python脚本封装成exe可执行文件 转
    爬虫技能
    python之map、filter、reduce、lambda函数 转
    小细节
  • 原文地址:https://www.cnblogs.com/siqi/p/13463055.html
Copyright © 2011-2022 走看看