Redirect to html with Spring Boot

Asked

Viewed 1,604 times

2

I’m starting to create applications with Spring Boot + Gradle, but when configuring Controller, I can’t redirect a url to an html or jsp file.

My application is like this:

inserir a descrição da imagem aqui

Project structure:

inserir a descrição da imagem aqui

My Controller:

inserir a descrição da imagem aqui

The page displayed by http://localhost:8080/ is a blank page written: login.html and not displaying the html I put in Static folder.

I saw some similar cases, but all the answers I found didn’t work, so I thought I’d open a new topic.

2 answers

1

I managed to solve it that way:

Creating a Requestmapping in the Controller

@RequestMapping("/")
   public ModelAndView index(){
   return new ModelAndView("index");
}

Unlike yours, it returns one ModelAndView, that will pull the page from within resources/templates/.

Creating a Dependency in pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

And to finish, have the page index.html inside resources/templates/.

  • Very good friend, thank you, in this way it worked perfectly p/me!

0

Make the following change to your endpoint

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.