在Springmvc和Thymeleaf中,获取"无法初始化模板解析器:尚未设置资源解析器]"错误

时间:2017-04-14 03:25:39

标签: java spring-mvc thymeleaf

接下来我会表现出一些错误,但首先我要解释一下。根据我的一些输出,创建ViewResolver,SpringTemplateEngine,TemplateResolver bean没问题,然后找到主页,查看名称是" home",这里没问题。

然后,根据百里香的模板,我将获得路径" /WEB-INF/templates/home.html" 。但这里错了。发生以下错误

 Servlet.service() for servlet [dispatcher] in context with path [/Spittr] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.ConfigurationException: Cannot initialize template resolver: a resource resolver has not been set] with root cause
org.thymeleaf.exceptions.ConfigurationException: Cannot initialize template resolver: a resource resolver has not been set
    at org.thymeleaf.templateresolver.TemplateResolver.initializeSpecific(TemplateResolver.java:136)
    at org.thymeleaf.templateresolver.AbstractTemplateResolver.initialize(AbstractTemplateResolver.java:106)
    at org.thymeleaf.Configuration.initialize(Configuration.java:200)
    at org.thymeleaf.TemplateEngine.initialize(TemplateEngine.java:831)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1122)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1095)
    at org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:259)
    at org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:179)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

我不知道它意味着什么,并检查了很多,但不知道如何解决。我将展示一些豆子:

@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine);
    System.out.println("create:viewResolver");
    return viewResolver;
}

@Bean
public SpringTemplateEngine templateEngine(TemplateResolver templateResolver){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    System.out.println("create:templateEngine");
    return templateEngine;
}

@Bean
public TemplateResolver templateResolver(){
    TemplateResolver templateResolver = new TemplateResolver();
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    System.out.println("create:templateResolver");
    return templateResolver;
}

以下是我的控制器

@RequestMapping("/")
public class HomeController {
    @RequestMapping(method=GET)
    public String home(){
        System.out.println("control:home");
        return "home";        //view name:home
    }
}}

请帮我看看上面的问题。

2 个答案:

答案 0 :(得分:0)

根据您提供的错误,templateEngine上没有设置解析器。不要传递templateResolver作为参数,只需在Bean方法中创建新实例,如下所示:

@Bean
public ViewResolver viewResolver(){
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    System.out.println("create:viewResolver");
    return viewResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    System.out.println("create:templateEngine");
    return templateEngine;
}

@Bean
public TemplateResolver templateResolver(){
    TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    System.out.println("create:templateResolver");
    return templateResolver;
}
祝你好运。

答案 1 :(得分:0)

您可以更改

@Bean
public TemplateResolver templateResolver(){
    TemplateResolver templateResolver = new TemplateResolver(); // This line
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    System.out.println("create:templateResolver");
    return templateResolver;
}

@Bean
public TemplateResolver templateResolver() {
    TemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    System.out.println("create:templateResolver");
    return templateResolver;
}