亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

錯誤:既沒有 BindingResult 也沒有可用作請求屬性的 bean 名稱“id”

錯誤:既沒有 BindingResult 也沒有可用作請求屬性的 bean 名稱“id”

慕的地8271018 2021-12-10 15:24:04
我正在使用百里香葉和彈簧。我想實現發布請求。我的控制器類是public class URLController {    @RequestMapping(value = "index")    public String index1(Model model){        model.addAttribute("employee",new Employee());        return "index";    }    @RequestMapping(value = "/")    public String index(Model model){        model.addAttribute("employee",new Employee());        return "index";    }    @PostMapping("/result")    public String result(@ModelAttribute Employee employee){        System.out.print(employee.getName());        return "result";    }}和 html 頁面是<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>index page</title></head><body><form action="#" th:action="@{/result}" modelAttribute="employee" method="post">    <p>Id: <input type="text" th:field="*{id}" /></p>    <p>name: <input type="text" th:field="*{name}" /></p>    <p>phone: <input type="text" th:field="*{phone}" /></p>    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p></form></body></html>沒有與 id 字段的綁定。
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

在您的 HTML 中,您需要為模型屬性使用正確的語法。Spring 抱怨它找不到屬性,id因為您提供的是 string employee,而不是對象。


modelAttribute="employee" --> th:object="${employee}"


此外,您可以合并到:


@Controller //please add this

public class URLController {


    @GetMapping({"/", "/index"})

    public String index1(Model model){

        model.addAttribute("employee",new Employee());

        return "index";

    }


    @PostMapping("/result")

    public String result(@ModelAttribute Employee employee){

        System.out.print(employee.getName()); //use a logger instead

        return "result"; //may want to return a different page name for clarity

    }

}

如果您將 HTML 標記更改為:


<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:th="http://www.thymeleaf.org">


最后,您可能希望使用tel電話字段的輸入類型。這樣做將允許為移動用戶顯示自定義鍵盤。


查看完整回答
反對 回復 2021-12-10
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號