【问题标题】:How to get label of selected item in managed bean [duplicate]如何在托管bean中获取所选项目的标签[重复]
【发布时间】:2015-03-06 08:13:29
【问题描述】:

我需要您的帮助才能将列表的值转换为两个变量。我的清单有描述和代码。但是,我需要将描述放在一个变量中,将代码放在另一个变量中,那么我该如何实现呢。

我的代码是

private String[] selectedCertificates;
private List<SelectItem> Certificates;

    public List<SelectItem> getCertificatesList(){
    Certificates = new ArrayList<SelectItem>();
    Certificates.add(new SelectItem("Certificate A","A"));
    Certificates.add(new SelectItem("Certificate B","B"));
    return bankCertificates;

}

public void setCertificates(List<SelectItem> Certificates) {
    this.Certificates = Certificates;
}
// Setters and Getters

选择项目代码:

                         <p:selectManyCheckbox id="Certificates" value="#{user.selectedCertificates}"
                                              layout="pageDirection" disabled="#{user.secondToggle}">
                            <f:selectItems value="#{user.Certificates}" var="bankCertificates"
                                           itemLabel="#{user.CertificatesString}" itemValue="#{user.CertificatesCode}"/>
                        </p:selectManyCheckbox>

我在哪里可以定义描述应该是第一个值,代码应该是列表中的第二个值,我可以在页面中使用它们。

谢谢

【问题讨论】:

  • 考虑使用HashMap
  • 我需要使用列表
  • 如何得到...列表成2个变量... brrr,可能是我太笨了,但我无法理解这个编程问题
  • 如果 SelectItem 是一个 bean,您可以使用它的 getter 将描述和代码存储在单独的变量中。
  • 能否发布您的 SelectItem 代码?

标签: jsf label selectmanycheckbox


【解决方案1】:

试试

class SelectItem {
    private String code;
    private String description;

    SelectItem (String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode () {
        return code;
    }
    public String getDescription () {
        return description;
    }
}

这是你的主要课程

class MainClass {
    public static void main (String...arg) {
        //construct your list here using SelectItem class objects
        List<SelectItem> certificates =  = new ArrayList<SelectItem>();
        certificates.add(new SelectItem("Certificate A","A"));
        certificates.add(new SelectItem("Certificate B","B"));

        //now first read the SelectItem objects you have added to the list
        //or you can also iterate through the list, modify accordingly
        SelectItem si1 = certificates.get(0);
        //to read the code and description use the getters defined in SelectItem
        si1.getCode(); si1.getDescription();
    }
}

您还可以选择创建一个方法,您可以将希望从列表中读取的索引传递给该方法。希望,这会有所帮助。

【讨论】:

    【解决方案2】:

    如果您的 SelectItem bean 上有 getter(我假设您有字段描述和代码),请尝试如下操作,它会存储您的对象字段,该字段位于您的 ArrayList 中的第一个位置。

    String description = Certificates.get(0).getDescription();
    String code = Certificates.get(0).getCode();
    

    【讨论】:

    • 它会只得到一个值还是在一个列表中?我可以用同一个 bean 做吗?
    • 它将获取对象字段的值并将它们存储到您的描述和代码变量中,您可以在需要的地方执行此操作,如果您愿意,可以在页面中声明变量并使用您的初始化它们列表中包含的对象字段,或者您可以在控制器(或您拥有的任何东西)中初始化它们并仅将描述和代码变量传递给页面(但您需要确切知道您需要从列表中获取哪个对象,在它位于哪个索引)。
    猜你喜欢
    • 2017-06-14
    • 2021-10-18
    • 1970-01-01
    • 2013-01-26
    • 2015-10-24
    • 2016-04-21
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多