How to return html page from sedative controller in spring boot?

I want to return a simple html page from the controller, but I only get the file name, not its contents. Why?

This is my controller code:

@RestController public class HomeController { @RequestMapping("/") public String welcome() { return "login"; } } 

This is my project structure:

[ enter image description here

+10
source share
11 answers

When using @RestController like this:

 @RestController public class HomeController { @RequestMapping("/") public String welcome() { return "login"; } } 

This is the same as you do it in a regular controller:

 @Controller public class HomeController { @RequestMapping("/") @ResponseBody public String welcome() { return "login"; } } 

Using @ResponseBody returns return "login"; as a string object. Any object you return will be attached as payload in the HTTP body as JSON.

That is why you only get login in the response.

+19
source

The answer from Kukkuz didn't work for me until I added this pom file to this dependency:

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

From the manual here .

Like updating registry resources, as described here .

Then he began to work perfectly. If someone encounters the same problem in the future and after the first answer does not solve your problem, follow these two other steps after using the code in @Kukkuz's answer and see if this has changed.

+4
source

You can try using ModelAndView :

 @RequestMapping("/") public ModelAndView index () { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); return modelAndView; } 
+3
source

Replace @RestController with @Controller .

+2
source

Replace @Restcontroller with @controller . @Restcontroller only returns content, not html and jsp pages.

+2
source

Follow below steps

  1. You need to put HTML files in / templates /
  2. Replace @RestController with @Controller
  3. Delete if you use any recognizers.
  4. Your controller method should return the view file name without extension as retunrn "index"
  5. Include below dependencies.

     <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>' 
+2
source

You must use login.html . And replace RestController with Controller

+1
source

I don't use Thymeleaf, and the only way it worked for me was with @Controller in the class and

 @RequestMapping(method = RequestMethod.GET, value = "/") public String index() { return "index.html"; } 
+1
source

String you will return here:

  return "login"; 

In fact, all the content that you send back to the browser.

If you want the contents of the file to be sent back, one way:

  • Open file
  • Reading content in a string
  • Returns value

You can answer this answer to the question Spring boot file to download file

0
source

You only get the name, because you only return "login"; name return "login"; This is an @RestController and this controller returns data, not a view; because of this, you only get the content that you return from the method.

If you want to show a view with this name, you need to use Spring MVC, see this example .

0
source

The most correct and modern form is the use of IoC to set dependencies in the endpoint method, for example, an instance of Model thymeleaf ...

  @Controller public class GreetingController { @GetMapping("/greeting") public String greeting( @RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; // returns the already proccessed model from src/main/resources/templates/greeting.html } } 

See Full Example: https://spring.io/guides/gs/serving-web-content/

0
source

All Articles