Spring - Thymeleaf:异常处理模板

我正在关注 Spring in Action 第 5 版一书,但我相信这是一个错误。

这是本书的 GitHub。我到达了第 3 章 tacos-jdbc 源代码

提交我的订单时突然出现错误:

http://img4.mukewang.com/639adeec0001836f12420266.jpg

并以这种方式在终端上:

2019-05-25 16:58:18.164 错误 11777 --- [nio-8080-exec-7] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-7] 异常处理模板“orderForm” :模板解析期间发生错误(模板:“类路径资源[templates/orderForm.html]”)

org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源 [templates/orderForm.html]”)

http://img3.mukewang.com/639adef800019be012030201.jpg

订单控制器:


@Controller

@RequestMapping("/orders")

@SessionAttributes("order")

public class OrderController {


    private OrderRepository orderRepo;


    public OrderController(OrderRepository orderRepo) {

        this.orderRepo = orderRepo;

    }


    @GetMapping("/current")

    public String orderForm() {

        return "orderForm";

    }


    @PostMapping

    public String processOrder(@Valid Order order, Errors errors,

                               SessionStatus sessionStatus) {

        if (errors.hasErrors()) {

            return "orderForm";

        }


        orderRepo.save(order);

        sessionStatus.setComplete();


        return "redirect:/";

    }


}


慕丝7291255
浏览 219回答 4
4回答

慕姐4208626

我修复了这段代码添加delivery前缀:&nbsp; &nbsp; <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"&nbsp; &nbsp; &nbsp; xmlns:th="http://www.thymeleaf.org"><head>&nbsp; &nbsp; <title>Taco Cloud</title>&nbsp; &nbsp; <link rel="stylesheet" th:href="@{/styles.css}" /></head><body><form method="POST" th:action="@{/orders}" th:object="${order}">&nbsp; &nbsp; <h1>Order your taco creations!</h1>&nbsp; &nbsp; <img th:src="@{/images/TacoCloud.png}"/>&nbsp; &nbsp; <h3>Your tacos in this order:</h3>&nbsp; &nbsp; <a th:href="@{/design}" id="another">Design another taco</a><br/>&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; <li th:each="taco : ${order.tacos}"><span th:text="${taco.name}">taco name</span></li>&nbsp; &nbsp; </ul>&nbsp; &nbsp; <div th:if="${#fields.hasErrors()}">&nbsp; &nbsp; &nbsp; &nbsp; <span class="validationError">&nbsp; &nbsp; &nbsp; &nbsp; Please correct the problems below and resubmit.&nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; </div>&nbsp; &nbsp; <h3>Deliver my taco masterpieces to...</h3>&nbsp; &nbsp; <label for="deliveryName">Name: </label>&nbsp; &nbsp; <input type="text" th:field="*{deliveryName}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('deliveryName')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{deliveryName}">Name Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="deliveryStreet">Street address: </label>&nbsp; &nbsp; <input type="text" th:field="*{deliveryStreet}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('deliveryStreet')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{deliveryStreet}">Street Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="deliveryCity">City: </label>&nbsp; &nbsp; <input type="text" th:field="*{deliveryCity}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('deliveryCity')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{deliveryCity}">City Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="deliveryState">State: </label>&nbsp; &nbsp; <input type="text" th:field="*{deliveryState}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('deliveryState')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{deliveryState}">State Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="deliveryZip">Zip code: </label>&nbsp; &nbsp; <input type="text" th:field="*{deliveryZip}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('deliveryZip')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{deliveryZip}">Zip Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <h3>Here's how I'll pay...</h3>&nbsp; &nbsp; <label for="ccNumber">Credit Card #: </label>&nbsp; &nbsp; <input type="text" th:field="*{ccNumber}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccNumber')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccNumber}">CC Num Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="ccExpiration">Expiration: </label>&nbsp; &nbsp; <input type="text" th:field="*{ccExpiration}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccExpiration')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccExpiration}">CC Num Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <label for="ccCVV">CVV: </label>&nbsp; &nbsp; <input type="text" th:field="*{ccCVV}"/>&nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccCVV')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccCVV}">CC Num Error</span>&nbsp; &nbsp; <br/>&nbsp; &nbsp; <input type="submit" value="Submit order"/></form></body></html>

慕尼黑8549860

我认为您应该按照书中所述处理 orderForm.html 中所有输入中验证错误的显示,如下所示:(来源 Git)<!-- tag::allButValidation[] --><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"&nbsp; &nbsp; &nbsp; xmlns:th="http://www.thymeleaf.org">&nbsp; <head>&nbsp; &nbsp; <title>Taco Cloud</title>&nbsp; &nbsp; <link rel="stylesheet" th:href="@{/styles.css}" />&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <form method="POST" th:action="@{/orders}" th:object="${order}">&nbsp; &nbsp; &nbsp; <h1>Order your taco creations!</h1>&nbsp; &nbsp; &nbsp; <img th:src="@{/images/TacoCloud.png}"/>&nbsp; &nbsp; &nbsp; <a th:href="@{/design}" id="another">Design another taco</a><br/>&nbsp; &nbsp; &nbsp; <div th:if="${#fields.hasErrors()}">&nbsp; &nbsp; &nbsp; &nbsp; <span class="validationError">&nbsp; &nbsp; &nbsp; &nbsp; Please correct the problems below and resubmit.&nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <h3>Deliver my taco masterpieces to...</h3>&nbsp; &nbsp; &nbsp; <label for="name">Name: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{name}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('name')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{name}">Name Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="street">Street address: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{street}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('street')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{street}">Street Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="city">City: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{city}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('city')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{city}">City Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="state">State: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{state}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('state')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{state}">State Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="zip">Zip code: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{zip}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('zip')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{zip}">Zip Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <h3>Here's how I'll pay...</h3><!-- tag::validatedField[] -->&nbsp; &nbsp; &nbsp; <label for="ccNumber">Credit Card #: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{ccNumber}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccNumber')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccNumber}">CC Num Error</span><!-- tag::allButValidation[] --><!-- end::validatedField[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="ccExpiration">Expiration: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{ccExpiration}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccExpiration')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccExpiration}">CC Num Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <label for="ccCVV">CVV: </label>&nbsp; &nbsp; &nbsp; <input type="text" th:field="*{ccCVV}"/><!-- end::allButValidation[] -->&nbsp; &nbsp; &nbsp; <span class="validationError"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:if="${#fields.hasErrors('ccCVV')}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; th:errors="*{ccCVV}">CC Num Error</span><!-- tag::allButValidation[] -->&nbsp; &nbsp; &nbsp; <br/>&nbsp; &nbsp; &nbsp; <input type="submit" value="Submit order"/>&nbsp; &nbsp; </form>&nbsp; </body></html><!-- end::allButValidation[] -->我认为您没有按照本章中解释的添加到 bean 中的验证规则在表单中插入正确的信息。通过显示验证错误,您将在提交订单时确切地知道哪个输入没有正确插入。在调查您的代码之后。Order.java 中的属性名称与视图页面 orderForm.html 中的属性名称不同例如,在 orderForm 中,属性是name&nbsp; <h3>Deliver my taco masterpieces to...</h3>&nbsp; &nbsp; <label for="name">Name: </label>&nbsp; &nbsp; <input type="text" th:field="*{name}"/>而在 Order.java 中是deliveryName。&nbsp;@NotBlank(message="Delivery name is required")&nbsp; &nbsp; private String deliveryName;解决方案是在 Order.java 和 orderForm.html 页面中使用相同名称的属性。

不负相思意

我想你可以这样做@Controllerpublic class OrderController {&nbsp; &nbsp;@GetMapping("/orders")&nbsp; &nbsp;public String orders(Order order) {&nbsp; &nbsp; &nbsp; return "orderForm";&nbsp; &nbsp;}&nbsp; &nbsp;@PostMapping("/orders")&nbsp; &nbsp;public String orderForm(@Valid Order order, BindingResult result, Model model) {&nbsp; &nbsp; &nbsp; &nbsp;if(result.hasErrors()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return "orderForm";&nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;retrun "your_success_view";&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}

白猪掌柜的

您th:object="${order}"在orderForm模板中使用,但 Thymeleaf 不知道。你必须像这样将它传递给模板,让 Thymeleaf 知道这个对象@GetMapping("/current")public ModelAndView orderForm() {&nbsp; &nbsp; ModelAndView mv = new ModelAndView("orderForm");&nbsp; &nbsp; mv.addObject("order", new Order());&nbsp; &nbsp; return mv;}注意:您必须在模板中使用该对象的所有地方从您的控制器层传递该对象。更新 1同时更新你的发布方法@PostMappingpublic ModelAndView processOrder(@Valid Order order, Errors errors,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SessionStatus sessionStatus) {&nbsp; &nbsp; if (errors.hasErrors()) {&nbsp; &nbsp; &nbsp; &nbsp; ModelAndView mv = new ModelAndView("orderForm");&nbsp; &nbsp; &nbsp; &nbsp; mv.addObject("order", new Order());&nbsp; &nbsp; &nbsp; &nbsp; return mv;&nbsp; &nbsp; }&nbsp; &nbsp; orderRepo.save(order);&nbsp; &nbsp; sessionStatus.setComplete();&nbsp; &nbsp; return new ModelAndView("redirect:/");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java