Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 876
fedor.belov Avatar asked Feb 10 '15 16:02

fedor.belov


People also ask

How display static html file in spring boot MVC application?

Just put index. html in src/main/resources/static/ folder and static html is done!

How use html page in Spring MVC?

Right click on your application, use Export → WAR File option and save your HelloWeb. war file in Tomcat's webapps folder. Now, start your Tomcat server and make sure you are able to access other webpages from webapps folder using a standard browser. Now try to access the URL – http://localhost:8080/HelloWeb/index.


1 Answers

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.

like image 119
hzpz Avatar answered Sep 28 '22 04:09

hzpz