我想动态创建我的 jsf 页面的内容。我要做的是构建一个组件编辑器。我在页面上放置了两个按钮。这是代码:
//Various imports;
@ManagedBean
@ViewScoped
public class Elpad implements Serializable{
private String padreCorrente = "Nessun padre è stato creato";
private HtmlPanelGrid customGrid;
private UIComponent padreCorrenteC;
private List<SelectItem> items = new ArrayList<SelectItem>();
private List<UISelectItems> listaItem = new ArrayList<UISelectItems>();
private String itemScelto = "";
public Elpad() {
//items = new ArrayList<SelectItem>();
//items.add(0, new SelectItem("poba", "poba"));
//items.add(1, new SelectItem("ripoba", "ripoba"));
}
public String getItemScelto() {
return itemScelto;
}
public void setItemScelto(String itemScelto) {
this.itemScelto = itemScelto;
}
private String ris ="";
public List<SelectItem> getItems() {
return items;
}
public void setItems(List<SelectItem> items) {
this.items = items;
}
public List<UISelectItems> getListaItem() {
return listaItem;
}
public void setListaItem(List<UISelectItems> listaItem) {
this.listaItem = listaItem;
}
public HtmlRadio getSelectedRadio() {
return selectedRadio;
}
public void setSelectedRadio(HtmlRadio selectedRadio) {
this.selectedRadio = selectedRadio;
}
public String getPadreCorrente() {
return padreCorrente;
}
public void setPadreCorrente(String padreCorrente) {
this.padreCorrente = padreCorrente;
}
public HtmlPanelGrid getCustomGrid() {
return customGrid;
}
public void setCustomGrid(HtmlPanelGrid customGrid) {
this.customGrid = customGrid;
}
public void aggiungiPadre(ActionEvent evt) {
HtmlSelectOneListbox sor = new HtmlSelectOneListbox();
FacesContext fc = FacesContext.getCurrentInstance();
ELContext elcntx = fc.getELContext();
Application app = fc.getApplication();
ExpressionFactory xprfct = app.getExpressionFactory();
ValueExpression ve = xprfct.createValueExpression(elcntx,"#{elpad.itemScelto}", String.class);
ve.getValue(FacesContext.getCurrentInstance().getELContext());
sor.setValueExpression("value", ve);
log.error("Il componente HtmlSelectOneListbox sor è stato creato con ID : " + sor.getClientId() + " e ValueExpression : " + sor.getValueExpression("value") + " quindi ha valore : " + sor.getValue());
UISelectItems a = new UISelectItems();
items.add(0, new SelectItem("poba", "poba"));
ValueExpression ve1 = xprfct.createValueExpression(elcntx,"#{elpad.items}", ArrayList.class);
ve1.getValue(FacesContext.getCurrentInstance().getELContext());
a.setValueExpression("valuee", ve1);
log.error("Il componente UISelectItems a è stato creato con ID : " + a.getClientId() + " e ValueExpression : " + a.getValueExpression("valuee") + " quindi ha valore : " + a.getValue() + " per l'attributo items : " + items.get(0).getLabel() + " di dimensione : " + items.size() + " ;" );
sor.getChildren().add(a);
customGrid.getChildren().add(sor);
padreCorrenteC = sor;
/*FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory()
.createValueExpression(elContext, "#{elpad.items}", String.class);
valueExpression.setValue(elContext, ris);*/
padreCorrente = padreCorrenteC.getId();
}
public void aggiungiFiglio(ActionEvent evt) {
items.add(new SelectItem(padreCorrente, padreCorrente.toString()));
}
}
那就是我的 XHTML 页面
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:icecore="http://www.icefaces.org/icefaces/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:ice="http://www.icesoft.com/icefaces/component"
>
<h:head>
<title>Cambia padre</title>
</h:head>
<h:body>
<ice:form>
<ice:panelGrid binding="#{elpad.customGrid}">
</ice:panelGrid>
<ice:commandButton value="Aggiungi padre" actionListener="# {elpad.aggiungiPadre}"></ice:commandButton>
<ice:commandButton value="Aggiungi figlio" actionListener="# {elpad.aggiungiFiglio}"></ice:commandButton>
</ice:form>
<ice:outputLabel value="#{elpad.padreCorrente}"></ice:outputLabel>
</h:body>
</html>
有人能帮我吗?我要做的是使用第一个按钮创建一个带有 SelectItems 子项的 SelectOneList,并使用第二个按钮将项目添加到列表中。我认为我在创建第二个 ValueExpression 时遗漏了一些东西(因为我尝试使用记录器进行一些测试并返回 null)但我不知道是什么