如何更改自动添加的Spring MVC处理程序的默认处理程序顺序

3
在实现我的项目时,我发现在使用@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
2个回答

0

通过使用value = "/**",你在说你的ApplicationController将处理所有请求的URL。

只需指定一个更严格的模式,使其不匹配静态内容,它就会按照你想要的方式工作;例如:

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

选择权在你手中,但"RESTful"风格现在非常流行,所以我建议遵循样式指南


0
我有同样的需求。 我正在将一个React应用程序捆绑在我的Spring Boot应用程序中,但我希望所有静态资源请求都具有最高优先级,然后回退到注释控制器。
这样,所有实际上不是静态文件的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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接