如何在复合组件中参数化requiredMessage属性? [英] How to parameterize requiredMessage attribute in composite component?

查看:197
本文介绍了如何在复合组件中参数化requiredMessage属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BalusC为有关如何为每个复合组件设置消息的问题提供了一个很好的答案. 为不同的复合组件指定不同的错误消息 答案涉及使用每个组件的资源包.

BalusC provided a good answer to a question about how to set messages per composite component. Specifying different error messages for different composite components The answer involved using per component resource bundles.

我已经应用了这种方法,但是当我在消息中使用{0}指定当前组件标签作为消息的一部分时,{0}会按照我的错误呈现,而不是被解决.使用这种方法时,可以使用这种分辨率的标签吗?还是我必须在复合组件中手动建立错误消息?例如

Ive applied this approach but when I use {0} in the message to specify the current component label as part of the message, the {0} is rendered as is in my error, rather than being resolved. Is this resolution of labels available when I use this approach? Or do I have to manually build up the error message in the composite component? e.g.

requiredMessage="#{cc.resourceBundleMap.requiredMessage} #{cc.attrs.prefix} #{cc.attrs.label}"

我猜更深层的问题是如何在资源包中设置参数化的消息字符串?我需要构建某种自定义消息解析器吗?

I guess the deeper question is how can I have parameterised message strings in my resource bundle? Will I need to build some kind of custom message resolver?

推荐答案

不幸的是,默认情况下无法对requiredMessage(以及validatorMessageconverterMessage)属性进行参数设置.我认为无论如何它们都是毫无意义的,因为它们特别与 specific 组件相关联.复合组件通常不包含多个 same 组件.您需要为每个单独的组件分别指定消息.

Unfortunately, the requiredMessage (and validatorMessage and converterMessage) attribues are not parameterizable by default. I think it would make little sense anyway as they are tied to a specific component in particular. A composite component does usually not contain a multiple of the same components. You would need to specify separate messages for every individual component.

但是从理论上讲,可以创建自定义的EL函数完成消息格式化工作.该方法将如下所示:

It's in theory however possible to create a custom EL function which does the message formatting job. The method would look something like this:

public static String format(String message, Object argument1, Object argument2) {
    return MessageFormat.format(message, argument1, argument2);
}

将使用如下方式:

requiredMessage="#{util:format(cc.resourceBundleMap.requiredMessage, cc.attrs.prefix, cc.attrs.label)}"

唯一的缺点是您不能创建可以使用varargs的EL函数.这是EL规范的限制.因此,如果您打算能够传递变量参数,则需要为所需的每种可能数量的参数创建一个单独的EL函数和一个Java方法.

The only disadvantage is that you can't create EL functions which can take varargs. This is a limitation of the EL specification. So if you intend to be able to pass variable arguments, you'd need to create a separate EL function and a Java method for every possible amount of arguments needed.

作为完全不同的替代方法,您可以创建一个自定义Validator并将其附加到特定的输入组件.您甚至可以将验证器方法直接放在<cc:interface componentType>与复合组件关联的支持组件"中.如果删除required属性,则在您可以自由地按所需方式编写消息的地方,将立即调用此验证器.

As a completely different alternative, you could create a custom Validator and attach it to the particular input component. You can even put the validator method straight in the "backing component" which is associated with the composite component by <cc:interface componentType>. If you remove the required attribute, then this validator will immediately be invoked where you've the freedom to compose messages the way you want.

例如

<h:inputText id="foo" validator="#{cc.validateFoo}" requiredMessage="#{cc.resourceBundleMap.requiredMessage}" />

使用

public void validateFoo(FacesContext context, UIComponent component, Object value) {
    if (value == null || value.toString().trim().isEmpty()) {
        String requiredMessage = (String) component.getAttributes().get("requiredMessage");
        String prefix = (String) getAttributes().get("prefix");
        String label = (String) getAttributes().get("label");
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
            MessageFormat.format(requiredMessage, prefix, label), null));
    }
}

这篇关于如何在复合组件中参数化requiredMessage属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆