Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么html页面没有';你不会在百里香中看到吗?_Java_Spring_Spring Mvc_Spring Boot_Thymeleaf - Fatal编程技术网

Java 为什么html页面没有';你不会在百里香中看到吗?

Java 为什么html页面没有';你不会在百里香中看到吗?,java,spring,spring-mvc,spring-boot,thymeleaf,Java,Spring,Spring Mvc,Spring Boot,Thymeleaf,我正在使用springboot和thymeleaf查看引擎 问题是控制器没有显示正确的html页面,并且总是显示白标签错误页面 控制员: package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class GifContr

我正在使用
springboot
thymeleaf
查看引擎

问题是控制器没有显示正确的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";
    }
}
参考资料/模板路径中的greets.html文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
Hello!
</body>
</html>
以及pom.xml:

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

4.0.0
com.example
演示
0.0.1-快照
罐子
演示
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.4.2.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
弹簧启动装置
org.springframework.boot
springbootmaven插件

问题出在哪里?当我浏览到
localhost:8080/greeting
时,它显示的是
whitelabel错误页面
,而不是
greets.html

,我只是一个spring boot学习者,试图通过解决您的问题来学习它

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

@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类中替换那个位置 @控制器 公共类控制器{

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

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

您的控制器和SpringBoot应用程序看起来正常。尝试清理并生成项目。当您运行DemoApplication时,您应该在控制台中看到如下所示

Mapped "{[/greeting]}" onto public java.lang.String com.example.controller.GifController.sayhello()

在您的DemoApplication中,您只需要@SpringBootApplication,因为此注释相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan。我遇到了相同的白标签错误。我花了几个小时才发现实际上thymeleaf依赖关系没有得到正确解决。解决方案非常简单既简单又愚蠢

  • 阻止雄猫
  • 右键单击项目
  • Gradle->刷新Gradle项目(或maven的类似步骤)
  • 启动tomcat
  • 如果这不起作用,请更改build.gradle中下线的位置(上移或下移),然后执行上述步骤

    编译('org.springframework.boot:springbootstartermyleaf')

  • 对于maven,在pom.xml中

    
    org.springframework.boot
    弹簧启动装置
    


    重要的一点是,在tomcat未运行时,您必须逐步刷新项目。如果我们使用devtools,我们会盲目地依赖它来重新启动servlet容器和内容,但有时手动启动-停止会很方便。

    我测试了您的代码,它对我有效。请尝试在templates文件夹中添加index.html,并在中再添加一个方法类似于@RequestMapping(“/”)的公共字符串hello(){return“index”;}。尝试清理、打包项目并测试它。我遇到了同样的问题,您解决了吗?您可以共享导入或共享完整的文件吗?
    Mapped "{[/greeting]}" onto public java.lang.String com.example.controller.GifController.sayhello()