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?
Just put index. html in src/main/resources/static/ folder and static html is done!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With