[英]Getting a 400 bad request response when submitting a form in Thymeleaf to my spring boot application
我正在創建一個帶有 thymeleaf 前端的 Spring 啟動應用程序。 我正在嘗試創建一個類型為“expense”的 object(見下面的代碼片段)但是,每當我在正確的路徑上調用 POST 操作時,我的應用程序都會收到 400 錯誤請求錯誤消息和“錯誤在控制台中的模板解析期間發生(模板:“ServletContext 資源 [/WEB-INF/views/error.html]”)”。
下面的實體表示我嘗試使用表單創建的數據類型。 要創建這個 object,只需要變量 expenses_id、expenses_name 和 expenses_date。 我能夠在其余字段中創建具有 null 值的這些對象之一。
@Entity(name = "expenses")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Expenses {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long expenses_id;
private String expenses_name;
private Integer expenses_amount;
private Date expenses_date;
@ManyToOne
@JoinTable(
name = "budget_expenses",
joinColumns = @JoinColumn(name = "expenses_id"),
inverseJoinColumns = @JoinColumn(name = "budgets_id"))
private Budgets budget;
//Gettes setters and constructor
}
下面的代碼片段顯示了帶有我的表單的模板
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Expense Creation</title>
</head>
<body>
<div class="container">
<div class="container">
<div th:insert="fragments/navbar.html"> </div>
<div class="jumbotron">
<div class="row">
<h3>Expense Creation</h3>
</div>
<form action="#" th:action="@{/budgets/{id}/add-expense(id = ${budgetId})}" th:object="${expense}" method="post">
<p>Name: <input type="text" th:field="*{expenses_name}" /></p>
<p>Amount: <input type="number" th:field="*{expenses_amount}" /></p>
<p>Date: <input type="text" th:field="*{expenses_date}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</div>
</body>
</html>
下面的代碼片段包含 GET 和 POST 操作。 GET 設法為相關表單提供正確的視圖模板。 問題來自 POST 映射。 一旦我點擊“提交”,錯誤就會發生。 我已經做了很多調試,我認為問題出在 @ModelAttribute 上,主要是因為它是隨時破壞應用程序的組件,我只是不明白如何糾正它。 我已經驗證了路徑並確保 GET 和 POST 正在對正確的映射進行。 我已經驗證了變量以確保它們具有正確的名稱和對應關系。 我不知道為什么會發生這種情況,更是如此,因為我有一個類似的創建通過動態表單和另一個 object 僅在此代碼上方幾行。
@GetMapping("{id}/add-expense")
public String expensesForm(Model model, @PathVariable Long id){
model.addAttribute("expense", new Expenses());
model.addAttribute("budgetId", id);
return "expenseForm";
}
@PostMapping("{id}/add-expense")
public String expenseSubmitted(@ModelAttribute Expenses expense, @PathVariable Long id, Model model){
Budgets budget = budgetsRepository.getById(id);
if(budget != null){
budget.addExpense(expense);
expense.setBudget(budget);
expensesRepository.saveAndFlush(expense);
}
else{
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "not all variables there");
}
model.addAttribute("expense", expense);
return "expenseResult";
}
如果有人能夠找到我所缺少的東西,我將不勝感激。
編輯:我在堆棧跟蹤中看到了這個:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/views/error.html]
這很奇怪,因為我沒有錯誤。html。 我試圖將主頁重命名為“error.html”,它不再給我 400 響應,即使整體行為仍然是錯誤的。
您的HTML模板中的object名稱有誤:
th:object="${expense}"
你在哪里使用class object:
public class Expenses
將您的 HTML expense
更改為expenses
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.