« Return to Thread: parameters in custom converters lost on form submission

Re: parameters in custom converters lost on form submission

by Asaf :: Rate this Message:

Reply to Author | View in Thread

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>

 « Return to Thread: parameters in custom converters lost on form submission