简体   繁体   English

带转换器的复合JSF组件

[英]Composite JSF component with converter

I have composite JSF component TimePicker.xhtml with a backing component timePickerComponent which is used as below: 我有一个复合JSF组件TimePicker.xhtml与一个支持组件timePickerComponent ,使用如下:

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

And timePickerConverter created in usual way: 并且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;
    }
}

How can I use this converter in composite component? 如何在复合组件中使用此转换器?

UPDATE: 更新:

This is code of the Composite Component: 这是复合组件的代码:

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

Basically what do I want is to convert plain text from inputText to Date object. 基本上我想要的是将纯文本从inputText转换为Date对象。 Date's part irrelevant for me, I only need Time's part of the object. 日期的部分与我无关,我只需要Time的一部分。 Btw, as a temporary solution I'll use getConvertedValue as described in this article from BalusC Composite component with multiple input fields But would like to know how to delegate this functionality to an external converter too, as described in the article 顺便说一下,作为临时解决方案,我将使用本文所述的带有多个输入字段的 BalusC Composite组件的 getConvertedValue但是想知道如何将此功能委托给外部转换器,如本文所述

Normally this method is not to be overridden and everything is delegated to default JSF Converter mechanisms 通常,不会覆盖此方法,并将所有内容委托给默认的JSF Converter机制

In order to use converters you can invoke converter explicitly in your backing component in method getConvertedValue . 为了使用转换器,您可以在方法getConvertedValue中的后台组件中显式调用转换器。 The converter could be retrieved from component's converter attribute: 可以从组件的converter属性中检索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;
        }
    }

}

See example code: https://github.com/destin/SO-answers/tree/master/SO-composite-jsf-component-with-converter 请参阅示例代码: https//github.com/destin/SO-answers/tree/master/SO-composite-jsf-component-with-converter

However, drawback of this method is that JSF components convert from String. 但是,此方法的缺点是JSF组件从String转换。 As I understand your component consists of few subelements. 据我所知,你的组件包含很少的子元素。 All their values needs to be converted to string (like I did: hourComp.getSubmittedValue() + ":" + minutesComp.getSubmittedValue() ). 他们所有的值都需要转换为字符串(就像我做的那样: hourComp.getSubmittedValue() + ":" + minutesComp.getSubmittedValue() )。

So maybe you would prefer to define your own hierarchy if TimeConverters independent of JSF components. 因此,如果TimeConverters独立于JSF组件,您可能更愿意定义自己的层次结构。 Such converters would be able to use few parameters or some complex object (like Time ) as a source. 这样的转换器将能够使用少量参数或一些复杂对象(如Time )作为源。 Such converters could be retrieved in exactly the same way as I did. 这样的转换器可以用与我完全相同的方式检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM