为什么单个值持有者上不允许使用多个转换器? [英] Why aren't multiple Converters allowed on a single value holder?

查看:50
本文介绍了为什么单个值持有者上不允许使用多个转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上,我有理由尝试在inputText组件上使用多个Converter,并且意识到这是行不通的.

This morning I had reason to try using multiple Converters on an inputText component and realized that this doesn't work.

为什么有人为什么JSF每个ValueHolder只允许一个Converter?在某些情况下,使用一系列Converter似乎很不错.

Does anyone who why JSF only allows a single Converter per ValueHolder? It seems that using a series of Converters would be elegant in several situations.

推荐答案

在JSF中,

In JSF, the Converter interface is designed with the following sole purpose:

转换器是一个接口,它描述一个Java类,该类可以在模型数据对象以及适合呈现的那些对象的String表示形式之间执行Object-to-String和String-to-Object转换.

Converter is an interface describing a Java class that can perform Object-to-String and String-to-Object conversions between model data objects and a String representation of those objects that is suitable for rendering.

...

getAsObject将与指定的UIComponent关联的指定的字符串值转换为适合在请求处理的 Apply Request Values 阶段存储的模型数据对象生命周期.

getAsObject Convert the specified string value, which is associated with the specified UIComponent, into a model data object that is appropriate for being stored during the Apply Request Values phase of the request processing lifecycle.

getAsString将与指定的UIComponent关联的指定的模型对象值转换为适合于包含在该对象的 Render Response 阶段生成的响应中的String.请求处理生命.

getAsString Convert the specified model object value, which is associated with the specified UIComponent, into a String that is suitable for being included in the response generated during the Render Response phase of the request processing lifeycle.

javadoc和JSF规范都没有提到链接转换器的可能性.

Neither the javadoc nor the JSF spec tells about the possibility to chain converters.

您最好的选择是在实施级别解决此问题.如果要扩展现有的转换器,则应该这样做,然后在适当的时候调用super方法.例如

Your best bet is to solve this problem at the implementation level. If you want to extend an existing converter, then you should just do so and then call super methods whenever appropriate. E.g.

public class SomeExtendedConverter extends SomeBasicConverter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Object basicConvertedValue = super.getAsObject(context, component, value);

        // ... manipulate more ...

        return extendedConvertedValue;
    }

    // ...
}

这篇关于为什么单个值持有者上不允许使用多个转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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