Spring endless redirect



  • Used Spring Bootwhen the user enters any resource /web/** He's been redecorated on the page. /web/index.htmlI mean, if he's trying to come in, /web/213.html, he's redirected. /web/index.html and with other references.

    I wrote it.

    @Bean
        public WebMvcConfigurerAdapter forwardToIndex(){
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addViewControllers(ViewControllerRegistry registry){
                    registry.addViewController("/web/**").setViewName("redirect:/web/index.html");
                }
            };
        }
    

    But if you use it, it's an endless recirculation. index.htmlOh, well. index.html It's in the web folder.

    How am I supposed to do this one time?



  • The simplest and most obvious way is to take. index.html from the folder web♪ If, for any reason, this path is not suitable for you, such workaround can be used:

    registry
        .addViewController("/web/{page:(?!index\\.html).+}")
        .setViewName("redirect:/web/index.html");
    

    Default PathMatcher (sighs) http://docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/util/AntPathMatcher.html (d) Does not support the comparison of URL by regular expression, but supports regular expressions PathVariable♪ That's what we're going to use: in URL, add variables. page to limit possible values for regular expression. If you don't know the design of the species (?!index\.html).+ - it's a regular expression https://ru.wikibooks.org/wiki/%D0%A0%D0%B5%D0%B3%D1%83%D0%BB%D1%8F%D1%80%D0%BD%D1%8B%D0%B5_%D0%B2%D1%8B%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F#.D0.93.D1.80.D1.83.D0.BF.D0.BF.D1.8B (negative lookahead, taken https://stackoverflow.com/a/977294/4863012 ) Any line is approaching him except index.html (sighs) https://www.debuggex.com/r/qb6RWCjLHWdEPBkY ) As a result, we get a redirect that works on any URL, except /web/index.html




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2