Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - how to serve one static html file for multiple routes in specified root

I need to serve static html file (/src/main/resources/static/folder/index.html) for all routes in specified root (as example '/main/\**'). I've tried to annotate controller method with @RequestMapping("/main/**"), but it works only for '/main' route, not for '/main/foo', '/main/foo/bar', etc...

So, how i can do this in spring boot?

like image 800
Anton Avatar asked Apr 15 '16 05:04

Anton


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!

Which folder is used to publish the html and web pages in spring boot?

html and hello. js files in the /public/ folder. This means that not only does Spring Boot offer a simple approach to building Java or Groovy apps, you can also use it to easily deploy client-side JavaScript code and test it within a real web server environment!


2 Answers

I found this solution:

// application.properties
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html


// Controller for index.html
@Controller
public class IndexController {

    @RequestMapping({"/login", "/main/**"})
    public String index() {
        return "index";
    }
}
like image 182
Anton Avatar answered Nov 15 '22 05:11

Anton


You have to add / edit a Configuration object.

Here is our way to do it:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    public static final String INDEX_VIEW_NAME = "forward:index.html";



    public void addViewControllers(final ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName(INDEX_VIEW_NAME);
        registry.addViewController("/login").setViewName(INDEX_VIEW_NAME);
        registry.addViewController("/logout").setViewName(INDEX_VIEW_NAME);
    }
}
like image 44
WeMakeSoftware Avatar answered Nov 15 '22 04:11

WeMakeSoftware