2012-06-22 42 views
4

這裏是我的struts.xml中的一部分:你如何覆蓋Struts 2消息?

<constant name="struts.custom.i18n.resources" value="global" /> 

<action name="picture_save" method="pictureSave" class="PictureActionBean"> 
    <interceptor-ref name="fileUpload"> 
     <param name="maximumSize"> 
      2097152 
     </param> 
     <param name="allowedTypes"> 
      image/bmp,image/gif,image/jpeg,image/jpg,image/png 
     </param> 
    </interceptor-ref> 
    <interceptor-ref name="defaultStack"></interceptor-ref> 
    <result name="success" type="redirect">picture</result> 
    <result name="error">error</result> 
</action> 

我global.properties文件直接存儲在SRC /,它包含此:

struts.messages.error.uploading=Upload failed 
struts.messages.error.file.too.large=Upload file is too large 
struts.messages.error.content.type.not.allowed=Wrong file type uploaded 

以下是我在jsp中如何顯示錯誤:

<s:fielderror /> 

目前,當我上傳的文件過大,例如,我得到的錯誤:

the request was rejected because its size (6561343) exceeds the configured maximum (2097152)

我想要做的就是它說「上傳的文件過大」的消息,而不是上面。什麼是一個簡單的方法來做到這一點?

編輯1:

我用struts版本2.1.8.1

編輯2:

另一件事是,當我添加:

<result name="input" type="redirect">error</result> 

到我的struts.xml,然後沒有任何錯誤顯示。這是什麼原因?

+0

到目前爲止我還沒有成功。我搜索了網頁並閱讀了文檔:http://struts.apache.org/2.1.8.1/docs/message-resource-files.html – ThreaT

+0

和: http://struts.apache.org/2.1。 8/struts2-core/apidocs/org/apache/struts2/interceptor/FileUploadInterceptor.html – ThreaT

+0

和:http://struts.apache.org/2.2.1/docs/exception-handling.html – ThreaT

回答

2

呵呵,你真的堅持這個問題。它看起來很簡單。

退房這裏的例子:http://struts2-by-ash.blogspot.com/2012/06/override-struts-2-messages.html

最好,J

+0

@ThreaT我可以幫助你,如果你發佈你的整個struts配置。你可以試試這個(c編輯1),我從來沒有嘗試過。希望它可以幫助 – Jaiwo99

+0

@ThreaT sry,它不應該工作,我只是檢查API Fielderror,它不像我想的那樣工作。我想這個錯誤可能是來自struts2的默認消息每次都會覆蓋你的消息。並檢查它(編輯2)。它應該是解決方案。 – Jaiwo99

0

來源Overriding Struts Default Properties

The framework uses a number of properties that can be changed to fit your needs. To change any of these properties, specify the property key and value in an struts.properties file. The properties file can be locate anywhere on the classpath, but it is typically found under /WEB-INF/classes

When you are redirecting error messages will be gone. Because you were invoking new action to handle this situation struts2 providing messageStoreInterceptor.

來源MessageStoreInterceptor

注:我在這裏指着舊版本的支柱,所以如果你使用最新版本的改變URL中的版本。

+0

我的問題是,爲什麼我的屬性文件不能踢? – ThreaT

0

使用<s:actionerror />標籤在JSP中打印錯誤。

0

首先,這個錯誤進入畫面,如果你想上傳的文件超過您對文件上傳用

指定的文件大小

有一個非常簡單的解決辦法:在你的struts.xml,增加文件上傳用的文件大小值,

<constant name="struts.multipart.maxSize" value="50777216000" /> 
    <param name="maximumSize">15728640</param> 

保持PARAM NAME =「MAXIMUMSIZE」值小於文件上傳用價值, 如在上述情況下,你會得到全球定義您的自定義錯誤。屬性,除非你超過struts.multipart.maxSize限制..所以試圖保持struts.multipart.maxSize值爲som高範圍。