title: Thymeleaf is trying to build a html which does not exist and which is not called anywhere
body: <p>I am trying to build a spring mvc app with spring boot and thymeleaf. I am trying to create a form to insert an object into my data base (something which I already do in my app and is working) but I constantly find this error:</p>
  <pre><code>org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: &quot;ServletContext resource [/WEB-INF/views/error.html]&quot;)
  (...)
  Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/views/error.html]
  (...)
  </code></pre>
  <p>This is the problem you usually find when you are trying to reference a template which does not exist or has a different name. <strong>Here's the thing</strong>. There is no Error.html in my views' folder as you can see in the print below:</p>
  <p><a href="https://i.stack.imgur.com/uMLy8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uMLy8.png" alt="enter image description here" /></a></p>
  <p>Can anyone help me understand what's going on?</p>
  <p><strong>EDIT with more information:</strong></p>
  <p>Below you can find the endpoints which are giving me trouble:</p>
  <pre><code>@GetMapping(&quot;{id}/add-expense&quot;)
  public String expensesForm(Model model, @PathVariable Long id){
     model.addAttribute(&quot;expense&quot;, new Expenses());
     model.addAttribute(&quot;budgetId&quot;, id);
     return &quot;expenseForm&quot;;
  }
  
  @PostMapping(&quot;{id}/add-expense&quot;)
  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, &quot;not all variables there&quot;);
     }
     model.addAttribute(&quot;expense&quot;, expense);
     return &quot;expenseResult&quot;;
  }
  </code></pre>
  <p>The GET endpoint seems to be working as intended. When I load the &quot;id/add-expense&quot; path it displays the page &quot;expenseForm&quot; correctly. The problem comes when I try to submit the form in the &quot;expenseForm&quot; template and go to the POST for &quot;id/add-expenses&quot;. That is when I get a 400 bad request response. I am lead to believe that the problem might be that the &quot;expenseForm&quot; cannot correctly populate the @ModelAttribute in the POST method but I can't say why or how to fix it. In the console, I am given the error above which tells me that Thymeleaf cannot find the template for &quot;error.html&quot;.</p>
  <p>expenseForm.html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
  
    &lt;title&gt;Expense Creation&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div th:insert=&quot;fragments/navbar.html&quot;&gt; &lt;/div&gt;
  
      &lt;div class=&quot;jumbotron&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
          &lt;h3&gt;Expense Creation&lt;/h3&gt;
        &lt;/div&gt;
        &lt;form action=&quot;#&quot; th:action=&quot;@{/budgets/{id}/add-expense(id = ${budgetId})}&quot; th:object=&quot;${expense}&quot; method=&quot;post&quot;&gt;
          &lt;p&gt;Name: &lt;input type=&quot;text&quot; th:field=&quot;*{expenses_name}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;Amount: &lt;input type=&quot;number&quot; th:field=&quot;*{expenses_amount}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;Date: &lt;input type=&quot;text&quot; th:field=&quot;*{expenses_date}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt; &lt;input type=&quot;reset&quot; value=&quot;Reset&quot; /&gt;&lt;/p&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  <p>expenseResult.html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
  
      &lt;title&gt;Expense Creation&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div th:insert=&quot;fragments/navbar.html&quot;&gt; &lt;/div&gt;
  
          &lt;div class=&quot;jumbotron&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;h3&gt;Expense Created&lt;/h3&gt;
              &lt;/div&gt;
              &lt;p th:text=&quot;'Name: ' + ${expense.expenses_name}&quot; /&gt;
              &lt;p th:text=&quot;'Amount: ' + ${expense.expenses_amount}&quot; /&gt;
              &lt;p th:text=&quot;'Date: ' + ${expense.expenses_date}&quot; /&gt;
              &lt;a href=&quot;expenses&quot;&gt;Submit another expense&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;js/budgetScript.js&quot;&gt;&lt;/script&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Class Expenses:</p>
  <pre><code>@Entity(name = &quot;expenses&quot;)
  @JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
  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 = &quot;budget_expenses&quot;,
              joinColumns = @JoinColumn(name = &quot;expenses_id&quot;),
              inverseJoinColumns = @JoinColumn(name = &quot;budgets_id&quot;))
      private Budgets budget;
  
      //Gettes setters and constructor
  }
  </code></pre>
  <p>Thank you in advance.</p>
  <p><strong>EDIT 2 after changing the name of &quot;expenseResult.html&quot; to &quot;error.html&quot;:</strong></p>
  <p>So I decided to follow the tip from <a href="https://stackoverflow.com/users/12567365/andrewjames">andrewJames</a> and I changed the name of &quot;expenseResult.html&quot; to &quot;error.html&quot;. I also changed the return value in the POST method from 'return &quot;expenseResult&quot;' to 'return &quot;error&quot;' and I am no longer given the 400 bad request error on the application. Instead, I am given the &quot;error.html&quot; page but the fields for expense.expenses_name, expense.expenses_amount or expense.expenses_date are not shown in the page. It is just an empty page with the &quot;Expense Creation&quot; text. In the console I receive a new error:</p>
  <pre><code>org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: &quot;expense.expenses_name&quot; (template: &quot;error&quot; - line 19, col 16)
  (...)
  Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'expenses_name' cannot be found on null
  (...)
  </code></pre>
  <p>From what we can see, the application cannot retrieve the fields from null, meaning that the @ModelAttribute Expenses expense is not returning the correct values from the form page. It more or less confirms my suspicion that the problem was in the modelAttribute but I continue to not know how to correct the problem since I have tried to verify multiple times if everything was okay and I found no issue. I also don't understand why it is looking for a &quot;error.html&quot; file considering that I had no prior mention to such a file in my code and project.</p>
  <p><strong>EDIT 3 after using ModelandView intead of ModelAttribute:</strong></p>
  <p>Following <a href="https://stackoverflow.com/users/1119161/dsp-user">dsp_user</a>'s suggestion. I started by hardcoding the path with &quot;/budgets/10/add-expense&quot; instead of using {id} and the problem remained, which makes me believe that the problem is not the path.</p>
  <p>I started using the ModelAndView, as can be shown below, but I continued to have the same problem as before (400 bad request).</p>
  <pre><code>@PostMapping(&quot;{id}/add-expense&quot;)
      public ModelAndView expenseSubmitted(@ModelAttribute(&quot;expenses&quot;) Expenses expenses, @PathVariable Long id, Model model){
          System.out.println(&quot;here&quot;);
          Budgets budget = budgetsRepository.getById(id);
          if(budget != null){
              budget.addExpense(expenses);
              expenses.setBudget(budget);
              expensesRepository.saveAndFlush(expenses);
          }
          else{
              throw new ResponseStatusException(HttpStatus.NOT_FOUND, &quot;not all variables there&quot;);
          }
          ModelAndView modelAndView = new ModelAndView(&quot;expenseResult&quot;);
          modelAndView.addObject(&quot;expense&quot;, expenses);
          return modelAndView;
      }
  </code></pre>
  <p><strong>EDIT 4 to add the Budgets logic:</strong></p>
  <p>Budgets:</p>
  <pre><code>@Entity(name = &quot;budgets&quot;)
  @JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
  public class Budgets {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long budgets_id;
      private String budgets_name;
      private String budgets_description;
      private Integer budgets_amount;
  
      @OneToMany(mappedBy = &quot;budget&quot;)
      @JsonIgnore
      private List&lt;Expenses&gt; expenses;
  
      @Transient
      public float budgets_expense_sum = 0;
  
  //Gettes setters and constructor
  }
  </code></pre>
  <p>Budget Creation Endpoints:</p>
  <pre><code>@GetMapping(&quot;new&quot;)
      public String budgetsForm(Model model){
          model.addAttribute(&quot;budget&quot;, new Budgets());
          return &quot;budgetForm&quot;;
      }
  
      @PostMapping(&quot;new&quot;)
      public String budgetSubmitted(@ModelAttribute Budgets budget, Model model){
          budgetsRepository.saveAndFlush(budget);
          model.addAttribute(&quot;budget&quot;, budget);
          return &quot;budgetResult&quot;;
  </code></pre>
  <p>BudgetForm.html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
  
    &lt;title&gt;Budget Creation&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div th:insert=&quot;fragments/navbar.html&quot;&gt; &lt;/div&gt;
  
      &lt;div class=&quot;jumbotron&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
          &lt;h3&gt;Budget Creation&lt;/h3&gt;
        &lt;/div&gt;
        &lt;form action=&quot;#&quot; th:action=&quot;@{/budgets/new}&quot; th:object=&quot;${budget}&quot; method=&quot;post&quot;&gt;
          &lt;p&gt;Name: &lt;input type=&quot;text&quot; th:field=&quot;*{budgets_name}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;Description: &lt;input type=&quot;text&quot; th:field=&quot;*{budgets_description}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;Monthly Amount: &lt;input type=&quot;number&quot; th:field=&quot;*{budgets_amount}&quot; /&gt;&lt;/p&gt;
          &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt; &lt;input type=&quot;reset&quot; value=&quot;Reset&quot; /&gt;&lt;/p&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>BudgetResult.html:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
  
      &lt;title&gt;Budget Creation&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div th:insert=&quot;fragments/navbar.html&quot;&gt; &lt;/div&gt;
  
          &lt;div class=&quot;jumbotron&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
                  &lt;h3&gt;Budget Created&lt;/h3&gt;
              &lt;/div&gt;
              &lt;p th:text=&quot;'Name: ' + ${budget.budgets_name}&quot; /&gt;
              &lt;p th:text=&quot;'Description: ' + ${budget.budgets_description}&quot; /&gt;
              &lt;p th:text=&quot;'Amount: ' + ${budget.budgets_amount}&quot; /&gt;
              &lt;a href=&quot;new&quot;&gt;Submit another message&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;js/budgetScript.js&quot;&gt;&lt;/script&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>