在Thymeleaf中向我的spring boot应用程序提交表单时获得400个坏请求响应



我正在创建一个带有thymleaf前端的Spring启动应用程序。我要创建一个类型为&;expense&;的对象(在下面的代码片段中看到)但是,每当我在正确的路径上调用POST操作时,我就会在我的应用程序上得到400个错误的请求错误消息和"模板解析期间发生错误(模板:"ServletContext resource [/WEB-INF/views/error.html]")">

下面的实体表示我试图使用表单创建的数据类型。要创建这个对象,只需要变量expenses_id、expenses_name和expenses_date。我能够在其余字段中创建一个具有空值的对象。

@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操作。我已经验证了变量,以确保它们有正确的名称和对应关系。我对为什么会发生这种情况感到茫然,更重要的是,由于我有一个类似的创建,通过表单动态与另一个对象,就在这段代码的几行以上。

@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模板中对象的名称有错误:

th:object="${expense}"

在哪里使用类对象:

public class Expenses

更改你的HTMLexpenseexpenses

相关内容

  • 没有找到相关文章

最新更新