簡體   English   中英

Thymeleaf失敗的Spring Boot

[英]Spring Boot with Thymeleaf Failing

我正在使用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!!!";
    }
}

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>

很抱歉,您的應用程序一團糟。

您正在使用spring-boot,最好的方法之一是可以擺脫XML配置,但要混合使用web.xml

無論如何,您的錯誤都很容易,您將返回一個視圖名稱“作品仍然失敗!!!” 與任何html模板都不匹配。

我的建議是,看看一些基本的spring-boot教程,然后從頭開始。

試試這個:

Spring Boot Web內容教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM