Mic*_*lin 5 jsf converter faces-config managed-bean
我遇到困难,需要JSF专家的外界帮助,遇到以下问题:我在faces-config.xml中为特定类定义了一些转换器,所以我不必一直使用converter-attribute或tag.例如:
<converter>
<converter-for-class>org.joda.time.DateTime</converter-for-class>
<converter-class>com.example.converter.JodaDateTimeConverter</converter-class>
</converter>
Run Code Online (Sandbox Code Playgroud)
现在需要一个JSF-Component(主要是rich:extendedDataTable)的爬虫,它构建整个组件树并逐级转换为CSV,HTML或稍后可能需要的任何东西.即导出为CSV,HTML,...的通用方法,无需每次重新实现它.这几乎已经完成了(感谢我的一位老同事的好主意),除了一部分之外它确实很有效:
Object expressionResult = expression.getValue(FacesContext.getCurrentInstance().getELContext());
expressionResultString = expressionResult.toString();
Run Code Online (Sandbox Code Playgroud)
该命令检索h:outputText的值并将其转换为String.如果有针对特定expressionResult的自定义转换器,那么最后一行就是我想要用converter-for-class替换的.我无法找到如何为我的类找到精确的转换器(由faces-config指定).FacesContext似乎没有为我的用例提供任何有用的方法/对象.直接访问faces-config.xml似乎有点不对劲.正确的方法可能类似于:
Converter converter = magically_fetch_the_correct_converter_for_expressionResult_type;
converter.getAsString(FacesContext.getCurrentInstance(), component,
expressionResult);
Run Code Online (Sandbox Code Playgroud)
如果我用转换器-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);
Run Code Online (Sandbox Code Playgroud)