2013-01-23 23 views
0

我在我的代碼中有以下幾行。我正在驗證JSP表單中的驗證碼。 我不明白在FieldError對象中傳遞的所有參數的含義。什麼是FieldError對象中的參數代碼?

if (!reCaptchaResponse.isValid()) { 
    FieldError fieldError = new FieldError("CaptchaObj", "captcha", 
      uresponse, false, new String[] { "badCptcha.CaptchaObj.captcha" }, 
      null, "Please, Try Again "); 
    result.addError(fieldError); 
} 

這裏的結果變量的類型是BindingResult

我想在FieldError對象的構造,特別是用於在構造其中String類型的代碼參數每個參數的確切含義。

回答

3

我會建議閱讀API文檔中發現的字段錯誤here

它提到了以下參數此構造函數:

Parameters: 
    objectName - the name of the affected object 
    field - the affected field of the object 
    rejectedValue - the rejected field value 
    bindingFailure - whether this error represents a binding failure (like a type mismatch); else, it is a validation failure 
    codes - the codes to be used to resolve this message 
    arguments - the array of arguments to be used to resolve this message 
    defaultMessage - the default message to be used to resolve this message 

其中最重要的參數之一是代碼的參數,它包含將您的信息源中進行搜索的代碼。如果找到與此代碼匹配的消息將被顯示。消息源可以獲取參數,所以消息源可以含有像一個條目:

typeMismatch.startDate={0} is an invalid date. Use format DD/MM/YYYY. 

在這種情況下的代碼將是typeMismatch.startDate和與此代碼將顯示的第一個參數,然後該消息對應的消息。該消息的{0}部分表示應該顯示第一個參數。這些參數由構造函數中的第6個參數提供,在您的示例中爲null。

+0

我想要的例子! –

+0

@HarshadDeshmukh我正在接近!我描述了兩個更重要的參數,希望這有助於。 –

+0

我知道了,謝謝 –

相關問題