< h:selectManyListbox JSF和Enums Class Cast错误 [英] <h:selectManyListbox JSF and Enums Class Cast error

查看:123
本文介绍了< h:selectManyListbox JSF和Enums Class Cast错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我疯狂,找不到错误。

This drives me crazy, cannot find the error.

这里是xhtml页面:

Here the xhtml page:

...
<h:selectManyListbox style="width: 207px" size="10" value="#{reportBean.selectedSeverities}">
                            <f:selectItems value="#{reportBean.severities}"/>
                        </h:selectManyListbox>
...

报表Bean:

...
private List<Severity> severities;
private List<Severity> selectedSeverities = new ArrayList<Severity>();
...
public List<Severity> getSeverities() {
   if (this.severities == null) {
        this.severities = new ArrayList<Severity>();
        this.severities.add(Severity.LOW);
        this.severities.add(Severity.HIGH);
        this.severities.add(Severity.UNDEFINED);
        this.severities.add(Severity.MEDIUM);
    }
        return severities;
}

对于命令按钮,我有以下操作方法:

For a command Button I have the following action method:

if (!selectedSeverities.isEmpty()) {
    Severity s = selectedSeverities.get(0);
}
return;

Wenn我选择一个严重性(枚举)并点击命令按钮我得到以下堆栈跟踪: p>

Wenn I select a severity(enum) and hit the commandbutton I get the following stack trace:

...
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to securityscan.util.Severity
...

我没有得到它。

任何帮助都非常感谢。

BR Reen

推荐答案

您不能使用枚举与 h:selectMany *** 组合而不使用转换器。 JSF / EL看不到/知道每个单独的列表项的通用类型。换句话说,它只看到一个列表而不是列表< Severity> ,并将每个项目视为 String ,除非你以其他方式告诉它。

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.

你需要自己创建和指定一个转换器。对于枚举,最好扩展JSF提供的 EnumConverter

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);
    }

}

(注意当您仍然在使用旧的JSF 1.2,您应该在 faces-config.xml < converter> c>而不是通过 @FacesConverter

(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)

您使用的内容如下:

<h:selectManyListbox converter="severityConverter">



另请参见:




  • 如何在选择的许多菜单中使用枚举?

  • See also:

    • How to use enums in select many menus?
    • 这篇关于&lt; h:selectManyListbox JSF和Enums Class Cast错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆