Why aren't multiple Converters allowed on a single value holder?

半腔热情 提交于 2019-12-17 22:00:55

问题


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.


回答1:


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

    // ...
}


来源:https://stackoverflow.com/questions/6667394/why-arent-multiple-converters-allowed-on-a-single-value-holder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!