我似乎找不到我的代碼中的錯誤。我所有的匯入都很好。一切似乎都很正常,但是當我點擊添加新按鈕時,我得到了錯誤:"BindingResult和普通目標物件都不能作為請求屬性。此外,如果我在Get和Post Mapping請求中把bean名稱改為products,同時在add檔案中做同樣的修改,它就能作業。
這是我的add.html檔案:
這是我的add.html檔案。
<! doctype html>
<nav th: replace="/fragments/nav :: nav-admin"></nav>
<div class="container">/span>
<h1 class="display-2"/span>> 添加一個產品</h1>>
< a href="/admin/products" class="btn btn-primary mt-4 mb-4" /span>> 回傳產品</a>
<div th:if="${message}"/span> th: text="${message}" th: class="${'alert' alertClass}"></div>
<form method="post" th: object="${product}" th: action="@{/admin/products/add}" enctype="multipart/form-data">
<div th:if="${#fields. hasErrors('*')}" class="alert alert-danger" >
有錯誤
</div>有錯誤。
<div class="form-group"/span>>
<label for="> 名稱</標簽>/span>
< input type="text" class="form-control" th: field="*{name}" placeholder="Name">
<span class="error"/span> th:if="${#fields. hasErrors('name')}" th:errors="*{name}"></span>
</div>/span>
<div class="form-group">
<label for="> 描述</label>。
<textarea th: field="*{description}"/span> rows="10"/span> class="form-control" placeholder="Description"> </textarea>>
<span class="error"/span> th:if="${#fields. hasErrors('description')}" th:errors="*{description}"></span>
</div>/span>
<div class="form-group">
<label for="> Image</label>。
< input type="file" class="form-control" th: name="file" th:id="file">
< img class="mt-2"/span> src="#" alt=" id="imgPreview1">
</div>
<div class="form-group">
<label for="> 價格</label>
< input type="text" class="form-control" th: field="*{price}" placeholder="20或20。 99">
<span class="error"/span> th:if="${#fields. hasErrors('price')}" th:errors="*{price}"></span>
</div>/span>
<div class="form-group">
<label for="> 類別</label>。
<select th: field="*{categoryId}" class="form-control">
<option th: value="0">選擇一個類別</option>/span>
<option th:each="cat: ${categories}" th:value="${cat. id}" th:text="${cat.name}"></option>
</select>/span>
<span class="error"/span> th:if="${#fields. hasErrors('categoryId')}" th:errors="*{categoryId}"></span>
</div>/span>
<button class="btn btn-danger mt-4 mb-4"/span>> 添加</button>。
</form>/span>
</div>/span>
<div th: replace="/fragments/footer"></div>
我的模型名稱是Products.java,這里是GET和POST的控制器代碼,用于添加
。@GetMapping("/add")
public String add(Products product, Model model) {
List<Category> categories = categoryRepo.findAll()。
model.addAttribute("category", categories)。
return "admin/products/add" 。
}
@PostMapping("/add"); }
public String add(@ValidProducts product, BindingResult bindingResult,
MultipartFile file, RedirectAttributes redirectAttributes,
Model模型) throws IOException {
List<Category> categories = categoryRepo.findAll();
if (bindingResult.hasErrors() ) {
model.addAttribute("categories", categories)。
return "admin/products/add" 。
}
boolean fileOK = false;
byte[] bytes = file.getBytes();
String filename = file.getOriginalFilename()。
Path path = Paths.get("src/main/resources/static/media/"/span> filename);
if (filename.endWith("jpg"/span>) || filename.endWith("png"/span>) ) {
fileOK = true;
}
redirectAttributes.addFlashAttribute("message", "Product added")。
redirectAttributes.addFlashAttribute("alertClass", "alert-success") 。
String slug = product.getName().toLowerCase().replace(" ", " -") 。
Products productExists = productRepo.findBySlug(slug)。
if ( ! fileOK ) {
redirectAttributes.addFlashAttribute("message", "Image must be a jpg or a png")。)
redirectAttributes.addFlashAttribute("alertClass", "alert-danger") 。
redirectAttributes.addFlashAttribute("product", product)。
}
else if ( productExists != null ) {
redirectAttributes.addFlashAttribute("message", "Product exists, choose another")。
redirectAttributes.addFlashAttribute("alertClass", "alert-danger") 。
redirectAttributes.addFlashAttribute("product", product)。
} else {
product.setSlug(slug);
product.setImage(filename);
productRepo.save(product)。
Files.write(path, bytes);
}
return "redirect:/admin/products/add"。
}
uj5u.com熱心網友回復:
你需要在獲取映射方法中向模型添加一個名為 "product "的屬性,代表表單。Spring MVC表單教程
@GetMapping("/add"/span>)
public String add(Model模型) {
List<Category> categories = categoryRepo.findAll() 。
model.addAttribute("categories", categories);
model.addAttribute("products", new Products() )。
return "admin/products/add" 。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313441.html
標籤:
