首页 > 解决方案 > 模板可能不存在

问题描述

我试图调用restapi并且我得到错误

package czajka.piotr.restapi.viewcontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FieldViewController {

    @RequestMapping("/view-fields")
    public String viewFields()
    {
        return "view-fields";
    }
}

org.thymeleaf.exceptions.TemplateInputException:解析模板时出错 [view-fields],模板可能不存在或可能无法被 org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) 上的任何已配置模板解析器访问~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12 .RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072 ) ~[thymeleaf-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366) ~[thymeleaf-spring5-3.0.12.RELEASE。 jar:3.0.12.RELEASE] 在 org.thymeleaf.spring5.view.ThymeleafView。渲染(ThymeleafView.java:190)~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1400)~等

特性

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/serverdb?useUnicode=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8081
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5

也许这会有所帮助

标签: javaspringthymeleaf

解决方案


根据您的 ss您已将模板命名为field-view.html但在控制器中您正在返回view-fields

你的代码:

package czajka.piotr.restapi.viewcontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FieldViewController {

    @RequestMapping("/view-fields")
    public String viewFields()
    {
        return "field-view";
    }
}

顺便说一句,当你制作rest api时,你应该使用@RestController注释而不是 @Controller。实际上, Controller会寻找寺庙,RestController会返回实际的字符串,或者你也可以编写 html 代码。


推荐阅读