模板解析期间出现内部服务器错误。发生什么事了?

问题描述 投票:0回答:1

因此,我正在构建一个简单的炸玉米饼订单应用程序,当我在 /design 提交表单时,出现了“模板解析”错误。

[[Error on endpoint orders/current](https://i.stack.imgur.com/ujN8P.jpg)](https://i.stack.imgur.com/KIaiC.jpg)

这是 orderForm.html 的 thymeleaf 代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Taco Cloud</title>
    <link rel="stylesheet" th:href="@{/styles.css}" />
  </head>

<body>
    <form method="POST" th:action="@{/orders}" th:object="${tacoOrder}"> 
        <h1>Order your taco creations!</h1>
        <img th:src="@{/images/TacoCloud.jpeg}"/>
        
        <h3>Your tacos in this order:</h3>
        <a th:href="@{/design}" id="another">Design another taco</a><br/> 
        <ul>
            <li th:each="taco : ${tacoOrder.tacos}">
                <span th:text="${taco.name}">taco name</span></li>
        </ul>
        
      <h3>Deliver my taco masterpieces to...</h3>
      <label for="deliveryName">Name: </label>
      <input type="text" th:field="*{deliveryName}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('deliveryName')}"
            th:errors="*{deliveryName}">Delivery Name Error</span>
      <br/>
      
      <label for="deliveryStreet">Street address: </label>
      <input type="text" th:field="*{deliveryStreet}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('deliveryStreet')}"
            th:errors="*{deliveryStreet}">Delivery Street Error</span>
      <br/>
      
      <label for="deliveryCity">City: </label>
      <input type="text" th:field="*{deliveryCity}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('deliveryCity')}"
            th:errors="*{deliveryCity}">Delivery City Error</span>
      <br/>
      
      <label for="deliveryState">State: </label>
      <input type="text" th:field="*{deliveryState}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('deliveryState')}"
            th:errors="*{deliveryState}">Delivery State Error</span>
      <br/>
      
      <label for="deliveryZip">Zip code: </label>
      <input type="text" th:field="*{deliveryZip}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('deliveryZip')}"
            th:errors="*{deliveryZip}">Delivery Zip Error</span>
      <br/>
      
      <h3>Here's how I'll pay...</h3>
      <label for="ccNumber">Credit Card #: </label>
        <input type="text" th:field="*{ccNumber}"/>
        <span class="validationError"
            th:if="${#fields.hasErrors('ccNumber')}"
            th:errors="*{ccNumber}">CC Num Error</span>
        <br/>
        
      <label for="ccExpiration">Expiration: </label>
      <input type="text" th:field="*{ccExpiration}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('ccExpiration')}"
            th:errors="*{ccExpiration}">CC Expiration Error</span>
      <br/>
      
      <label for="ccCVV">CVV: </label>
      <input type="text" th:field="*{ccCVV}"/>
      <span class="validationError"
            th:if="${#fields.hasErrors('ccCVV')}"
            th:errors="*{ccCVV}">CC CVV Error</span>
      <br/>
      
      <input type="submit" value="Submit Order"/>
   </form>
</body>
</html>

OrderController.java代码

import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import tacos.TacoOrder;

@Slf4j
@Controller
@RequestMapping("/orders")
@SessionAttributes("tacoOrder")
public class OrderController {
    
    @GetMapping("/current")
    public String orderForm() {
        return "orderForm";
  }
    
    @PostMapping
    public String processOrder(@Valid TacoOrder order, Errors errors, SessionStatus sessionStatus) {
        if (errors.hasErrors()) {
            return "orderForm";
          }
      log.info("Order submitted: {}", order);
      sessionStatus.setComplete();
      return "redirect:/";
    }

}

我的 TacoOrder 课程:

public class TacoOrder {
    @NotBlank(message="Delivery name is required")  
    private String deliveryName;
    
    @NotBlank(message="Street is required")
    private String deliveryStreet;
    
    @NotBlank(message="City is required")
    private String deliveryCity;
    
    @NotBlank(message="State is required")
    private String deliveryState;
    
    @NotBlank(message="Zipcode is required")
    private String deliveryZip;
      
    @CreditCardNumber(message="Not a valid credit card number")
    private String ccNumber;
      
    @Pattern(regexp="^(0[1-9]|1[0-2])([\\/])([2-9][0-9])$", message="Must be formatted MM/YY")
    private String ccExpiration;
      
    @Digits(integer=3, fraction=0, message="Invalid CVV")
    private String ccCVV;
    
    private List<Taco> tacos = new ArrayList<>();
      
    public void addTaco(Taco taco) {
        this.tacos.add(taco);
    }

}

我的炸玉米饼课:

public class Taco {
    @NotNull
    @Size(min=5, message="Name must be at least 5 characters long") 
    private String name;
    
    @NotNull
    @Size(min=1, message="You must choose at least 1 ingredient")
    private List<Ingredient> ingredients;
}

我检查了 orderForm.html、tacoorder.java,检查了所有控制器类,但似乎无法找到为什么会出现此错误。 发生什么事了?

java spring-boot model-view-controller thymeleaf
1个回答
0
投票

ThymeLeaf 使用 getter 和 setter 来访问字段。目前“tacos”是 TacoOrder 的私有字段,因此 ThymeLeaf 不可见。

将 getter 和 setter 添加到字段 - 然后它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.