gpt4 book ai didi

jsf - 在 Primefaces SelectOneMenu 中传递对象值

转载 作者:行者123 更新时间:2023-12-02 17:24:00 25 4
gpt4 key购买 nike

您好,我正在使用 PrimeFaces 4.0,我需要在 SelectOneMenu 中传递对象值。我正在使用转换器将其从字符串格式转换为类对象格式。这些是代码文件请帮助我...

lablevalue.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form id="myform">
<p:growl showDetail="true"></p:growl>
<p:selectOneMenu value="#{itemlableAcction.idCard}" >
<f:converter converterId="converter.SelectMenUConverter" />
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItems value="#{itemlableAcction.idCards}" var="idv" itemLabel="#{idv.name}" itemValue="#{idv}" />
</p:selectOneMenu>
<h:commandButton action="#{itemlableAcction.onclickSubmit}" value="Submit"></h:commandButton>
</h:form>
</h:body>
</html>

ItemlableAction

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import bo.IdCard;

@ManagedBean
public class ItemlableAcction {

List<IdCard> idCards = new ArrayList<IdCard>();

IdCard idCard;

public Object getIdCard() {
return idCard;
}

public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}

public List<IdCard> getIdCards() {
return idCards;
}

public void setIdCards(List<IdCard> idCards) {
this.idCards = idCards;
}

public ItemlableAcction() {
IdCard card1 = new IdCard();
card1.setId(1);
card1.setName("ABC");
card1.setAddress("USA");
idCards.add(card1);
IdCard card2 = new IdCard();
card2.setId(2);
card2.setName("MNO");
card2.setAddress("INDIA");
idCards.add(card2);
IdCard card3 = new IdCard();
card3.setId(3);
card3.setName("XYZ");
card3.setAddress("Chaina");
idCards.add(card3);
}

public String onclickSubmit() {
IdCard ic = (IdCard) idCard;
System.out.println("In action id values are " + ic.getId() + " " + ic.getAddress());
return "";
}

}

选择MenUConverter

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import bo.IdCard;

@FacesConverter("converter.SelectMenUConverter")
public class SelectMenUConverter implements Converter {

public SelectMenUConverter() {
System.out.println("Inside converter");
}

public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
System.out.println("One" + arg2);
IdCard idCard = new IdCard(arg2);
return idCard;
}

public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
System.out.println("Two" + value);
return value.toString();
}

}

身份证

public class IdCard {

String name;

int id;

String address;

public IdCard() {
}

public IdCard(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}

最佳答案

假设您必须为 100 个类实现转换器。

如果您不想实现自己的转换器并从现有列表中获取数据,请使用:

SelectItems Converter Omnifaces

您有一个完整的示例。

PD:不要忘记使用唯一的 id 实现 toString。(请参阅文档)

关于jsf - 在 Primefaces SelectOneMenu 中传递对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com