【问题标题】:Unable to auto-convert enum in JSF无法在 JSF 中自动转换枚举
【发布时间】:2019-06-22 19:32:10
【问题描述】:

根据这个答案:https://stackoverflow.com/a/8229982/988145,JSF 应该自动转换枚举。出于某种原因,它没有。我收到以下错误:

“'null Converter'的FrequencyConversion错误设置值'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;
}

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

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

【问题讨论】:

  • 请考虑提供任意版本信息和minimal reproducible example。例如,#{cellContentsBean.pillSheetProfile.frequency} 的目标未在您的问题中定义。

标签: jsf


【解决方案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>

【讨论】:

  • @Creature 所以只是你的 faces-config 导致了问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多