I want to return a simple html page from controller, but I get only the name of the file not its content. Why?
This is my controller code:
@RestController
public class HomeController {
@RequestMapping("/")
public String welcome() {
return "login";
}
}
This is my project structure:
[
This question is related to
java
html
spring
spring-boot
controller
I know this is old but maybe it helps someone. To do this in a RestController, return a String that is the actual HTML code, then the browser will know what to display.
The answer from Kukkuz did not work for me until I added in this dependency into the pom file:
<!-- Spring boot Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
From the guide here.
As well as updating the registry resources as outlined here.
It then started working just fine. If anyone in the future runs into the same issue and following the first answer does not solve your problem, follow these 2 other steps after utilizing the code in @Kukkuz's answer and see if that makes a difference.
The String
you return here:
return "login";
Is actually the whole content what you send back to the browser.
If you want to have the content of a file to be sent back, one way is:
You can go by with this answer on the question Spring boot service to download a file
I'm not using 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";
}
@Controller
public class HomeController {
@RequestMapping(method = RequestMethod.GET, value = "/")
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
}
This will return the Login.html File. The Login.html should be inside the static Folder.
Note: thymeleaf dependency is not added
@RestController
public class HomeController {
@RequestMapping(method = RequestMethod.GET, value = "/")
public String welcome() {
return "login";
}
}
This will return the String login
You get only the name because you return only the name return "login";
. It's @RestController
and this controller returns data rather than a view; because of this, you get only content that you return from method.
If you want to show view with this name you need to use Spring MVC, see this example.
I did three things:
@RequestMapping(method = RequestMethod.GET, value = "/")
public String aName() {
return "myPage.html";
}
No particular dependency is needed.
Replace @RestController
with @Controller
.
Replace @Restcontroller
with @controller
. @Restcontroller
returns only content not html and jsp pages.
You can solve this in two ways:
First way: If you wish to keep using REST you have to you a ModelAndView
object to render a HTML page. An example is being posted by Happy Nguyen and I am posting it once more here:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
Second Way: If it is not important weather you keep using REST or not, so you can just change the @RestController
to @Controller
and make sure that you have already added Thymeleaf template engine.
The most correct and modern form is to use IoC
to put dependencies into the endpoint method, like the thymeleaf Model
instance...
@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 complete example at: https://spring.io/guides/gs/serving-web-content/
You should use login.html
. And replace RestController
with Controller
@Controller
public class WebController {
@GetMapping("/")
public String homePage() {
return "index";
}
}
Follow below steps:
Must put the html files in resources/templates/
Replace the @RestController
with @Controller
Remove if you are using any view resolvers.
Your controller method should return file name of view without extension like return "index"
Include the 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>`
You can try using ModelAndView
:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
Source: Stackoverflow.com