java Spring Boot - 提供纯 html 和静态内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36239403/
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
Spring Boot - serving pure html and static content
提问by Robson021
So far I have spent many hours to done this and still can not figure it out. How to serve pure .html pages. This is the project: https://github.com/robson021/Invoice-Writer
到目前为止,我已经花了很多时间来完成这项工作,但仍然无法弄清楚。如何提供纯 .html 页面。这是项目:https: //github.com/robson021/Invoice-Writer
Thymeleaf engine works fine, but if I try to return "regular" .html file I got error.
Thymeleaf 引擎工作正常,但如果我尝试返回“常规”.html 文件,我会出错。
Whitelabel Error Page (...) There was an unexpected error (type=Internal Server Error, status=500). Exception parsing document: template="test", line 6 - column 3
白标错误页面 (...) 出现意外错误(类型=内部服务器错误,状态=500)。异常解析文档:template="test",第 6 行 - 第 3 列
I acknowledge that this is caused because my "test.html" file does not look like Thymeleaf file. However I tried to remove Thymeleaf form Maven's POM (or create new project form spring initializer in InteliJ, project without Thymeleaf, only Web) and put that .html files into different directories (static, public, WEB-INF) and still failed... I also tried to configure project manually with Java classes. Unfortunetely got 404 or 500 erorros.
我承认这是因为我的“test.html”文件看起来不像 Thymeleaf 文件。但是,我尝试从 Maven 的 POM 中删除 Thymeleaf(或在 InteliJ 中创建新的项目表单 spring 初始值设定项,没有 Thymeleaf 的项目,只有 Web)并将该 .html 文件放入不同的目录(静态、公共、WEB-INF),但仍然失败.. . 我也尝试用 Java 类手动配置项目。不幸的是得到 404 或 500 erorros。
Since this is the school project and goal is to make your front-end independet, I want to use pure html with AngularJS. No .jsp or themplate engines.
由于这是学校项目,目标是使您的前端独立,我想在 AngularJS 中使用纯 html。没有 .jsp 或模板引擎。
Can anyone tell me how to make it work is Spring Boot project?
谁能告诉我如何使它工作是 Spring Boot 项目?
Edit:
编辑:
my controller:
我的控制器:
@Controller
public class TestController {
@RequestMapping("/test")
public String goToTestPage() {
return "test";
}
}
main class:
主类:
@SpringBootApplication
@EnableAutoConfiguration
public class InvoiceWriterApplication {
public static void main(String\[\] args) {
SpringApplication.run(InvoiceWriterApplication.class, args);
}
}
and project structure: http://i.stack.imgur.com/vCqiQ.png
回答by Muhammad Hewedy
Replace @Controller
with @RestController
- (or just add @ResponseBody
with the @Contorller
on the controller class) - to convert a controller into a REST controller.
更换@Controller
用@RestController
- (或只需添加@ResponseBody
与@Contorller
上控制器类) -到控制器转换为REST控制器。
This is because @Controller
annotation alone will result in the return value "home" to be mapped to a template file.
这是因为@Controller
单独的注释会导致返回值“home”映射到模板文件。
Also for JPA repositories to work you need to use @EnableJpaRepositories
.
此外,要使 JPA 存储库正常工作,您需要使用@EnableJpaRepositories
.