【问题标题】:How can I change the default handler order of automatically added spring mvc handler如何更改自动添加的 spring mvc 处理程序的默认处理程序顺序
【发布时间】:2011-11-15 06:54:28
【问题描述】:

在实施我的项目时,我发现在使用@RequestMapping(value = "/**")@RequestMapping(method=RequestMethod.GET) 设置控制器后<mvc:resources/> 无法正常工作。

似乎带注释的控制器比 SimpleURLHandler 获得了更高的优先级。

有没有人能解决这个问题?我需要那个控制器,但无法移除它。

提前致谢!

这是我如何设置项目以及有关问题的详细信息:

Web.xml

<servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

webmvc-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">

    <!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
    <context:component-scan base-package="com.web.test" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <!-- Turns on support for mapping requests to Spring MVC @Controller methods
         Also registers default Formatters and Validators for use across all @Controllers -->
    <mvc:annotation-driven conversion-service="applicationConversionService"/>
    <bean class="com.web.test.web.ApplicationConversionServiceFactoryBean" id="applicationConversionService"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
    <mvc:resources location="classpath:/META-INF/web-resources/, /WEB-INF/views/" mapping="/resources/**" cache-period="0" order="0"/>

    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
    <mvc:default-servlet-handler/>

            <!-- selects a static view for rendering without the need for an explicit controller -->
            <mvc:view-controller path="/" view-name="index"/>

            <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver" id="resourceBundleViewResolver" p:basename="META-INF/view/wicket-views" p:order="1"/>

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
        <property name="order" value="2"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    </bean>
    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/views/layouts/layouts.xml</value>
                <value>classpath:/META-INF/view/tiles-views.xml</value>
            </list>
        </property>
    </bean>
... ...
</beans>

ApplicationController.java

@Controller
@RequestMapping(method=RequestMethod.GET)
public class ApplicationController {

    @RequestMapping(value = "/**")
    public ModelAndView handleRequest(HttpServletRequest request){


        ModelAndView mav = new ModelAndView("index");   

        return mav;
    }
}

运行日志。 (这里可以看到,资源请求是由带注解的控制器处理的,而不是ResourceHttpRequestHandler)。

2011-11-15 17:21:09,821 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.tiles2.TilesView: name 'index'; URL [index]] in DispatcherServlet with name 'Test'
2011-11-15 17:21:09,821 [http-8080-2] DEBUG org.apache.tiles.impl.BasicTilesContainer - Render request recieved for definition 'index'
2011-11-15 17:21:09,821 [http-8080-4] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Test' processing GET request for [/Test/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js]
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Matching patterns for request [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] are [/**/, /**]
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - URI Template variables for request [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] are {}
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] to HandlerExecutionChain with handler [com.web.Test.web.ApplicationController@1a57c9e4] and 4 interceptors
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/Test/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] is: -1
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.web.Test.web.ApplicationController.handleRequest(javax.servlet.http.HttpServletRequest)

如果我删除ApplicationController,就可以访问静态资源了。

2011-11-15 17:48:16,784 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Test' processing GET request for [/Test/resources/styles/application-common.css]
2011-11-15 17:48:16,784 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/styles/application-common.css] are [/resources/**]
2011-11-15 17:48:16,785 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/styles/application-common.css] are {}
2011-11-15 17:48:16,785 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/resources/styles/application-common.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@6cc5cbab] and 4 interceptors

【问题讨论】:

    标签: spring-mvc resources handler static-linking


    【解决方案1】:

    通过使用value = "/**",您的意思是您的ApplicationController 将处理所有请求的URL。

    只需指定一个限制性更强的模式,您的静态内容将无法匹配它,它会按照您的意愿工作;例子:

    • value = "/dynamic/**"
    • value = "/**/*.html"
    • value = "/pages/**"

    选择权在你,但“RESTful”风格最近很火,所以我会遵循style guide

    【讨论】:

      【解决方案2】:

      我也有同样的要求。
      我在我的 Spring Boot 应用程序中捆绑了一个 React 应用程序,但我希望所有静态资源请求都具有最高优先级,然后回退到带注释的控制器。

      这样,所有 不是 实际上是静态文件的 url 都将解析为我的 React 应用程序的索引/根页面。

      例如 -
      /admin/css/style.css 应解析为静态文件。
      但是 React 路由应该解析根索引页面/视图。
      /admin/sub/path 应该解析为视图。

      这通过映射我带注释的控制器中的所有路径来工作 -

      @Controller
      public class DefaultMvcController {
      
          PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
      
          @GetMapping({ "", "/"})
          public String index() throws IOException {
              return "index";
          }
      
          @RequestMapping(path = {"/admin", "/admin/**"}, method = RequestMethod.GET)
          public String admin() throws IOException {
      
              return "admin/index";
          }
      
      }
      

      并将静态文件处理程序映射的顺序更改为比带注释的控制器映射的优先级小1

      @Configuration
      @ComponentScan(basePackages = "net.savantly.sprout.controllers")
      @RequiredArgsConstructor
      public class SproutWebMvcConfigurer extends  WebMvcConfigurationSupport {
      
          @Bean
          @Override
          public HandlerMapping resourceHandlerMapping(UrlPathHelper urlPathHelper, PathMatcher pathMatcher,
                  ContentNegotiationManager contentNegotiationManager, FormattingConversionService conversionService,
                  ResourceUrlProvider resourceUrlProvider) {
              HandlerMapping mapping = super.resourceHandlerMapping(urlPathHelper, pathMatcher, contentNegotiationManager, conversionService,
                      resourceUrlProvider);
              ((AbstractHandlerMapping)mapping).setOrder(-1);
              return mapping;
          }
      
      }
      

      完整的源代码在这里 - https://github.com/savantly-net/sprout-platform/tree/development/spring/sprout-spring-boot-starter

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多