прежде чем я попытался вставить данные в MySQL базу данных с использованием Spring Boot (я добавил данные в таблицу), страница отображалась правильно. Однако после попытки добавить данные на странице отображается страница ошибки Whitelabel.
вот какая ошибка, которую я получил:
Код: Выделить всё
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.
Я действительно в замешательстве.
Вот моя структура каталогов:
Код: Выделить всё
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
ProductsController
Код: Выделить всё
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
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 {
}
Код: Выделить всё
Bootstrap demo
welcome to our website
[url=/templates/products]products[/url]
Код: Выделить всё
Best Store
Products
[url=/templates/products/create]Create Product[/url]
ID
Name
Brand
Category
Price
Image
Created
Action
[img]https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js[/img]
Код: Выделить всё
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
Код: Выделить всё
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.2.10
com.boostmytool
bestStore
0.0.1-SNAPSHOT
bestStore
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-devtools
runtime
true
com.mysql
mysql-connector-j
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
Подробнее здесь: https://stackoverflow.com/questions/790 ... ces-static