Spring boot does not load jsp file

Viewed 656

Using spring boot starter 2.4.1 version.

resources folder has the following structure. As it is seen hello.jsp is in templates folder. These are included in application.properties:

  spring.mvc.view.prefix=/templates/
  spring.mvc.view.suffix=.jsp

And a simple controller:

@Controller
public class HelloController {
    @GetMapping("/hello")
    public String index(){
        return "hello";
    }
}

But get 404.

Controller method is entering for sure.

Tried what is suggested in the following answer, tried using the webapp, but didn't help: /WEB-INF is not created inside target at all, it seems maven is ignoring webapp and WEB-INF folders.

target

3 Answers

Just restarting Inellij IDEA with invalidating caches (File | Invalidate Caches / Restart) solved the problem.

Actually it started to work with templates folder, so templates are the root, and in you put something inside it, you can access from controllers without any prefix setting.

But again nothing is workable from webapp, seams maven is ignoring webapp at all. Maybe this is connected with maven, or spring-boot version.

And add thymeleafe dependency and it will work with .html files (this is not the thing I like, but up to now the only workable solution found).

try to change your configs and codes like below:

in application.properties put these configs:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp


LoginForm class:
    public class LoginForm {
        private String email;
        private String password;

        public LoginForm() {}
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

**LoginForm is just a normal bean with a no-arg Constructor and the getters / setters**

Now, we create a LoginHandler Class to coordinate and handle the whole login process.

    @Controller
    @RequestMapping("/login")
    public class LoginHandler {
    private static final String LOGIN_VIEW = "login";
    private static final String LOGOUT_VIEW = "logout";
    }

let’s define some handler methods.

    @GetMapping("/login")
    public String showLoginView(Model model) {
    model.addAttribute("loginForm", new LoginForm());
    return LOGIN_VIEW;
    }

okay, i will fix your code and run in your IDE,

 @RequestMapping("/hello")
public String index() {
    return "hello";
}

Related