3

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?

3
  • If the controller isn't called, that has nothing to do with views. Move your application class to the ... securejwt package. Commented Dec 28, 2018 at 7:59
  • 1
    move you HisservicesJwtApplication.java to outer/upper layer package i.e. com.skm.hisservice.secueejwt Commented Dec 28, 2018 at 8:36
  • please post code of HisservicesJwtApplication.java Commented Dec 28, 2018 at 8:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that's correct. It was all about package restructuring. Thanks. I created project from Spring Boot Initializer and it created this package structure. Thanks a lot.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.