How do you override Struts 2 Messages?

2019-05-04 22:55发布

Here is a portion of my struts.xml:

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

and

<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>

My global.properties file is stored directly in src/ and it contains this:

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

Here is how I'm displaying the error in the jsp:

<s:fielderror />

Currently, when I upload a file that is too big for example, I get the error:

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

What I want to do is make it say "Uploaded file is too large" instead of the message above. What is an easy way to do this?

EDIT 1:

I'm using struts version 2.1.8.1

EDIT 2:

Another thing is that when I add:

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

to my struts.xml then no error shows at all. What is the reason for this?

4条回答
成全新的幸福
2楼-- · 2019-05-04 23:26

Source 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.

Source MessageStoreInterceptor

Note: I'm here pointing to older version of struts so if you were using latest version change the version in url.

查看更多
放我归山
3楼-- · 2019-05-04 23:32

hehe, you rly sticked on this question. it seems pretty simple.

Check out the example here: http://struts2-by-ash.blogspot.com/2012/06/override-struts-2-messages.html

best, j

查看更多
Summer. ? 凉城
4楼-- · 2019-05-04 23:38

Use <s:actionerror /> tag in JSP to print the error.

查看更多
可以哭但决不认输i
5楼-- · 2019-05-04 23:47

Firstly, This error comes into picture if you are trying to upload a file more than the file size you specified for struts.multipart.maxSize

There's a pretty easy solution: In your struts.xml, increase the file size value of struts.multipart.maxSize,

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

Keep param name="maximumSize" value less than struts.multipart.maxSize value, As in above case, you will get your customised error defined in global.properties unless you exceed struts.multipart.maxSize limit..so try to keep struts.multipart.maxSize value to some high range.

查看更多
登录 后发表回答