gpt4 book ai didi

jsf - 为 ManagedBean (JSF 1.2) 中的类获取 JSF 转换器

转载 作者:行者123 更新时间:2023-12-04 17:38:34 25 4
gpt4 key购买 nike

我被困住了,需要 JSF 专家的外部帮助,解决以下问题:
我在faces-config.xml 中为特定类定义了一些转换器,因此我不必一直使用converter-attribute 或标记。例如:

    <converter>
<converter-for-class>org.joda.time.DateTime</converter-for-class>
<converter-class>com.example.converter.JodaDateTimeConverter</converter-class>
</converter>

现在需要一个 JSF 组件(主要是丰富的:extendedDataTable)的爬虫,它构建整个组件树并将一层又一层地转换为 CSV、HTML 或以后可能需要的任何东西。即导出到 CSV、HTML 等的通用方法,无需每次都重新实现。它几乎完成了(感谢我的一位老同事的好主意)并且它确实工作得很好,除了一个部分:
    Object expressionResult = expression.getValue(FacesContext.getCurrentInstance().getELContext());
expressionResultString = expressionResult.toString();

该命令检索 h:outputText 的值并将其转换为字符串。如果有针对特定 expressionResult 的自定义转换器,那么最后一行就是我想用转换器换类替换的内容。我不知道如何为我的类找到那个确切的转换器(由faces-config 指定)。对于我的用例,FacesContext 似乎没有任何有用的方法/对象。直接访问 faces-config.xml 似乎有点错误。正确的方法可能类似于:
    Converter converter = magically_fetch_the_correct_converter_for_expressionResult_type;
converter.getAsString(FacesContext.getCurrentInstance(), component,
expressionResult);

如果我使用转换器 ID 和组件本身的适当属性/标签,那将相当容易,但我真的想避免那种无用的附加代码。

有人可以帮助我吗?

最佳答案

您正在寻找 Application#createConverter() .

Object object = expression.getValue(context.getELContext());
Class<?> type = expression.getType(context.getELContext());
Converter converter = context.getApplication().createConverter(type);
String string = converter.getAsString(context, component, object);

关于jsf - 为 ManagedBean (JSF 1.2) 中的类获取 JSF 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10500085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com