<h:selectManyListbox JSF and Enums Class Cast error

前端 未结 1 1016
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 10:39

This drives me crazy, cannot find the error.

Here the xhtml page:

...


        
相关标签:
1条回答
  • 2021-01-14 11:18

    You can't use enums in combination with h:selectMany*** components without using a converter. JSF/EL does not see/know the generic type of each of the separate list items. In other words, it only sees a List and not List<Severity> and treats every item as a String, unless you tell it to do otherwise.

    You need to create and specify a converter yourself. For enums, it's the best to extend the JSF-provided EnumConverter.

    package com.example;
    
    import javax.faces.convert.EnumConverter;
    import javax.faces.convert.FacesConverter;
    
    @FacesConverter(value="severityConverter")
    public class SeverityConverter extends EnumConverter {
    
        public SeverityConverter() {
            super(Severity.class);
        }
    
    }
    

    (note that when you're still using the old JSF 1.2, you should be declaring this as <converter> in faces-config.xml instead of by @FacesConverter)

    Which you use as follows:

    <h:selectManyListbox converter="severityConverter">
    

    See also:

    • How to use enums in select many menus?
    0 讨论(0)
提交回复
热议问题