[英]Redirect page in Spring Boot with Jpa and Thymeleaf
我正在創建一個一對多的關系,其中類別有許多產品。 在列出我現有的所有類別后,我想查看特定類別中的所有產品並對這些產品(來自該類別)進行基本的 CRUD。 我發現在 Thymaleaf 中重定向頁面有困難,最確切地說,我無法使用 Thymeleaf 訪問“添加產品”頁面。
@Entity
@Table(name="category")
public class Category {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column
@NotEmpty(message = "*Please provide a description")
private String description;
@OneToMany(mappedBy = "category")
private Set<Product> products;
public Category(){}
@Entity
@Table(name="product")
public class Product {
@Id
@Column(name="id")
private String id;
@Column
private String name;
@Column
private String description;
@Column
private float price;
@Column
private int discount;
@Column
private boolean isInStock;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
這是我在服務層中保存產品的方式
public Product save(Product product, long id) {
Category category = categoryRepository.getById(id);
if (category != null) {
product.setId(UUID.randomUUID().toString());
product.setCategory(category);
product.setInStock(false);
return productRepository.save(product);
} else {
throw new ResourceNotFoundException("Category not found with id: " + id);
}
}
在 controller 我有:
@Autowired
ProductService productService;
@GetMapping("/products/{categoryId}")
public String viewProducts(Model model, @PathVariable("categoryId") long categoryId) {
List<Product> productsList = productService.findByCategoryId(categoryId);
model.addAttribute("productsList", productsList);
model.addAttribute("categoryId",categoryId );
return "products";
}
@GetMapping("/newProduct/{categoryId}")
public String showAddForm(Model model,@PathVariable("categoryId") long categoryId) {
Product product = new Product();
model.addAttribute("product", product);
return "newProduct";
}
@PostMapping("/save/{categoryId}")
public String addProduct(@ModelAttribute("product") Product product, @PathVariable long categoryId){
productService.save(product, categoryId);
return "redirect:products";
}
Where I am stuck is that I can't redirect from products.html to newProduct.html (that showAddForm method should return) using the path variable categoryId.It redirects me to an error page where if I enter the categoryId by hand in the URL ,我終於找到了新產品。html。 在 products.html 中,我有:
<body>
<div class="container my-2">
<div class="card">
<div class="card-body">
<!-- TODO get category ID from categories (parameters between HTML)-->
<div th:switch="${productsList}" class="container my-5">
<p class="my-5">
<a href="/product/newProduct/" class="btn btn-primary">Add product</a>
</p>
<div class="col-md-10">
<h2 th:case="null">No Products yet!</h2>
<div th:case="*">
<table class="table table-striped table-responsive-md">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Discount</th>
<th>IsInStock</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${productsList}">
<td th:text="${product.name}"></td>
<td th:text="${product.description}"></td>
<td th:text="${product.discount}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.isInStock}"></td>
<td><a th:href="@{/product/edit/{id}(id=${product.id})}" class="btn btn-primary"></a></td>
<td><a th:href="@{/product/delete/{id}(id=${product.id})}" class="btn btn-primary"></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我看到的問題是,您的帶有路徑@GetMapping("/newProduct/{categoryId}")
的showAddForm
代碼需要一個 catgoryID,它沒有在上面的添加產品按鈕中發送。 由於您在model.addAttribute("categoryId",categoryId );
您可以在 href 元素中使用它。 因此,該鏈接應該類似於
<p class="my-5">
<a th:href="@{/product/newProduct(categoryId=${categoryId})}" class="btn btn-primary">Add product</a>
</p>
但是由於在方法showAddForm
中沒有使用 categoryID,所以您可以刪除它。 並嘗試使用類似的東西
<p class="my-5">
<a th:href="@{/product/newProduct}" class="btn btn-primary">Add product</a>
</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.