1. 程式人生 > >spring mvc 資料繫結問題 提交表單提示HTTP status 400, The request sent by the client was syntactically incorrect

spring mvc 資料繫結問題 提交表單提示HTTP status 400, The request sent by the client was syntactically incorrect

我們在spring mvc 中controller方法中的引數,spring mvc會自動為我們進行資料繫結。
spring mvc 方法中不一定要全部都有 form表單提交的屬性, 也可以有 請求屬性中 沒有的引數(這時候只會把對應不上的引數設為null),這兩種情況都不會報錯。
但是有幾種情況會報錯,可能會提示HTTP status 400, The request sent by the client was syntactically incorrect,而且此時如果加斷點你會發現根本進不了controller,但是瀏覽器中url的路徑是正確的。
會出現錯誤的情況如下:

    錯誤(1) 前臺請求的引數在方法中有對應的引數名,但是無法轉成對應的型別,比如form中 有<input name="id" type="text" />,
        方法public String aa(int id){ } , 當在文字框中輸入“wwwwee”等非整形字元時,就會出錯。特別提示的是,
        Java Date型別,在form表單中必須按照 “2016-10-10 12:10:12”  的格式走(注意中間的空格,最少要寫到時,
        即2016-10-10 12)。在form的checkbox多選框對應 Java的陣列,不能對應List。

    錯誤(2)方法中引數名重複。public String aa(User user,String name){   }, User這個類中有name屬性,現在就導致 spring mvc 
        不知道繫結哪各屬性。