我的抵抗頁面作業正常,當點擊“注冊”時,它接受輸入并推送到資料庫。但是,當我單擊轉到“列出專案”頁面時,該頁面同樣在單擊“串列專案”時將輸入推送到資料庫中的表中,它會出現“不支持請求方法'POST'” . 因此,從主登錄選單頁面單擊時,不會出現“列出專案”頁面。
這里是控制器類的一部分:
@GetMapping("/register")
public String goToRegisterPage(Model model) {
User user = new User();
model.addAttribute("user", user);
return "register_page";
}
@GetMapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
FreeItem freeItem = new FreeItem();
model.addAttribute("freeItem", freeItem);
return "create_item_page";
}
此處提供轉到 create_item_page 的 html 檔案的一部分:
<div>
<form th:action="@{/update_user_page}" method="post">
<input type="submit" value="Update account" />
</form>
</div>
<div>
<form th:action="@{/go_to_create_item_page}" method="post">
<input type="submit" value="List an Item" />
</form>
</div>
create_item_page:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Create Item</title>
<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div class="container text-center">
<div>
<h1>Create an Item to list publicly</h1>
</div>
<form th:action="@{/process_created_item}" method="post"
style="max-width: 600px; margin: 0 auto;"
th:object="${freeItem}">
<div class="m-3">
<div class="form-group row">
<label class="col-form-label col-4">Item Name</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{itemName}" required
minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Item Description</label>
<div class="col-8">
<input type="text" class="form-control"
th:field="*{itemDescription}" required
minlength="6" maxlength="10" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">Quantity Of Item</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{quantityOfItem}" required
minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">General Item Location</label>
<div class="col-8">
<input type="text" class="form-control" th:field="*{itemLocation}"
required minlength="2" maxlength="20" />
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-4">E-mail for contact</label>
<div class="col-8">
<input type="email" class="form-control" th:field="*{email}"
required />
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">List Item</button>
</div>
</div>
</form>
</div>
</body>
</html>
以及指定要推送到資料庫的物件的 FreeItem 類。
package com.marinelli.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "free_items")
public class FreeItem {
public FreeItem(String itemName, String itemDescription, String quantityOfItem, String itemLocation, String email) {
super();
this.itemName = itemName;
this.itemDescription = itemDescription;
this.quantityOfItem = quantityOfItem;
this.itemLocation = itemLocation;
this.email = email;
}
public FreeItem() {
}
@Id
@Column(nullable = false, unique = true, length = 64)
private String itemName;
@Column(nullable = false, length = 64)
private String itemDescription;
@Column(nullable = false, length = 25)
private String quantityOfItem;
@Column(nullable = false, length = 25)
private String itemLocation;
@Column(nullable = false, unique = true, length = 45)
private String email;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getQuantityOfItem() {
return quantityOfItem;
}
public void setQuantityOfItem(String quantityOfItem) {
this.quantityOfItem = quantityOfItem;
}
public String getItemLocation() {
return itemLocation;
}
public void setItemLocation(String itemLocation) {
this.itemLocation = itemLocation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "FreeItem [itemName=" itemName ", itemDescription=" itemDescription ", quantityOfItem="
quantityOfItem ", itemLocation=" itemLocation ", email=" email "]";
}
}
uj5u.com熱心網友回復:
你需要創建go_to_create_item_page 和update_user_page作為@Postmapping("....")
@Postmapping("/update_user_page")
public String goToUpdateUserPage(Model model) {
...
...
...
}
@Postmapping("/go_to_create_item_page")
public String goToCreateItemPage(Model model) {
...
...
...
}
uj5u.com熱心網友回復:
該錯誤清楚地表明您對提供的端點使用了不正確的方法。
在您的情況下,/go_to_create_item_page 定義為 GET,您的表單宣告為 method = post。將它們更改為同時使用 POST 或 GET 應該可以解決問題(就 API 設計而言,它應該是 GET,因為您從服務器獲取資源而不是創建/更新)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460482.html
下一篇:Laravel回傳請求內容和回應
