在胸腺启动申请中提交表格时获得400不良请求的响应

发布于 2025-01-28 08:28:42 字数 4192 浏览 4 评论 0原文

我正在创建一个带有百里叶前端的春季启动应用程序。我正在尝试创建一个类型的“费用”对象(在下面的代码片段中看到),但是,每当我在正确路径上调用邮政操作时,我就会在我的应用程序上给我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和邮政操作。 GET设法提供了有关表格的正确视图模板。问题来自帖子映射。一旦我单击“提交”错误,就会发生错误。我进行了很多调试,我相信问题在于@ModelAttribute主要是因为它随时都会破坏应用程序,我只是不明白如何纠正它。我已经验证了路径,并确保完成了正确的映射。我已经验证了变量,以确保它们具有正确的名称和信件。我对为什么会发生这种情况感到茫然,这更是如此,这是因为我通过表单动态具有类似的创建,而另一个对象则在此代码上方几行。

@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]

这很奇怪,因为我没有错误。我试图将HOHING页面重命名为“ error.html”,即使整体行为仍然是错误的,它也不再给我400响应。

I am creating a Spring boot application with a thymeleaf front-end. I am trying to create an object of type "expense" (seen in the code snippet below) but, whenever I call the POST operation on the correct path, I am given a 400 bad request errors message on my app and a "An error happened during template parsing (template: "ServletContext resource [/WEB-INF/views/error.html]")" in the console.

The entity below represents the data type I am trying to create using the form. To create this object, only the variables expenses_id, expenses_name and expenses_date are required. I was able to create one of these objects with null values in the remaining fields.

@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
}

The snippet below shows the template with my form

<!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>

The snippet below contains the GET and POST operations. The GET manages to provide the correct view template with the form in question. The problem comes from the POST mapping. As soon as I click "submit" the errors happen. I have done quite a lot of debugging and I believe that the problem is with the @ModelAttribute mainly because it is the component which breaks the application anytime it is present, I just don't understand how to correct it. I have verified the paths and made sure that the GET and POST are being done to the correct mapping. I have verified the variables to make sure they have the correct names and correspondence. I am at a loss as to why this is happening, even more so due to the fact that I have a similar creation via form dynamic with another object just a few lines above this code.

@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";
}

If someone would be able to find what I am missing, I'd much appreciate it.

EDIT:
I saw this in the stack trace:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/views/error.html]

Which is strange since I do not have a error.html. I tried to rename the homing page to "error.html" and it stopped giving me the 400 response even though the overall behaviour is still wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-02-04 08:28:42

HTML模板中对象的名称存在错误:

th:object="${expense}"

您在哪里使用类对象:

public class Expenses

更改您的html 费用 oppeense

there is an error in the name of the object in your HTML template:

th:object="${expense}"

where do you use the class object:

public class Expenses

change in you HTML expense to expenses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文