如何在托管bean中获取所选项目的标签
How to get label of selected item in managed bean
我需要你的帮助来将列表的值转换为两个变量。我的清单有描述和代码。但是,我需要将描述放在一个变量中,将代码放在另一个变量中,所以我该如何实现。
我的代码是
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
Select 商品代码:
<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>
我在哪里可以定义描述应该是第一个值,代码应该是列表中的第二个值,我可以在页面中使用它们。
谢谢
如果您的 SelectItem bean 上有 getter(我假设您有字段描述和代码),请尝试按照以下步骤操作,它存储您的对象字段,它位于您的 ArrayList 中的第一个位置。
String description = Certificates.get(0).getDescription();
String code = Certificates.get(0).getCode();
尝试
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
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();
}
}
您还可以选择创建一个方法,您可以将要从列表中读取的索引传递给该方法。希望,这有帮助。
我需要你的帮助来将列表的值转换为两个变量。我的清单有描述和代码。但是,我需要将描述放在一个变量中,将代码放在另一个变量中,所以我该如何实现。
我的代码是
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
Select 商品代码:
<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>
我在哪里可以定义描述应该是第一个值,代码应该是列表中的第二个值,我可以在页面中使用它们。
谢谢
如果您的 SelectItem bean 上有 getter(我假设您有字段描述和代码),请尝试按照以下步骤操作,它存储您的对象字段,它位于您的 ArrayList 中的第一个位置。
String description = Certificates.get(0).getDescription();
String code = Certificates.get(0).getCode();
尝试
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
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();
}
}
您还可以选择创建一个方法,您可以将要从列表中读取的索引传递给该方法。希望,这有帮助。