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

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

问题描述

我有一个JavaServer Faces页面和一个会话范围的托管bean,它有两个 ArrayList< Integer> 属性:一个用于保存可能值的列表,另一个用于保存选定值的列表。在JSF页面上有一个带有这两个属性的< h:selectManyListBox> 组件。



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


验证错误:值无效


问题:如何绑定 ArrayList< Integer> 属性到< h:selectManyListBox> 组件是否正常?



感谢您的帮助。 p>

具体代码



JSF页面:

 <?xml version ='1.0'encoding ='UTF-8'?> 
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
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}>
< / h:selectManyListbox>
< h:commandButton action =#{testBean.go}value =go/>
< ui:repeat value =#{testBean.selection}var =i>
#{i}:#{i.getClass()}
< / ui:repeat>
< / h:表格>
< / h:body>
< / html>

而托管bean:

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

@ javax.faces.bean.ManagedBean
@ javax.enterprise.context.SessionScoped
public class TestBean实现Serializable
{
private ArrayList< Integer> ;选择;
private ArrayList< Integer>列表;

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


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

public ArrayList< Integer> getSelection()
{
返回选择;


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

public String go()
{
//引发异常:java.lang.ClassCastException:java.lang.String不能转换为java .lang.Integer
/ * for(Integer i:selection)
{
System.out.println(i);
} * /
返回null;
}
}


解决方案

通用类型信息 List< Integer> 在运行时会丢失,因此仅查看 List 的JSF / EL无法使用以确定泛型类型是 Integer 并假定它是默认的 String (因为这是底层的默认类型 HttpServletRequest#getParameter()在应用请求值阶段调用)。您需要 >显式地指定 Converter ,可以使用JSF内置 IntegerConverter

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

只是使用整数[] 代替,它的类型信息在运行时显而易见:

  private Integer [] selection; 


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&lt; Integer&gt; JSF中的selectManyListbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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