2012-07-12 28 views
2

我正在使用Struts2-FileUploadInterceptor來處理正在上傳的文件的內容類型和最大大小。由FileUploadInterceptor創建的Struts2字段錯誤密鑰要求

爲了顯示錯誤消息,<s:fielderror/>顯示錯誤消息,這是完美的。

我其中有多個部分的頁面上工作,我需要顯示僅屬於該部分的錯誤消息,與<s:fielderror/>的問題是,它會顯示所有的錯誤信息,並解決了這個參數傳遞,就像

<s:fielderror><s:param>firstName</s:param></s:fielderror> 

我不確定什麼是FileUploadInterceptor爲內容類型錯誤和最大尺寸錯誤創建的錯誤鍵,請幫我解決這個問題。

回答

1

如果我理解正確的話,我覺得你想要做這樣的事情:

<s:fielderror>[some reference to a content type error]</s:fielderror> 
<s:fielderror>[some reference to a max size error]</s:fielderror> 

,這樣就可以單獨/使它們不同。

這是目前不可能因爲<s:fielderror/>只需要字段名稱。
您無法以這種方式分隔一個字段的多個錯誤。

此外,查看FileUploadInterceptoracceptFile方法的source code,將首先檢查最大大小,如果檢查失敗,則會設置錯誤。如果最大尺寸已經失敗,代碼將不會繼續檢查允許的類型。

353   } else if (maximumSize != null && maximumSize < file.length()) { 
    354    String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new Object[]{inputName, filename, file.getName(), "" + file.length()}, locale); 
    355    if (validation != null) { 
    356     validation.addFieldError(inputName, errMsg); 
    357    } 
    358 
    359    LOG.warn(errMsg); 
    360   } else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) { 
    361    String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new Object[]{inputName, filename, file.getName(), contentType}, locale); 
    362    if (validation != null) { 
    363     validation.addFieldError(inputName, errMsg); 
    364    } 
    365 
    366    LOG.warn(errMsg); 
    367   } 

因此,您應該一次只能得到這些錯誤之一。

+0

感謝您的回覆,抱歉,如果我有我的問題困惑,我的查詢是什麼應該是關鍵從特定的錯誤,從你的代碼,什麼應該是確切的關鍵代碼'[[一些內容類型的引用錯誤]' – 2012-07-12 15:26:37

+0

@PramodCA我想告訴你的是,有沒有這樣的關鍵**只是內容類型錯誤或只是最大尺寸錯誤。你的唯一選擇是' myFileFieldName' – nmc 2012-07-12 15:30:56

+0

我的文件名是pramod_resume.doc,比我編碼爲' pramod_resume.doc',即使是這樣,錯誤也沒有顯示。 – 2012-07-12 15:40:45