【问题标题】:Composite JSF component with converter带转换器的复合 JSF 组件
【发布时间】:2015-04-15 08:51:18
【问题描述】:

我有一个复合 JSF 组件 TimePicker.xhtml 和一个支持组件 timePickerComponent,如下所示:

      <mi:timePicker style="display: initial;"
        widgetVar="toTimepickerWidget"
        converter="#{timePickerConverter}"
                            value="#{calendarBean.event.to}" 
      /> 

timePickerConverter 以通常的方式创建:

public class TimePickerConverter implements Converter, Serializable {
    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
            throws ConverterException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
            throws ConverterException {
        // TODO Auto-generated method stub
        return null;
    }
}

如何在复合组件中使用此转换器?

更新:

这是复合组件的代码:

<cc:implementation componentType="timePickerComponent">
    <h:outputScript name="js/timepicker/timepicker_helper.js" target="head"/>
    <div id="#{cc.clientId}" style="#{cc.attrs.style}">

            <p:inputText id="timepicker"
                    scrollHeight="200"
                    value="#{cc.timeLocal}"  
                    size="5"/>

    </div>
</cc:implementation>

基本上我想要的是将纯文本从inputText 转换为Date 对象。日期的部分与我无关,我只需要对象的时间部分。 顺便说一句,作为临时解决方案,我将使用 BalusC 的这篇文章中描述的 getConvertedValue Composite component with multiple input fields但也想知道如何将此功能委托给外部转换器,如文章

通常这个方法不会被覆盖,一切都是 委托给默认的 JSF 转换器机制

【问题讨论】:

  • 可以加TimePicker.xhtml吗?你的实际问题在哪里?
  • @Smutje,由于公司限制,我无法发布确切的代码 sn-p,所以我发布了具有相同想法的 MVCE,谢谢。
  • 我不太明白,为什么不把转换器提供给类似于&lt;p:inputText id="timepicker" ... converter="#{cc.attrs.converter}"/&gt;inputText
  • @Smutje,如果我们谈论的是 simple 复合组件当然是可能的,但是如果我们谈论的是具有多个输入字段的复合组件呢?多个&lt;p:inputText&gt;?在这种情况下,只有委托转换器可以成为解决方案。

标签: jsf jsf-2 converter composite-component


【解决方案1】:

为了使用转换器,您可以在方法getConvertedValue 的支持组件中显式调用转换器。转换器可以从组件的converter 属性中获取:

@FacesComponent("timePickerComponent")
public class TimePickerComponent extends UIInput implements NamingContainer {

    ...

    @Override
    public Object getSubmittedValue() {
        UIInput hourComp = (UIInput) findComponent("timepicker_hour");
        UIInput minutesComp = (UIInput) findComponent("timepicker_minutes");
        return hourComp.getSubmittedValue() + ":" + minutesComp.getSubmittedValue();
    }

    @Override
    protected Object getConvertedValue(FacesContext context,
            Object newSubmittedValue) throws ConverterException {
        Converter converter = (Converter) getAttributes().get("converter");
        if (converter != null) {
            return converter.getAsObject(context, this, (String) newSubmittedValue);
        } else {
            return newSubmittedValue;
        }
    }

}

查看示例代码: https://github.com/destin/SO-answers/tree/master/SO-composite-jsf-component-with-converter

但是,这种方法的缺点是 JSF 组件从 String 转换而来。据我了解,您的组件由几个子元素组成。它们的所有值都需要转换为字符串(就像我所做的那样:hourComp.getSubmittedValue() + ":" + minutesComp.getSubmittedValue())。

因此,如果 TimeConverters 独立于 JSF 组件,您可能更愿意定义自己的层次结构。这样的转换器将能够使用很少的参数或一些复杂的对象(如Time)作为源。可以用与我完全相同的方式检索此类转换器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2011-09-09
    • 2012-10-12
    • 2011-07-23
    • 2013-10-17
    • 2015-11-22
    • 2011-06-27
    相关资源
    最近更新 更多