3

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

Serob_b
  • 965
  • 12
  • 29
  • How do you know the 'controller method is entering for sure'? – BeUndead Dec 20 '20 at 00:34
  • Debugging, logging. – Serob_b Dec 20 '20 at 00:42
  • The files are loaded from `classpath:/templates` NOT `templates`. That and JSP only works with a WAR file not a JAR file and is limited. – M. Deinum Dec 21 '20 at 13:12
  • @M.Deinum you are right. That's what is set by spring-boot for thymeleaf: https://docs.spring.io/spring-boot/docs/1.3.0.M1/reference/html/common-application-properties.html. But when I set `classpath:/templates` in my properties file (without adding thymleafe dependency) it doesn't work. So the only (and I am sure not the effective) solution I found was adding thymleaf dependency into pom. – Serob_b Dec 23 '20 at 23:29

3 Answers3

2

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 if 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).

Serob_b
  • 965
  • 12
  • 29
0

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;
    }
Issa
  • 49
  • 5
-1

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

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