parameters in custom converters lost on form submission

View: New views
3 Messages — Rating Filter:   Alert me  

parameters in custom converters lost on form submission

by bbjsjjs :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am trying to write a custom converter that takes a parameter (enum Class Name) and converts  a selected value to an enum of that type. My problem is that when the form is submitted, a new EnumConverter instance is created and the parameter value for enumClass is lost. I want the parameter value to be available to my converter after form submission. Can anyone tell me what I am doing wrong?

Form snippet:

<h:selectManyCheckbox value="#{searchOptions}" >
        <s:selectItems value="#{advertSearchOptions}" var="searchOption" label="#{messages[searchOption.key]}" />
        <myCompany:convertEnum enumClass="com.myCompany.server.myApp.advert.AdvertSearchOption" />
</h:selectManyCheckbox>

Converter:

public class EnumConverter implements Converter {

        private String enumClass;

        public EnumConverter() {
        }

        public Object getAsObject(FacesContext context, UIComponent comp, String value) {

                Class enumType;
                try {
                        enumType = Class.forName(enumClass);
                } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                }
                return Enum.valueOf(enumType, value);
        }

        public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
                if (object == null) {
                        return null;
                }

                return ((Enum) object).name();
        }

        public void setEnumClass(String enumClass) {
                this.enumClass = enumClass;
        }

        public String getEnumClass() {
                return enumClass;
        }
}

faces-config:

<converter>
        <converter-id>com.myCompany.server.myApp.EnumConverter</converter-id>
        <converter-class>com.myCompany.server.myApp.EnumConverter</converter-class>
</converter>

taglib.xml:

<tag>
        <tag-name>convertEnum</tag-name>
        <converter>        
                <converter-id>com.myCompany.server.myApp.EnumConverter</converter-id>
        </converter>
</tag>

Re: parameters in custom converters lost on form submission

by Asaf :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,

Are there any news regarding this issue?
I'm facing the same problem, parameters are not attached to the converter class,


Many thanks,


Asaf.
bbjsjjs wrote:
Hi,

I am trying to write a custom converter that takes a parameter (enum Class Name) and converts  a selected value to an enum of that type. My problem is that when the form is submitted, a new EnumConverter instance is created and the parameter value for enumClass is lost. I want the parameter value to be available to my converter after form submission. Can anyone tell me what I am doing wrong?

Form snippet:

<h:selectManyCheckbox value="#{searchOptions}" >
        <s:selectItems value="#{advertSearchOptions}" var="searchOption" label="#{messages[searchOption.key]}" />
        <myCompany:convertEnum enumClass="com.myCompany.server.myApp.advert.AdvertSearchOption" />
</h:selectManyCheckbox>

Converter:

public class EnumConverter implements Converter {

        private String enumClass;

        public EnumConverter() {
        }

        public Object getAsObject(FacesContext context, UIComponent comp, String value) {

                Class enumType;
                try {
                        enumType = Class.forName(enumClass);
                } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                }
                return Enum.valueOf(enumType, value);
        }

        public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
                if (object == null) {
                        return null;
                }

                return ((Enum) object).name();
        }

        public void setEnumClass(String enumClass) {
                this.enumClass = enumClass;
        }

        public String getEnumClass() {
                return enumClass;
        }
}

faces-config:

<converter>
        <converter-id>com.myCompany.server.myApp.EnumConverter</converter-id>
        <converter-class>com.myCompany.server.myApp.EnumConverter</converter-class>
</converter>

taglib.xml:

<tag>
        <tag-name>convertEnum</tag-name>
        <converter>        
                <converter-id>com.myCompany.server.myApp.EnumConverter</converter-id>
        </converter>
</tag>

Re: parameters in custom converters lost on form submission

by javamustang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi both,

I had problem my converter got parameter set only the first time, further page refreshing created new converters without parameter pering delivered.

Cause is converters and validators seem to loose state. Solution is describes here and works for me fine.
http://blog.projectnibble.org/tag/facelets-validators/