2010-06-29 66 views
1

我正在使用Spring MVC的Web應用程序,我正在處理選擇客戶端和顯示聯繫人信息的簡單表單。java.lang.NumberFormatException - 數據綁定錯誤

我遇到的一個問題是,如果我第一次選擇一個客戶端,它會拉起信息,但第二次不會。它會顯示前一個客戶的信息。

我查看了更多我正在記錄的內容,並注意到我收到了一個數據綁定錯誤,並在我的控制檯上看到了非常熟悉的輸出,如下所示。

Failed to convert property value of type [java.lang.String[]] to required type 
[java.lang.Integer] for property 'clientId'; nested exception is 
java.lang.NumberFormatException: For input string: "3349,4182"

如果在錯誤輸出看到它顯示

...for string: "3349,4182"

作爲參考的3349是所選擇的第一個客戶,當表單貼4182的客戶端ID是的客戶端識別第二。

我已經做了一些研究,並且遇到了一些人,他們曾經說過,它將兩個數字作爲一個數組[3349,4182]對待,而不是僅僅採用新的ClientID。

謝謝,



編輯 背襯目的是如下

 
public class ClientContactModel implements Serializable { 
    private String searchText; 
    private Integer clientId; 
public ClientContactModel() { 
    } 
    public String getSearchText() { 
     return searchText; 
    } 
    public void setSearchText(String searchText) { 
     this.searchText = searchText; 
    } 
    public Integer getClientId() { 
     return clientId; 
    } 
    public void setClientId(Integer clientId) { 
     this.clientId = clientId; 
    } 
} 

被投擲NumberFormatException的呼叫是

 clientId = Integer.valueOf(request.getParameter("clientId"));


要回答你的第一個問題,是的,我使用的是SimpleformController

這裏是UI代碼有關客戶,

<pre><code><tr> 
    <td align="center"> 
    <form:select path="clientId"> 
     <form:option value="">Select a client...</form:option> 
     <form:options items="${clientUsers}" itemValue="id" itemLabel="username" /> 
    </form:select> 
    <input type="button" name="selectButton" id="selectButton" value="Go" onclick="selectContact();"> 
    </td> 
</tr> 
<tr> 
    <td align="center"> 
    Display clients who have not updated their contact information since 
    <input name="month" type="text" id="textfield2" value="MM" size="4"> 
    <input name="day" type="text" id="textfield3" value="DD" size="4"> 
    <input name="year" type="text" id="textfield4" value="YY" size="4"> 
    <input type="submit" name="notUpdatedButton" id="notUpdatedButton" value="Go"> 
    </td> 
</tr> 

有一個小JS它太

function selectContact() { 
    document.getElementById("searchText").value = ""; 
    document.getElementById("clientContactObj").submit(); 
    } 
+0

你剛使用SimpleFormController嗎?你的FormBackingObject看起來像什麼? – 2010-06-29 19:44:01

+1

如果您正在使用下拉菜單,您是否有multiple = true? clientId的任何UI代碼都會有所幫助。 – 2010-06-29 19:58:58

+1

嗯,我不是專家,但我認爲問題在於你的形式:選擇你需要設置multiple = false。我認爲這是因爲Spring試圖將String []傳遞給你的支持對象,因爲它應該只傳遞一個String,我想。 – 2010-06-29 20:16:10

回答

2

您可以在列表中選擇多個客戶端嗎?

如果是,您需要管理服務器中的多個ID(以逗號分隔的列表)。

如果否,請在選擇中使用multiple = false以防止選擇多個條目。