为什么单个价值持有者不允许多个转换器?

发布于 2024-11-19 22:46:18 字数 121 浏览 3 评论 0原文

今天早上,我有理由尝试在 inputText 组件上使用多个转换器,并意识到这不起作用。

有人知道为什么 JSF 只允许每个 ValueHolder 使用一个转换器吗?在某些情况下,使用一系列转换器似乎会很优雅。

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

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.

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

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

发布评论

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

评论(1

空‖城人不在 2024-11-26 22:46:18

在 JSF 中, Converter 接口是设计的唯一目的如下:

Converter 是一个描述 Java 类的接口,该类可以在模型数据对象和适合呈现的对象的字符串表示形式之间执行对象到字符串和字符串到对象的转换。

...

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

getAsString 将与指定 UIComponent 关联的指定模型对象值转换为适合包含在 请求处理生命周期的渲染响应阶段。


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

最好的办法是在实施层面解决这个问题。如果您想扩展现有转换器,那么您应该这样做,然后在适当的时候调用 super 方法。例如

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

    // ...
}

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

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

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

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

    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文