无法在JSF中自动转换枚举

时间:2019-06-22 19:32:10

标签: jsf

根据以下答案:https://stackoverflow.com/a/8229982/988145,JSF应该自动转换枚举。由于某种原因,事实并非如此。我收到以下错误:

““空转换器”的FrequencyConversionError类型设置值“ DAY_OF_WEEK”。”

我的枚举:

public enum FrequencyType implements Serializable
{
    DAY_NUMBER, DAY_OF_WEEK
}

选择标记:

<h:selectOneMenu onchange="toggleFrequencyTypes(this);" 
         value="#{cellContentsBean.pillSheetProfile.frequency}" 
         class="form-control" id="frequencyTypeDd">
    <f:selectItems value="#{cellContentsBean.frequencyTypes}" />
</h:selectOneMenu>

bean中的频率类型getter:

public FrequencyType[] getFrequencyTypes() {
    return FrequencyType.values();
}

设置者:

private FrequencyType frequencyType;

/**
 * @return the frequencyType
 */
public FrequencyType getFrequencyType()
{
    return frequencyType;
}

/**
 * @param frequencyType the frequencyType to set
 */
public void setFrequencyType(FrequencyType frequencyType)
{
    this.frequencyType = frequencyType;
}

我什至按照另一个线程的建议在我的faces配置中添加了一个转换器,但是它什么也没做:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

        <converter>
            <converter-for-class>java.lang.Enum</converter-for-class>
            <converter-class>javax.faces.convert.EnumConverter</converter-class>
        </converter>
    </application>
</faces-config>

1 个答案:

答案 0 :(得分:1)

虽然这可能无法解决您的问题,但我必须注意,您的faces-config.xml有点破损:

  1. The JSF Namespaces you declared do not exist this way
  2. 错误的元素嵌套:converter元素不是application元素的子元素。

最好尝试一下:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

    <converter>
        <converter-for-class>java.lang.Enum</converter-for-class>
        <converter-class>javax.faces.convert.EnumConverter</converter-class>
    </converter>
</faces-config>