I tried Spring Boot and got the same problem, and just fixed it, I am posting my solution here because, in my opinion, this might be useful for someone.
First, put the application class (which contains the main method) in the root directory of the controllers:
com.example.demo | +-> controller | | | +--> IndexController.java | +--> LoginController.java | +-> Application.java
Application.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Spring scans all components of the demo package subpackages
IndexController.java (return index.html )
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = {""}) public class IndexController { @GetMapping(value = {""}) public ModelAndView index() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); return modelAndView; } }
LoginController.java (return login.html )
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = {"/login"}) public class LoginController { @GetMapping(value = {""}) public ModelAndView login() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("login"); return modelAndView; } }
And now I can log in. View indexes: http: // localhost: 8080 / demo / and Log in: http: // localhost: 8080 / demo / login
source share