首页 > 解决方案 > 覆盖文件上传拦截器的 Struts 2 错误消息

问题描述

<action name="upload" class="uploadManualAction">
           <interceptor-ref name="fileUpload">
                <param name="maximumSize">20971520</param>
                <param name="allowedTypes">
                    application/pdf
                </param>
           </interceptor-ref>
           <interceptor-ref name="logFilterStack"></interceptor-ref>
           <result name="success">/jsp/confirmManualUpload.jsp</result>
           <result name="error">/error/manualUploadError.jsp</result>
           <result name="input">/jsp/manualUpload.jsp</result> 
        </action>

this is the first action.





<action name="csvUploadAction" class="csvUploadAction" method="processReadCsv">
         <interceptor-ref name="fileUpload">
                <param name="maximumSize">20971520</param>
                <param name="allowedTypes">
                    text/csv
                </param>
           </interceptor-ref>
           <interceptor-ref name="logFilterStack"></interceptor-ref>

            <result name="success">/jsp/csvUpload.jsp</result>
             <result name="error">/error/manualUploadError.jsp</result>
             <result name="input">/jsp/csvUpload.jsp</result> 
        </action>

This is the second acton 


i want to overide 

struts.messages.error.content.type.not.allowed=仅允许 PDF 内容 struts.messages.error.content.type.not.allowed=根据操作,此消息仅允许 CSV 内容

标签: javastruts2struts

解决方案


公共类 CsvFileUploadInterceptor 扩展 FileUploadInterceptor {

private static final long serialVersionUID = 1L;

private static Logger log = Logger.getLogger(CsvFileUploadInterceptor.class);

@Override
protected String getTextMessage(Object action, String messageKey, String[] args) {
    String actionName = action.getClass().getName();
    if (actionName.endsWith(WebAppConst.CSV_UPLOAD_ACTION)) {
        if (messageKey.equals(WebAppConst.STRUTS_MESSAGES_ERROR_FILE_TOO_LARGE)) {
            messageKey = WebAppConst.CSV_UPLOAD_ERROR_FILE_TOO_LARGE;
        } else if (messageKey.equals(WebAppConst.STRUTS_MESSAGES_ERROR_CONTENT_TYPE_NOT_ALLOWED)) {
            messageKey = WebAppConst.CSV_UPLOAD_ERROR_CONTENT_TYPE_NOT_ALLOWED;
        }
    }
    return super.getTextMessage(actionName, messageKey, args);

}

}


推荐阅读