【问题标题】:Spring Boot - serving pure html and static contentSpring Boot - 提供纯 html 和静态内容
【发布时间】:2016-07-14 08:36:03
【问题描述】:

到目前为止,我已经花了很多时间来完成这项工作,但仍然无法弄清楚。如何提供纯 .html 页面。这是项目: https://github.com/robson021/Invoice-Writer

Thymeleaf 引擎工作正常,但如果我尝试返回“常规”.html 文件,则会出错。

Whitelabel 错误页面 (...) 出现意外错误 (类型=内部服务器错误,状态=500)。异常解析文档: template="test",第 6 行 - 第 3 列

我承认这是因为我的“test.html”文件看起来不像 Thymeleaf 文件。但是,我尝试从 Maven 的 POM 中删除 Thymeleaf(或在 InteliJ 中创建新的项目形式 spring 初始化程序,没有 Thymeleaf 的项目,只有 Web)并将 .html 文件放入不同的目录(静态、公共、WEB-INF)但仍然失败.. . 我还尝试使用 Java 类手动配置项目。不幸的是得到了 404 或 500 错误。

由于这是学校项目,目标是让你的前端独立,我想使用纯 html 和 AngularJS。没有 .jsp 或模板引擎。

谁能告诉我如何让它工作是 Spring Boot 项目?

编辑:

我的控制器:

@Controller
public class TestController {

    @RequestMapping("/test")
    public String goToTestPage() {
        return "test";
    }
}

主类:

@SpringBootApplication
@EnableAutoConfiguration
public class InvoiceWriterApplication {
    public static void main(String\[\] args) {
        SpringApplication.run(InvoiceWriterApplication.class, args);
    }
}

及项目结构:http://i.stack.imgur.com/vCqiQ.png

【问题讨论】:

    标签: java html spring spring-mvc configuration


    【解决方案1】:

    @Controller 替换为@RestController - (或在控制器类上添加@ResponseBody@Contorller) - 将控制器转换为REST 控制器。

    这是因为@Controller 单独注解会导致返回值“home”被映射到模板文件。

    还需要使用 @EnableJpaRepositories 才能使 JPA 存储库工作。

    【讨论】:

    • 我已经这样做了。 “src/main/resources/static/test.html”并得到“出现意外错误(类型=内部服务器错误,状态=500)。圆形视图路径 [test]:将调度回当前处理程序 URL [/ test] 再次。检查您的 ViewResolver 设置!(提示:这可能是未指定视图的结果,由于默认视图名称生成。)"
    • 尝试干净启动项目
    • 你必须删除TestController
    • 但是没有控制器我将如何响应 /test 页面?我的意思是使用 MediaType.APPLICATION_JSON_VALUE
    猜你喜欢
    • 2019-03-30
    • 2015-10-30
    • 1970-01-01
    • 2015-06-21
    • 2019-09-20
    • 2021-10-29
    • 1970-01-01
    • 2015-05-24
    相关资源
    最近更新 更多