21.16.11 路径匹配

这些配置允许你对许多与URL映射和路径匹配有关的设置进行定制。关于所有可用的配置选项,请参考PathMatchConfigurer类的API文档。

下面是采用MVC Java编程配置的一段代码:

  1. @Configuration
  2. @EnableWebMvc
  3. public class WebConfig extends WebMvcConfigurerAdapter {
  4. @Override
  5. public void configurePathMatch(PathMatchConfigurer configurer) {
  6. configurer
  7. .setUseSuffixPatternMatch(true)
  8. .setUseTrailingSlashMatch(false)
  9. .setUseRegisteredSuffixPatternMatch(true)
  10. .setPathMatcher(antPathMatcher())
  11. .setUrlPathHelper(urlPathHelper());
  12. }
  13. @Bean
  14. public UrlPathHelper urlPathHelper() {
  15. //...
  16. }
  17. @Bean
  18. public PathMatcher antPathMatcher() {
  19. //...
  20. }
  21. }

在XML命名空间下实现同样的功能,可以使用<mvc:path-matching>元素:

  1. <mvc:annotation-driven>
  2. <mvc:path-matching
  3. suffix-pattern="true"
  4. trailing-slash="false"
  5. registered-suffixes-only="true"
  6. path-helper="pathHelper"
  7. path-matcher="pathMatcher"/>
  8. </mvc:annotation-driven>
  9. <bean id="pathHelper" class="org.example.app.MyPathHelper"/>
  10. <bean id="pathMatcher" class="org.example.app.MyPathMatcher"/>