【问题标题】:How do you override Struts 2 Messages?您如何覆盖 Struts 2 消息?
【发布时间】:2012-06-22 10:23:12
【问题描述】:

这是我的 struts.xml 的一部分:

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

目前,当我上传一个太大的文件时,例如,我收到错误:

请求被拒绝,因为它的大小 (6561343) 超过 配置最大值 (2097152)

我想要做的是让它说“上传的文件太大”而不是上面的消息。有什么简单的方法可以做到这一点?

编辑 1:

我正在使用 struts 版本 2.1.8.1

编辑 2:

另一件事是,当我添加:

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

到我的 struts.xml 然后根本没有错误显示。这是什么原因?

【问题讨论】:

标签: jsp error-handling struts2 messages


【解决方案1】:

呵呵,这个问题你坚持。看起来很简单。

在此处查看示例:http://struts2-by-ash.blogspot.com/2012/06/override-struts-2-messages.html

最好的,j

【讨论】:

  • @ThreaT 我可以帮助你,如果你发布你的整个 struts 配置。你可以试试这个(c EDIT 1),我以前从未尝试过。希望它可以帮助
  • @ThreaT 对不起,它不应该工作,我刚刚检查了 API Fielderror,它不像我想的那样工作。我猜这个错误可能是struts2的默认消息每次都会覆盖你的消息。并检查这个(编辑2)。它应该是解决方案。
【解决方案2】:

来源Overriding Struts Default Properties

该框架使用了许多可以更改以适应的属性 您的需求。要更改这些属性中的任何一个,请指定属性 struts.properties 文件中的键和值。属性文件可以是 定位在类路径上的任何位置,但它通常位于 /WEB-INF/类

当您重定向时,错误消息将消失。因为你是 调用新动作来处理这种情况 struts2 提供 messageStoreInterceptor。

来源MessageStoreInterceptor

注意:我这里指的是旧版本的 struts,所以如果您使用的是最新版本,请更改 url 中的版本。

【讨论】:

  • 我的问题是,为什么我的属性文件没有启动?
【解决方案3】:

在JSP中使用&lt;s:actionerror /&gt;标签打印错误。

【讨论】:

    【解决方案4】:

    首先,如果您尝试上传的文件大于您为 struts.multipart.maxSize 指定的文件大小,则会出现此错误

    有一个非常简单的解决方案:在你的 struts.xml 中,增加 struts.multipart.maxSize 的文件大小值,

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

    保持 param name="maximumSize" 值小于 struts.multipart.maxSize 值, 与上述情况一样,除非您超过 struts.multipart.maxSize 限制,否则您将在 global.properties 中定义自定义错误。所以请尝试将 struts.multipart.maxSize 值保持在某个较高的范围内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2011-04-28
      • 2014-09-15
      相关资源
      最近更新 更多