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

时间:2010-07-21 09:26:45

标签: struts2

如何在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
我尝试过这样的事情。如何实现这个目标?

3 个答案:

答案 0 :(得分:2)

This article解释了如何做你想做的事。它的要点是您将如下定义验证规则:

<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

答案 1 :(得分:2)

它给了我,我所期待的

<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

答案 2 :(得分:1)

它也可以解决如下

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