Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

java - Spring boot doesn't map folder requests to `index.html` files

I've got static folder with following structure:

index.html
docs/index.html

Spring Boot correctly maps requests / to index.html. But it doesn't map /docs/ request to /docs/index.html (/docs/index.html request works correctly).

How to map folder/subfolder requests to appropriate index.html files?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can manually add a view controller mapping to make this work:

@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/docs").setViewName("redirect:/docs/");
        registry.addViewController("/docs/").setViewName("forward:/docs/index.html");
    super.addViewControllers(registry);
    }
}

The first mapping causes Spring MVC to send a redirect to the client if /docs (without trailing slash) gets requested. This is necessary if you have relative links in /docs/index.html. The second mapping forwards any request to /docs/ internally (without sending a redirect to the client) to the index.html in the docs subdirectory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...