Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何更改自动添加的SpringMVC处理程序的默认处理程序顺序_Spring Mvc_Resources_Handler_Static Linking - Fatal编程技术网

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

Spring mvc 如何更改自动添加的SpringMVC处理程序的默认处理程序顺序,spring-mvc,resources,handler,static-linking,Spring Mvc,Resources,Handler,Static Linking,在执行我的项目时,我发现在我使用@RequestMapping(value=“/**”)和@RequestMapping(method=RequestMethod.GET)设置控制器后,不起作用 带注释的控制器似乎比SimpleRhlHandler具有更高的优先级 有人能解决这个问题吗?我需要那个控制器,但无法删除它 提前谢谢 以下是我如何设置项目以及有关问题的详细信息: Web.xml <servlet> <servlet-name>Test</servlet

在执行我的项目时,我发现在我使用
@RequestMapping(value=“/**”)和
@RequestMapping(method=RequestMethod.GET)
设置控制器后,
不起作用

带注释的控制器似乎比SimpleRhlHandler具有更高的优先级

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

提前谢谢

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

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>
运行日志。(正如您在这里看到的,资源请求由带注释的控制器处理,而不是ResourceHttpRequestHandler)

如果我删除
应用程序控制器
,就可以访问静态资源

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
通过使用
value=“/**”
您的
ApplicationController
将绝对处理所有请求的URL

只需指定一个静态内容不匹配的限制性更强的模式,它就会像您希望的那样工作;示例:

  • value=“/dynamic/**”
  • value=“/***/.html”
  • value=“/pages/**”
选择取决于您,但是“RESTful”样式现在非常流行,所以我会遵循它。

使用
value=“/**”
您的
应用程序控制器将绝对处理所有请求的URL

只需指定一个静态内容不匹配的限制性更强的模式,它就会像您希望的那样工作;示例:

  • value=“/dynamic/**”
  • value=“/***/.html”
  • value=“/pages/**”

选择取决于您,但“RESTful”风格最近非常流行,所以我会遵循它。

我也有同样的要求。
我在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


这里有完整的源代码-

我也有同样的要求。
我在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

完整的源代码在这里-

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
@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";
    }

}
@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;
    }

}