第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Spring - Thymeleaf:異常處理模板

Spring - Thymeleaf:異常處理模板

慕絲7291255 2022-12-15 16:46:16
我正在關(guān)注 Spring in Action 第 5 版一書,但我相信這是一個(gè)錯(cuò)誤。這是本書的 GitHub。我到達(dá)了第 3 章 tacos-jdbc 源代碼提交我的訂單時(shí)突然出現(xiàn)錯(cuò)誤:并以這種方式在終端上:2019-05-25 16:58:18.164 錯(cuò)誤 11777 --- [nio-8080-exec-7] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-7] 異常處理模板“orderForm” :模板解析期間發(fā)生錯(cuò)誤(模板:“類路徑資源[templates/orderForm.html]”)org.thymeleaf.exceptions.TemplateInputException:模板解析期間發(fā)生錯(cuò)誤(模板:“類路徑資源 [templates/orderForm.html]”)訂單控制器:@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:/";    }}
查看完整描述

4 回答

?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

我修復(fù)了這段代碼添加delivery前綴:


    <!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="${order}">

    <h1>Order your taco creations!</h1>


    <img th:src="@{/images/TacoCloud.png}"/>


    <h3>Your tacos in this order:</h3>

    <a th:href="@{/design}" id="another">Design another taco</a><br/>

    <ul>

        <li th:each="taco : ${order.tacos}"><span th:text="${taco.name}">taco name</span></li>

    </ul>


    <div th:if="${#fields.hasErrors()}">

        <span class="validationError">

        Please correct the problems below and resubmit.

        </span>

    </div>


    <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}">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}">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}">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}">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}">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 Num 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 Num Error</span>

    <br/>


    <input type="submit" value="Submit order"/>

</form>


</body>

</html>



查看完整回答
反對 回復(fù) 2022-12-15
?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

我認(rèn)為您應(yīng)該按照書中所述處理 orderForm.html 中所有輸入中驗(yàn)證錯(cuò)誤的顯示,如下所示:(來源 Git)


<!-- tag::allButValidation[] -->

<!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="${order}">

      <h1>Order your taco creations!</h1>


      <img th:src="@{/images/TacoCloud.png}"/>

      <a th:href="@{/design}" id="another">Design another taco</a><br/>


      <div th:if="${#fields.hasErrors()}">

        <span class="validationError">

        Please correct the problems below and resubmit.

        </span>

      </div>


      <h3>Deliver my taco masterpieces to...</h3>

      <label for="name">Name: </label>

      <input type="text" th:field="*{name}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('name')}"

            th:errors="*{name}">Name Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <label for="street">Street address: </label>

      <input type="text" th:field="*{street}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('street')}"

            th:errors="*{street}">Street Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <label for="city">City: </label>

      <input type="text" th:field="*{city}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('city')}"

            th:errors="*{city}">City Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <label for="state">State: </label>

      <input type="text" th:field="*{state}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('state')}"

            th:errors="*{state}">State Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <label for="zip">Zip code: </label>

      <input type="text" th:field="*{zip}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('zip')}"

            th:errors="*{zip}">Zip Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <h3>Here's how I'll pay...</h3>

<!-- tag::validatedField[] -->

      <label for="ccNumber">Credit Card #: </label>

      <input type="text" th:field="*{ccNumber}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('ccNumber')}"

            th:errors="*{ccNumber}">CC Num Error</span>

<!-- tag::allButValidation[] -->

<!-- end::validatedField[] -->

      <br/>


      <label for="ccExpiration">Expiration: </label>

      <input type="text" th:field="*{ccExpiration}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('ccExpiration')}"

            th:errors="*{ccExpiration}">CC Num Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <label for="ccCVV">CVV: </label>

      <input type="text" th:field="*{ccCVV}"/>

<!-- end::allButValidation[] -->

      <span class="validationError"

            th:if="${#fields.hasErrors('ccCVV')}"

            th:errors="*{ccCVV}">CC Num Error</span>

<!-- tag::allButValidation[] -->

      <br/>


      <input type="submit" value="Submit order"/>

    </form>


  </body>

</html>

<!-- end::allButValidation[] -->

我認(rèn)為您沒有按照本章中解釋的添加到 bean 中的驗(yàn)證規(guī)則在表單中插入正確的信息。通過顯示驗(yàn)證錯(cuò)誤,您將在提交訂單時(shí)確切地知道哪個(gè)輸入沒有正確插入。


在調(diào)查您的代碼之后。Order.java 中的屬性名稱與視圖頁面 orderForm.html 中的屬性名稱不同


例如,在 orderForm 中,屬性是name


  <h3>Deliver my taco masterpieces to...</h3>

    <label for="name">Name: </label>

    <input type="text" th:field="*{name}"/>

而在 Order.java 中是deliveryName。


 @NotBlank(message="Delivery name is required")

    private String deliveryName;

解決方案是在 Order.java 和 orderForm.html 頁面中使用相同名稱的屬性。


查看完整回答
反對 回復(fù) 2022-12-15
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

我想你可以這樣做


@Controller

public class OrderController {


   @GetMapping("/orders")

   public String orders(Order order) {

      return "orderForm";

   }


   @PostMapping("/orders")

   public String orderForm(@Valid Order order, BindingResult result, Model model) {

       if(result.hasErrors()) {

           return "orderForm";

       } else {

           retrun "your_success_view";   

       }

    }

}


查看完整回答
反對 回復(fù) 2022-12-15
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

您th:object="${order}"在orderForm模板中使用,但 Thymeleaf 不知道。你必須像這樣將它傳遞給模板,讓 Thymeleaf 知道這個(gè)對象


@GetMapping("/current")

public ModelAndView orderForm() {

    ModelAndView mv = new ModelAndView("orderForm");

    mv.addObject("order", new Order());

    return mv;

}

注意:您必須在模板中使用該對象的所有地方從您的控制器層傳遞該對象。


更新 1


同時(shí)更新你的發(fā)布方法


@PostMapping

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

                           SessionStatus sessionStatus) {

    if (errors.hasErrors()) {

        ModelAndView mv = new ModelAndView("orderForm");

        mv.addObject("order", new Order());

        return mv;

    }


    orderRepo.save(order);

    sessionStatus.setComplete();


    return new ModelAndView("redirect:/");

}


查看完整回答
反對 回復(fù) 2022-12-15
  • 4 回答
  • 0 關(guān)注
  • 202 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號