为什么html页面没有在百里香中显示出来?

时间:2016-11-24 23:56:14

标签: java spring spring-mvc spring-boot thymeleaf

我正在使用thymeleafWhitelabel Error Page视图引擎。

问题是控制器没有显示正确的html页面并始终显示package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class GifController { @RequestMapping("/greeting") public String sayhello() { return "greets"; } }

控制器:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
Hello!
</body>
</html>

资源/模板路径中的greets.html文件:

@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

也:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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



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



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

和pom.xml:

localhost:8080/greeting

问题出在哪里?当我浏览whitelabel Error page时,它会显示greets.html而不是DECLARE @Table AS TABLE (Client CHAR(6), AMT INT, Date DATE) INSERT INTO @Table VALUES ('ABC Co',250 ,'2016/09/20') ,('ABC Co',250 ,'2016/10/20') ,('CDE Co',200 ,'2016/11/20') ,('CDE Co',200 ,'2016/10/20') ,('CDE Co',-200,'2016/09/20') ,('FGH Co',600 ,'2016/01/01') ,('FGH Co',-500,'2016/09/20') ,('FGH Co',-50 ,'2016/10/20') ,('FGH Co',100 ,'2016/11/20') ,('IJK Co',-100 ,'2016/01/01') ,('IJK Co',-100 ,'2016/09/20') ;WITH cte AS ( SELECT Client ,Date ,AMT ,CurrentBalance = SUM(AMT) OVER (PARTITION BY Client) ,BackwardsRunningTotal = SUM(AMT) OVER (PARTITION BY Client ORDER BY Date DESC) ,CurrentBalanceMinusBackwardsRunningTotal = SUM(AMT) OVER (PARTITION BY Client) - SUM(AMT) OVER (PARTITION BY Client ORDER BY Date DESC) ,DateGroup = CASE WHEN DATEDIFF(day,Date,GETDATE()) BETWEEN 0 AND 29 THEN '0-29days' WHEN DATEDIFF(day,Date,GETDATE()) BETWEEN 30 AND 59 THEN '30-59days' WHEN DATEDIFF(day,Date,GETDATE()) BETWEEN 60 AND 89 THEN '60-89days' WHEN DATEDIFF(day,Date,GETDATE()) >= 90 THEN '90days+' ELSE 'Unknown Error' END ,BalanceAtTime = SUM(AMT) OVER (PARTITION BY Client ORDER BY Date) FROM @Table ) , cteWhenCurrentBalanceIsMet AS ( SELECT Client ,MaxDate = MAX(DATE) FROM cte WHERE CurrentBalanceMinusBackwardsRunningTotal = 0 GROUP BY Client ) , cteAgedDebtPrepared AS ( SELECT c.Client ,Balance = c.CurrentBalance ,c.DateGroup ,Amt = CASE WHEN CurrentBalanceMinusBackwardsRunningTotal = 0 THEN ISNULL(LAG(CurrentBalanceMinusBackwardsRunningTotal) OVER (PARTITION BY c.Client ORDER BY Date DESC),AMT) ELSE AMT END FROM cteWhenCurrentBalanceIsMet m INNER JOIN cte c ON m.Client = c.Client AND m.MaxDate <= c.Date AND SIGN(c.AMT) = SIGN(c.CurrentBalance) ) SELECT * FROM cteAgedDebtPrepared PIVOT ( SUM(Amt) FOR DateGroup IN ([0-29days],[30-59days],[60-89days],[90days+]) ) pvt ORDER BY Client

3 个答案:

答案 0 :(得分:1)

我只是一个弹簧启动学习者,并试图通过解决你的问题来学习它。

我做了一些更改,我创建了一个Config类并在其中添加了一些配置

@Configuration
public class Config {
    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setOrder(1);
        return resolver;
    }
}



@SpringBootApplication
@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

另外我将greet.html页面放在/src/main/webapp/WEB-INF/templates/greet.html中 我想将html / jsp页面放到该位置,您可以使用自己的位置,只需在Config.java类中替换该位置     @Controller     公共类GifController {

    @RequestMapping("/greeting")
    public String sayhello() {
        return "greets";
    }
}

尝试这些更改,看看是否能够在浏览器上看到输出。

答案 1 :(得分:0)

您的Controller和SpringBoot应用程序看起来不错。尝试清理和构建您的项目。当您运行DemoApplication时,您应该在控制台

中看到如下所示
Mapped "{[/greeting]}" onto public java.lang.String com.example.controller.GifController.sayhello()

在你的DemoApplication中你只需要@SpringBootApplication,因为这个注释相当于使用@Configuration,@ EnableAutoConfiguration和@ComponentScan

答案 2 :(得分:0)

我有相同的白标错误。花了我几个小时才弄清楚实际上对百里香的依赖没有得到正确解决。解决方案非常简单而且愚蠢。

  1. 停止tomcat。
  2. 右键单击该项目。
  3. Gradle->刷新gradle项目(或与maven相似的步骤)
  4. 启动tomcat。
  5. 如果这不起作用,请更改build.gradle中下一行的位置(向上或向下移动),然后执行以上步骤。

    compile('org.springframework.boot:spring-boot-starter-thymeleaf')

对于maven,在pom.xml中

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

重要的部分是,当tomcat不运行时,您必须逐步刷新项目。如果使用devtools,我们会盲目地依靠它来重新启动servlet容器和东西,但是有时手动启动停止很方便。