如何绑定List<Integer>JSF 中 selectManyListbox 的值 [英] How to bind List&lt;Integer&gt; values to selectManyListbox in JSF

查看:28
本文介绍了如何绑定List<Integer>JSF 中 selectManyListbox 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个 JavaServer Faces 页面和一个会话范围的托管 bean,它具有两个 ArrayList 属性:一个用于保存可能值的列表,另一个用于保存选定值的列表.在 JSF 页面上有一个 组件,绑定了这两个属性.

问题:提交表单后,选中的值将被转换为字符串(ArrayList 类型的属性实际上包含几个字符串!);但是,当我使用转换器时,会收到如下错误消息:

<块引用>

验证错误:值无效

问题:如何将 ArrayList 属性正确绑定到 组件?>

感谢您对我的帮助.

具体代码

JSF 页面:

<html xmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core"><h:body><h:形式><h:selectManyListbox value="#{testBean.selection}"><f:selectItems value="#{testBean.list}"></f:selectItems></h:selectManyListbox><h:commandButton action="#{testBean.go}" value="go"/><ui:repeat value="#{testBean.selection}" var="i">#{i}: #{i.getClass()}</ui:repeat></h:form></h:body>

和托管 bean:

import java.io.Serializable;导入 java.util.ArrayList;@javax.faces.bean.ManagedBean@javax.enterprise.context.SessionScoped公共类 TestBean 实现了可序列化{私有 ArrayList选择;私有 ArrayList列表;公共 ArrayList获取列表(){if(list == null || list.isEmpty()){list = new ArrayList();列表.添加(1);列表.添加(2);列表.添加(3);}退货清单;}public void setList(ArrayList list){this.list = 列表;}公共 ArrayList获取选择(){返回选择;}public void setSelection(ArrayList selection){this.selection = 选择;}公共字符串 go(){//这会抛出一个异常:java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer/*for (整数 i : 选择){System.out.println(i);}*/返回空;}}

解决方案

List的泛型类型信息在运行时丢失,因此JSF/EL只能看到List 无法识别泛型类型是 Integer 并假定它是默认的 String(因为这是底层 HttpServletRequest#getParameter() 在应用请求值阶段调用).

您需要要么明确指定一个Converter,您可以使用JSF内置IntegerConverter:

只是使用 Integer[] 代替,其类型信息在运行时是清楚的:

私有整数[]选择;

The situation: I have a JavaServer Faces page and a session-scoped managed bean that has two ArrayList<Integer> properties: one for holding a list of possible values and another for holding a list of selected values. On the JSF page there is a <h:selectManyListBox> component with these two properties bound.

The problem: after submitting the form the selected values will be converted to string (the property of type ArrayList actually holds a couple of strings!); however, when I use a converter, I get an error message like this:

Validation Error: Value is not valid

The question: How can I bind an ArrayList<Integer> property to the <h:selectManyListBox> component properly?

Thank you for your kind helping me.

The concrete codes

The JSF page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
        <h:form>
            <h:selectManyListbox value="#{testBean.selection}">
                <f:selectItems value="#{testBean.list}"></f:selectItems>
            </h:selectManyListbox>
            <h:commandButton action="#{testBean.go}" value="go" />
            <ui:repeat value="#{testBean.selection}" var="i">
                #{i}: #{i.getClass()}
            </ui:repeat>
        </h:form>
    </h:body>
</html>

And the managed bean:

import java.io.Serializable;
import java.util.ArrayList;

@javax.faces.bean.ManagedBean
@javax.enterprise.context.SessionScoped
public class TestBean implements Serializable
{
    private ArrayList<Integer> selection;
    private ArrayList<Integer> list;

    public ArrayList<Integer> getList()
    {
        if(list == null || list.isEmpty())
        {
            list = new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(3);
        }
        return list;
    }

    public void setList(ArrayList<Integer> list)
    {
        this.list = list;
    }

    public ArrayList<Integer> getSelection()
    {
        return selection;
    }

    public void setSelection(ArrayList<Integer> selection)
    {
        this.selection = selection;
    }

    public String go()
    {
            // This throws an exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
            /*for (Integer i : selection)
            {
                System.out.println(i);
            }*/
        return null;
    }
}

解决方案

The generic type information of List<Integer> is lost during runtime and therefore JSF/EL who sees only List is not able to identify that the generic type is Integer and assumes it to be default String (as that's the default type of the underlying HttpServletRequest#getParameter() call during apply request values phase).

You need either to explicitly specify a Converter, you can use JSF builtin IntegerConverter:

<h:selectManyListbox ... converter="javax.faces.Integer">

or just to use Integer[] instead, whose type information is clearly known during runtime:

private Integer[] selection;

这篇关于如何绑定List<Integer>JSF 中 selectManyListbox 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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