Search code examples
springspring-bootstart-page

How to set default landing page in Spring Boot project


I have a Spring Boot project with webapp folder like:

webapp\
    myapp\
        api\
            dashboard.xhtml
        auth\
            login.xhtml
            register.xthml

When I run the sever I need to always enter the url http://localhost:8080/myapp/auth/login.xhtml to begin.

I found this very annoying and want to automatically redirect to this url when I enter just http://localhost:8080.

How can I achieve this?


Solution

  • You can make a new configuration inheriting the WebMvcConfigurer class. In Spring Boot, the MVC part is measuring automatically, so you wouldn't do any more request controlling part in case you are new to it.

    The WebMvcConfigurer class offers addViewControllers virtual function, so that you can override it and add your own controller inside it.

    Just like:

      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/")
            .setViewName("forward:/helloworld.xhtml");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
      }
    

    For more detailed part, you can find it here.