如何实现struts2中的{0}字段验证消息

发布于 2024-09-10 22:15:56 字数 537 浏览 2 评论 0原文

struts2中如何实现参数化消息。 在actioname-validation.xml中我有

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="common.required">
                <argo key="common.family" />
            </message>
        </field-validator>
    </field>

资源文件,

common.required = {0} is required.
common.family = Family

我尝试过类似的东西。如何实现这一目标?

How to achieve parameterized message in struts2.
in actioname-validation.xml i have

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="common.required">
                <argo key="common.family" />
            </message>
        </field-validator>
    </field>

resource file i have,

common.required = {0} is required.
common.family = Family

some thing like this i tried. how to achive this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟雨扶苏 2024-09-17 22:15:56

这篇文章解释了如何做你想做的事。其要点是您定义验证规则如下:

<field name="family.familyName">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="common.required"/>
    </field-validator>
</field>

然后在您的 .properties 文件中,您将具有以下内容:

common.required = ${getText(fieldName)} is required.
family.familyName = Family

This article explains how to do what you want. The gist of it is that you'd define your validation rule as follows:

<field name="family.familyName">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="common.required"/>
    </field-validator>
</field>

And then in your .properties file, you'd have the following:

common.required = ${getText(fieldName)} is required.
family.familyName = Family
你怎么这么可爱啊 2024-09-17 22:15:56

它给了我我所期望的

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>
                ${getText("requiredstring", {getText("common.family")})}
            </message>
        </field-validator>
    </field>

应用程序资源,

requiredstring = {0} is required.
common.family = Family

It gives me, what i expected

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>
                ${getText("requiredstring", {getText("common.family")})}
            </message>
        </field-validator>
    </field>

Application Resources,

requiredstring = {0} is required.
common.family = Family
乙白 2024-09-17 22:15:56

也可以通过如下方式解决

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="common.required">
                <param name="1">getText('common.family')</param>
            </message>
        </field-validator>
    </field>

It might be also resolved as follows

<field name="family.familyName">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message key="common.required">
                <param name="1">getText('common.family')</param>
            </message>
        </field-validator>
    </field>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文