NoResourceFoundException:没有静态资源,但它存在于 resources/static

问题描述 投票:0回答:1

我最近一直在练习使用 Thymeleaf 和 Spring Boot 并连接到 MySQL 数据库。但是,我遇到了一些问题。

在我尝试使用 Spring Boot 将数据插入 MySQL 数据库之前(我将数据添加到表中),页面显示正确。但是,尝试添加数据后,页面显示白标错误页面。

这是我得到的错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Oct 05 23:04:04 CST 2024
There was an unexpected error (type=Not Found. status=404).
No static resource templates/products/create.
org.springframework.web.servdet.resource.NoResourceFoundException: No static resource templates/products/create.

我使用XAMPP管理MySQL,不知道是不是端口问题?

我真的感觉很混乱。

这是我的目录结构:

src
└── main
    ├── java
    │   └── com
    │       └── boostnytool
    │           └── bestStore
    │               ├── controllers
    │               │   └── ProductsController.java
    │               ├── models
    │               │   └── Product.java
    │               └── services
    │                   └── ProductRepository.java
    │               └── BestStoreApplication.java
    └── resources
        ├── static
        │   └── images
        │       ├── 11736965.jpg
        │       ├── 57380538.jpg
        │       ├── 80522267.jpg
        │       └── 97815739.jpg
        ├── index_first.html
        └── templates
            └── products
                └── index.html
        └── application.properties

这是我的代码:

产品控制器


package com.boostmytool.bestStore.controllers;

import com.boostmytool.bestStore.models.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.boostmytool.bestStore.services.ProductRepository;

import java.util.List;

@Controller
@RequestMapping("/products")
public class ProductsController {
    @Autowired
    private ProductRepository repo;

    @GetMapping({"","/"})
    public  String showProductList(Model model){
        List<Product> products = repo.findAll();
        // 打印產品列表到控制台
        System.out.println(products);

        // 打印每個產品的 imageFileName 以進一步檢查
        for (Product product : products) {
            System.out.println("Product ID: " + product.getId() + ", Image File Name: " + product.getImageFileName());
        }
        model.addAttribute("products",products);
        return "products/index";
    }
}

产品

package com.boostmytool.bestStore.models;

import jakarta.persistence.*;

import java.util.Date;

@Entity
@Table(name = "templates/products")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;
    private String brand;
    private String category;
    private double price;

    @Column(columnDefinition = "TEXT")
    private String description;
    private Date createdAt;
    private String imageFileName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }
}

产品存储库

package com.boostmytool.bestStore.services;

import com.boostmytool.bestStore.models.Product;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProductRepository extends JpaRepository<Product,Integer> {
}

index_first.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <h1 class="text-center my-4">welcome to our website </h1>

    <a class="btn btn-primary" href="/templates/products">products</a>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>
</html>

index.html

<!doctype html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Best Store</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <h1 class="text-center my-4">Products</h1>
    <a class="btn btn-primary" href="/templates/products/create">Create Product</a>
    <table class="table">
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Brand</th>
            <th>Category</th>
            <th>Price</th>
            <th>Image</th>
            <th>Created</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="product : ${products}">
            <td th:text="${product.id}"></td>
            <td th:text="${product.name}"></td>
            <td th:text="${product.brand}"></td>
            <td th:text="${product.category}"></td>
            <td th:text="${product.price} + '$'"></td>
            <td>
                <img th:src="@{/images/{fileName}(fileName=${product.imageFileName})}" alt="Product Image" width="100">
            </td>
            <td th:text="${product.createdAt.toString().substring(0, 10)}"></td>
            <td style="white-space: nowrap">
                <a class="btn btn-primary btn-sm" th:href="@{/products/edit(id=${product.id})}">Edit</a>
                <a class="btn btn-danger btn-sm" th:href="@{/products/delete(id=${product.id})}" onclick="return confirm('Are you sure?')">Delete</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

应用程序.属性

spring.application.name=bestStore

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/beststore
spring.datasource.username=root
spring.datasource.password=springboot
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true

pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.boostmytool</groupId>
    <artifactId>bestStore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bestStore</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <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-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- sql -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</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>

我试图找出连接名称是否有拼写错误,并且还检查了application.properties文件,但问题没有解决。

java spring-boot
1个回答
0
投票

错误显示“没有静态资源模板/产品/创建。” 我认为您请求了 http://localhost/products/create。 您映射的 url 是 http://localhost/products 或 http://localhost/products/ 您必须在 http://localhost/products 或 http://localhost/products/ 上请求

© www.soinside.com 2019 - 2024. All rights reserved.