HTTP状态404 –找不到Spring Boot

时间:2020-09-19 08:04:15

标签: spring spring-boot

我正在使用Spring Boot创建简单的Crud系统。当我加载页面时,它正在正常工作。当我添加一个新链接时,它显示为HTTP Status 404 – Not Found Spring Boot,我不明白为什么我所做的一切都是我以现成的方式在屏幕快照中附加了以下代码。

错误屏幕 enter image description here

EmployeeController

@Autowired
private EmployeeService service;

@RequestMapping("/")
public String viewHomePage(Model model) {
    List<Employee> listemployee = service.listAll();
    model.addAttribute("listemployee", listemployee);

    return "index";
}

@RequestMapping(value = "/new")
public String add(Model model) {
    model.addAttribute("employee", new Employee());
    return "new";
}

index.html

<table class="table">
<tbody>
<tr>
    <td>
        <a th:href="@{'/new'}">Add new</a>
    </td>
</tr>
<tr  th:each="employee : ${listemployee}">
    <td th:text="${employee.id}">Employee ID</td>
    <td th:text="${employee.firstName}">FirstName</td>
    <td th:text="${employee.lastName}">LastName</td>

    <td>
        <a th:href="@{'/edit/' + ${employee.id}}">Edit</a>
        &nbsp;&nbsp;&nbsp;
        <a th:href="@{'/delete/' + ${employee.id}}">Delete</a>
    </td>
</tr>
</tbody>
</table>

文件夹结构

enter image description here

porm.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lindaschool?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.error.whitelabel.enabled=false
server.port=7020
spring.datasource.username=root
spring.datasource.password=
#logging.level.root=WARN
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false

1 个答案:

答案 0 :(得分:0)

问题是软件包结构,您没有遵循spring boot和spring框架的标准软件包结构。默认情况下,spring boot应用程序将只扫描主类及其子包中带有任何构造型注释的类。我建议将所有依赖项类移到主类的子包下

com.example.crudbank
  |
  -------------------->CrudBankApplication.java
com.example.crudbank.service
  |
  --------------------->StudentService
com.example.crudbank.controller
  |
  ----------------------> StudentController.java
com.example.crudbank.repository
  |
  -----------------------> StudentRepository.java
com.example.crudbank.domain
  |
  ------------------------>StudentDomain.java