2012-12-13 27 views
-1

我試圖驗證使用Spring JSR303驗證的對象,我有有一些嵌套的對象在這裏某種形式的屬性一起表單對象是我的表單簽名Spring的MVC Bean驗證類型mismtach錯誤

public class PaymentDetailsForm 
{ 
    private AddressForm billingAddress; 
    // other properties and getter and setters 

} 

在我的AddressForm bean中,我使用了Bean驗證註釋來驗證數據,但是我沒有在我的PaymentDetailsForm中使用@Valid註釋billingAddress。 這是我的控制器方法的

public String createUpdatePaymentInfos(final Model model, 
@ModelAttribute("paymentInfo") @Valid final PaymentDetailsForm form, final BindingResult bindingResult) 
{ 
} 

如果我從形式都發送正確的數據是工作完全正常的簽名,而是在需要或者如果我從billingAddress省略任何字段,標記爲不空我得到以下裝訂錯誤異常

org.springframework.validation.BeanPropertyBindingResult: 1 errors 
Field error in object 'paymentInfo' on field 'billingAddress': 
rejected value [[email protected],true]; 
codes [typeMismatch.paymentInfo.billingAddress,typeMismatch.billingAddress,typeMismatch.com.xxx.storefront.forms.AddressForm,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [paymentInfo.billingAddress,billingAddress]; arguments []; default message [billingAddress]]; 
default message [Failed to convert property value of type 'java.lang.String[]' 
to required type 'com.xxx.storefront.forms.AddressForm' for property 'billingAddress'; 
nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String[]] to required type [com.xxx.storefront.forms.AddressForm] for property 'billingAddress': 
no matching editors or conversion strategy found] 

我期待,因爲我沒有使用@valid標註爲billingAddress財產,它不應該被驗證,但即使在情況下,它得到驗證,我不能夠理解上述異常/錯誤

回答

0

這從UI一些錯誤的映射是因爲,在我的JSP頁面,我是映射地址字段billingAddress對象,但有一樣

<form:hidden path="billingAddress" id="billingAddress"/> 

這是錯誤的原因,一個隱藏字段,因爲它試圖發送字符串數組和彈簧綁定無法區分我想要做什麼

0

您看到的bindingResult看起來不像是因爲驗證錯誤,它很可能是因爲綁定錯誤 - 無法將UI字段綁定到內部billingAddress字段。即使是綁定錯誤最終也會顯示在您看到的緊接着的綁定結果論證中。

+0

即時通訊同意您的輸入,我只是調試this.What可能是這個原因,因爲如果我發送所有數據完全字段映射只有在缺少一些所需數據時纔會正確解決此問題即將到來 –