Java 如何在 Spring Boot 中从一个安静的控制器返回一个 html 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38700790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a html page from a restful controller in spring boot?
提问by Gustavo
I want to return a simple html page from controller, but I get only the name of the file not its content. Why?
我想从控制器返回一个简单的 html 页面,但我只得到文件的名称而不是它的内容。为什么?
This is my controller code:
这是我的控制器代码:
@RestController
public class HomeController {
@RequestMapping("/")
public String welcome() {
return "login";
}
}
This is my project structure:
这是我的项目结构:
[
[
采纳答案by kukkuz
When using @RestController
like this:
@RestController
像这样使用时:
@RestController
public class HomeController {
@RequestMapping("/")
public String welcome() {
return "login";
}
}
This is the same as you do like this in a normal 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.
使用@ResponseBody
返回return "login";
作为 String 对象。您返回的任何对象都将payload
作为 JSON附加到 HTTP 正文中。
This is why you are getting just login
in the response.
这就是为什么你得到的只是login
回应。
回答by ppeterka
The String
you return here:
在String
你回到这里:
return "login";
Is actually the whole contentwhat you send back to the browser.
实际上是您发送回浏览器的全部内容。
If you want to have the content of a file to be sent back, one way is:
如果您想将文件的内容发回,一种方法是:
- Open file
- Read contents into String
- Return the value
- 打开文件
- 将内容读入字符串
- 返回值
You can go by with this answer on the question Spring boot service to download a file
回答by Aleksandr Podkutin
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.
你只得到 name 因为你只返回 name return "login";
。这是@RestController
与此控制器返回数据,而不是一个视图。因此,您只能获得从方法返回的内容。
If you want to show view with this name you need to use Spring MVC, see this example.
如果要显示此名称的视图,则需要使用 Spring MVC,请参阅此示例。
回答by Happy Nguyen
You can try using ModelAndView
:
您可以尝试使用ModelAndView
:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
回答by Noshin
Replace @RestController
with @Controller
.
替换@RestController
为@Controller
。
回答by user1001
Replace @Restcontroller
with @controller
. @Restcontroller
returns only content not html and jsp pages.
替换@Restcontroller
为@controller
。@Restcontroller
只返回内容而不是 html 和 jsp 页面。
回答by Kairat Doshekenov
You should use login.html
. And replace RestController
with Controller
你应该使用login.html
. 并替换RestController
为Controller
回答by PGMacDesign
The answer from Kukkuz did not work for me until I added in this dependency into the pom file:
直到我将此依赖项添加到 pom 文件中后,Kukkuz 的答案才对我有用:
<!-- 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.
然后它开始工作得很好。如果将来有人遇到同样的问题并且遵循第一个答案并不能解决您的问题,请在使用@Kukkuz 答案中的代码后执行另外 2 个步骤,看看这是否有所不同。
回答by Please_Dont_Bully_Me_SO_Lords
The most correct and modern form is to use IoC
to put dependencies into the endpoint method, like the thymeleafModel
instance...
最正确和现代的形式是使用IoC
将依赖项放入端点方法中,例如thymeleafModel
实例...
@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/
回答by Johnny Willer
I'm not using Thymeleaf and the only way it worked for me was with @Controller
in the class and
我没有使用 Thymeleaf,它对我有用的唯一方法是@Controller
在课堂上使用
@RequestMapping(method = RequestMethod.GET, value = "/")
public String index() {
return "index.html";
}