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?
Just put index. html in src/main/resources/static/ folder and static html is done!
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!
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";
}
}
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);
}
}
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