由FileUploadInterceptor创建的Struts2字段错误密钥

时间:2012-07-12 13:09:17

标签: struts2 struts-validation

我正在使用Struts2 - FileUploadInterceptor来处理正在上传的文件的内容类型和最大大小。

为了显示错误消息,<s:fielderror/>显示错误消息,这是完美的。

我正在处理一个包含多个部分的页面,我需要显示仅属于该部分的错误消息,<s:fielderror/>的问题是它显示所有错误消息并解决此参数将被传递,喜欢

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

我不确定FileUploadInterceptor为内容类型错误和最大大小错误创建的错误密钥是什么,请帮我解决此问题。

1 个答案:

答案 0 :(得分: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/>只接受字段名称 您无法以这种方式分隔一个字段的多个错误。

此外,查看acceptFileFileUploadInterceptor方法的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           }

所以你应该一次只能得到其中一个错误。