2
votes

My Spring Boot project is not loading view from src/main/resources/templates/login.html. However it does load view from src/main/resources/static/index.html.

Here is my project structure:

enter image description here

I have added simple <a> tag for opening my login page in my index.html

    <div>
        <a class="btn" href="login">Login</a>
    </div>

LoginController:

@RequestMapping("/login")
public String showLogin() {
    System.out.println("Login method called");
    return "login";
}

This method doesn't get called. Moreover, I have seen many tutorials showing you don't need to add your view in static folder, just add your views in templates folder and Spring Boot will automatically pick it.

But it seems that Spring Boot is not picking views from templates folder and shows white label error page.

I am not using jsp pages instead I am on Thymelead template.

application.properties

server.servlet.context-path=/hisservices

server.port=8081

welcome.message: Welcome to the HIS Services Dashboard

WelcomeController: <- This shows index.html page

// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
    model.put("message", this.message);
    return "welcome";
}

Do I need to add explicit mapping for templates folder?

2
If the controller isn't called, that has nothing to do with views. Move your application class to the ... securejwt package. - JB Nizet
move you HisservicesJwtApplication.java to outer/upper layer package i.e. com.skm.hisservice.secueejwt - user3145373 ツ
please post code of HisservicesJwtApplication.java - user3145373 ツ

2 Answers

1
votes

IMO, you need to do package restructure.

Move main application file(HisserviceJwtApplication.java) into com.skm.hisservice.securejwt package and create new package config under com.skm.hisservice.securejwt and move SecurityConfig. No need to change another package.

By doing this, your LoginController will scan automatically by spring-boot and method showLogin would also called.

If, you want to continue with existing package structure(not advisable), then add @ComponentScan('com.skm.hisservice.securejwt.controller') into main application file.

0
votes

Unless uses of Thymeleaf, Just Spring Boot could not find views from templates. You have to add following repository to your project to get solution. The way like this

main/webapp/WEB-INIF/*.html

Enter this two lines of code on file application.properties

spring.mvc.view.prefix: /WEB-INF/
spring.mvc.view.suffix: .html

Now, run the project hope the issue resolved.