【问题标题】:Spring Boot with Thymeleaf FailingThymeleaf 失败的 Spring Boot
【发布时间】:2017-07-10 16:47:43
【问题描述】:

我正在使用 Spring Boot 开发一个应用程序,但由于 Thymeleaf 配置失败 -

    [THYMELEAF][http-nio-8080-exec-4] Exception processing template "Works still fails!!!": Error resolving template "Works still fails!!!", template might not exist or might not be accessible by any of the configured Template Resolvers
Feb 21, 2017 12:37:47 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/ursserver] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Works still fails!!!", 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 "Works still fails!!!", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)

我在点击 ping 服务时收到上述错误消息。

SpringBoot 主类

//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.shc.ecom.urs.convertors.URSServiceMessageConvertor;

//import org.springframework.context.annotation.PropertySource;
import org.springframework.boot.Banner;


/**
 * @author bnarula
 *
 */
@SpringBootApplication
@Import({CassandraDsConfig.class})
@ImportResource({"spring-urs-context.xml"/*, "cassandra-dce.xml","cassandra.xml"*/ })
//@Import(CassandraDsConfig.class)
@PropertySource({ "urs-application.properties", "cassandra.properties"})
/*@PropertySources(value = { @PropertySource("cassandra-dce.xml"), @PropertySource("cassandra.properties"), 
        @PropertySource("cassandra.xml")})*/
//@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages = "com.shc.ecom")
//@EnableWebMvc
public class URSMainModule extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return configureApplication(builder);
    }

    public static void main(String[] args) {
        configureApplication(new SpringApplicationBuilder()).run(args);
    }

    private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
        return builder.sources(URSMainModule.class).bannerMode(Banner.Mode.OFF);
    }

    /*@Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }*/
}

Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.shc.marketplace.ias.service.MessagingApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

Pom 有以下 Thymeleaf 条目 -

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Spring-context.xml 具有以下 Thmeleaf 映射 -

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5" />
    </bean>

毕竟,当我点击服务时,它会给我一条错误消息 -

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Tue Feb 21 12:39:03 IST 2017</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Error resolving template &quot;Works still fails!!!&quot;, template might not exist or might not be accessible by any of the configured Template Resolvers</div></body></html>

web-inf/index.html 的文件夹结构 -

谁能帮我解决这个问题?任何线索都会有所帮助。

控制器 -

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

import com.shc.ecom.urs.binder.ReservationRequest;

import org.jboss.resteasy.annotations.providers.jaxb.json.Mapped;
import org.jboss.resteasy.annotations.providers.jaxb.json.XmlNsMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 


@RequestMapping("/inventory")
public interface ReservationService {

@RequestMapping(value = "/ping", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN)
    public String testWebService();
}
}

实现类-

@Component
public class ReservationServiceImpl implements ReservationService{  
    public String testWebService() {
        final String METHOD_NAME = "testWebService";
        logger.info(" Inside " + METHOD_NAME + "Request valid");
        return "Works still fails!!!";
    }
}

索引.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head lang="en">

<title>Spring Framework Guru</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Hello</h1>

    <h2>Fellow Spring Framework Gurus!!!</h2>
</body>
</html>

【问题讨论】:

  • 查看异常,看起来 thymeleaf 引擎正在寻找名称为“Works still failed!!!”的模板文件所以它失败了。确保将正确的模板名称传递给解析器
  • 删除你的整个配置,包括WEB-INF,将index.html移动到src/main/resources/templates/并返回字符串"index"(你返回"Works still fails!!!", when there's no “Works仍然失败.html”` )。
  • 向我们展示您的控制器。您返回什么视图名称?你有 src/main/resources/templates/viewname.html 文件吗?
  • @mhasan 实际上,这不是我的服务返回的消息。它失败了,因为它根本找不到模板。不知道为什么会这样。
  • 简单地说,你从@RequestMapping 方法返回视图名称,然后 thymleaf 在classpath:/templates/&lt;return&gt;.html 中查找它,你可以将模板放入 src/main/resources

标签: java rest maven spring-boot thymeleaf


【解决方案1】:

很抱歉,您的申请一团糟。

您正在使用 spring-boot,其中一个优点是您可以摆脱 XML 配置,但是您正在混合 web.xml

无论如何,您的错误很容易,您返回的视图名称“仍然失败!!!”与任何 html 模板都不匹配。

我的建议,看看一些基本的 spring-boot 教程,然后从头开始

试试这个:

Spring Boot web content tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 2019-06-28
    • 2017-03-12
    • 1970-01-01
    • 2016-07-31
    • 2019-06-24
    • 2020-01-26
    • 2018-05-29
    相关资源
    最近更新 更多