web-dev-qa-db-ja.com

テンプレート[ホーム]の解決中にエラーが発生しました。テンプレートが存在しないか、構成済みのテンプレートリゾルバーからアクセスできない可能性があります

初めて投稿するので、これを間違って公式化しているときに修正してください。フォルダー構造の既定のマッピングから逸脱しているわけではありませんが、設定に追加すると思いました。

/ homeマッピングを要求するたびにTemplateInputExceptionが返されます

HomeControllerクラス

@Controller
public class HomeController {
    @GetMapping("/home")
    public String home(Model model, @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        model.addAttribute("name", name);
        return "home";
    }
}

WebConfig:

@Configuration
@ComponentScan(basePackages = "be.mubuntu.dev.cbrapi")

public class WebConfig implements WebMvcConfigurer {
    @Bean
    public ClassLoaderTemplateResolver yourTemplateResolver() {
        ClassLoaderTemplateResolver configurer = new ClassLoaderTemplateResolver();
        configurer.setPrefix("templates/");
        configurer.setSuffix(".html");
        configurer.setTemplateMode(TemplateMode.HTML);
        configurer.setCharacterEncoding("UTF-8");
        configurer.setOrder(0);  // this is important. This way spring //boot will listen to both places 0 and 1
        configurer.setCacheable(false);
        configurer.setCheckExistence(true);
        return configurer;
    }
}

folder structure for home.html file

ログ:

2019-02-09 13:37:48.663 ERROR 12859 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [home], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [home], template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.Java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.Java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.Java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.Java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.Java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.Java:1370) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.Java:1116) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:1055) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:942) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:1005) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:897) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:634) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:882) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:741) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:231) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:53) ~[Tomcat-embed-websocket-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.Java:99) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.Java:92) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.Java:93) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:200) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:199) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:490) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:139) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:92) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:74) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:343) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:408) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:834) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1417) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at Java.base/Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1128) ~[na:na]
    at Java.base/Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:628) ~[na:na]
    at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61) ~[Tomcat-embed-core-9.0.14.jar:9.0.14]
    at Java.base/Java.lang.Thread.run(Thread.Java:834) ~[na:na]

home.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ~{name} + '!'" />
</body>
</html>

result when I navigate to said mapping

3
mUBUNTU

なぜ春のブートで構成するための冗長なコードを書くことによって頭痛を引き起こしているのですか? Spring bootはこれらすべてを自動的に設定します。

クラスを削除するだけです

@Configuration
@ComponentScan(basePackages = "be.mubuntu.dev.cbrapi")

public class WebConfig implements WebMvcConfigurer {
    @Bean
    public ClassLoaderTemplateResolver yourTemplateResolver() {
        ClassLoaderTemplateResolver configurer = new ClassLoaderTemplateResolver();
        configurer.setPrefix("templates/");
        configurer.setSuffix(".html");
        configurer.setTemplateMode(TemplateMode.HTML);
        configurer.setCharacterEncoding("UTF-8");
        configurer.setOrder(0);  // this is important. This way spring //boot will listen to both places 0 and 1
        configurer.setCacheable(false);
        configurer.setCheckExistence(true);
        return configurer;
    }
}

そしてあなたのコントローラークラスではreturn "home";を返していますが、templatesフォルダーにhome.htmlという名前のhtmlはありません。 hello.htmlがあります。だからあなたのコントローラーで

return "hello";
5
Avijit Barua

私の場合、IntelliJ Ideaの再起動が役立ちました。テンプレートのキャッシュに問題があると思いました。

0
nurb

エラーメッセージはあなたにすべてを伝えました

テンプレートが存在しない可能性があります

コントローラーから"home"テンプレートを返しましたが、同封されたスクリーンショットのtemplatesフォルダー内にそのようなファイルがありません。

templates/hello.htmlの名前をtemplates/home.htmlに変更

0
Jakub Ch.