Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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/6/eclipse/8.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 Spring引导白标签错误页面(类型=未找到,状态=404)_Java_Eclipse_Spring Boot_Thymeleaf - Fatal编程技术网

Java Spring引导白标签错误页面(类型=未找到,状态=404)

Java Spring引导白标签错误页面(类型=未找到,状态=404),java,eclipse,spring-boot,thymeleaf,Java,Eclipse,Spring Boot,Thymeleaf,下午好! 我正在开始春季研究,我以同样的方式遵循教程,但它返回一个错误: 文件夹结构: 奇怪的是: 如果我将“EventoController.java”插入br.com.SpringApp.SpringApp,它将正常工作 package br.com.SpringApp.SpringApp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.S

下午好! 我正在开始春季研究,我以同样的方式遵循教程,但它返回一个错误:

文件夹结构:

奇怪的是: 如果我将“EventoController.java”插入br.com.SpringApp.SpringApp,它将正常工作

package br.com.SpringApp.SpringApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAppApplication {

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


根据请求,我正在添加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>br.com.SpringApp</groupId>
    <artifactId>SpringApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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


   </project>

4.0.0
br.com.SpringApp
SpringApp
0.0.1-快照
罐子
SpringApp
SpringBoot的演示项目
org.springframework.boot
spring启动程序父级
1.5.10.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴开发工具
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件

有人能告诉我哪里错了吗?

请验证您的pom.xml中是否有正确的thymeleaf依赖项:

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

org.springframework.boot
弹簧启动装置

确保主类位于其他类之上的根包中

当您运行Spring引导应用程序(即用@SpringBootApplication注释的类)时,Spring将只扫描主类包下面的类

你的声明是这样的

包br.com.SpringApp.SpringApp
在这个主要类中,即SpringAppApplication

包br.com.SpringApp.SpringApp.controller
控制器的名称,即EventoController和IndexController

包br.com.SpringApp.SpringApp.model
您的型号名称,即Evento

此后
清理项目并重新运行spring boot应用程序

正如Deepak所回答的,主类应该位于根包中,而不是其他包中。 但如果您不想这样做,可以使用:

@SpringBootApplication(scanBasePackages = {"com.other.packages","com.other"})
public class SpringAppApplication {
.....
}
解决方案: 如果在控制器类上使用
@Controller
,则它将被视为MVC控制器类。但是,如果您希望在RESTFul web服务中使用一个特殊的控制器,那么您可以将
@controller
@ResponseBody
注释一起使用,也可以直接在
controller
类上使用
@RestController
。在使用RESTfulWebServices创建SpringBoot项目时,我遇到了同样的错误,这对我来说很有效

package br.com.SpringApp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class EventoController {

    @RequestMapping("/cadastroEvento")
    @ResponseBody
    public String form() {      
        return "evento/formEvento"; 
    }

}
或:


作为SpringBoot的初学者,任何人都可能至少发生一次此错误,因为有几个原因

  • 应用程序类(例如:SpringAppApplication.java)应该在根级别,控制器类可以放在同一级别下,也可以放在子目录中。此应用程序类应使用
    @SpringBootApplication
    扩展SpringBootApplication
  • 您可能会错过控制器类顶部的
    @RestController
    注释
  • 或者可能会错过映射注释。(
    @PostMapping(“/save”)
    @GetMapping(“/id”)
    等)
  • 最后,只需检查您是否输入了正确的请求映射地址(URL),您可能会错过斜杠或
    @RequestMapping(“/customer”)
    (如果控制器类中有注释)或URL中的拼写错误

  • 你能发布你的pom.xml吗?在下面提到的层次结构包br.com.SpringApp.SpringApp.contorller中声明你的控制器;就像您需要为模型声明一样,它也将解决您所要求的问题,我正在添加pom.xml谢谢!问题恰恰是这个。。。对不起,我有个愚蠢的疑问。。。
    package br.com.SpringApp.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class EventoController {
    
        @RequestMapping("/cadastroEvento")
        @ResponseBody
        public String form() {      
            return "evento/formEvento"; 
        }
    
    }
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class EventoController {
    
        @RequestMapping("/cadastroEvento")
        public String form() {      
            return "evento/formEvento"; 
        }
    
    }