简体   繁体   中英

Spring Boot with Thymeleaf Failing

I am developing an application with spring boot but its failing with Thymeleaf configuration -

    [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)

I get the above error message on hitting a ping service.

SpringBoot Main Class

//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 has below Thymeleaf entry -

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

Spring-context.xml has the below mapping for 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>

After all this, when I hit the service, it gives me an error message -

<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>

Folder structure for web-inf/index.html - 在此处输入图片说明

Can anyone help me with the issue? Any leads would be helpful.

Controller -

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();
}
}

Implementation Class -

@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!!!";
    }
}

Index.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>

I am sorry but you have a complete mess in your application.

You are using spring-boot which one of the best points is that you can get rid of XML configurations, but you are mixing then the web.xml

In any case your mistake is easy, you are returning a view name "works still fails!!!" which doesnt match with any html template.

My advice, have a look to some basic spring-boot tutorial and start from scratch again

Try this one:

Spring Boot web content tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM