1.Introudction
Petite démonstration de la création dynamique de composants avec JSF plus précisement Icefaces… Qui a dit que java c’était lourd et compliqué?
2. Notre page xhtml, du moins la partie qui nous intéresse
<ice:form>
<ice:panelGrid id="grid" binding="#{dynamicgrid.containerComponent}" columns="2">
</ice:panelGrid>
<ice:commandButton value="Add UI Components" action="#{dynamicgrid.addComponent}" partialSubmit="true"/>
</ice:form>
<ice:panelGrid id="grid" binding="#{dynamicgrid.containerComponent}" columns="2">
</ice:panelGrid>
<ice:commandButton value="Add UI Components" action="#{dynamicgrid.addComponent}" partialSubmit="true"/>
</ice:form>
3. le backing bean
public class DynamicGrid {
public DynamicGrid(){
}
private HtmlPanelGrid containerComponent;
public void addComponent() {
//clean previous component
containerComponent.getChildren().clear();
//dynamically add Child Components to Container Component
for (int i=0;i<10;i++) {
UIColumn col = new UIColumn();
HtmlOutputText ot = new HtmlOutputText();
ot.setValue("test");
col.getChildren().add(ot);
HtmlInputText it = new HtmlInputText();
it.setValue("");
it.setId("label");
col.getChildren().add(it);
if (containerComponent == null) {
containerComponent = new HtmlPanelGrid();
}
containerComponent.getChildren().add(col);
}
}
public HtmlPanelGrid getContainerComponent() {
return containerComponent;
}
public void setContainerComponent(HtmlPanelGrid containerComponent) {
this.containerComponent = containerComponent;
}
}
public DynamicGrid(){
}
private HtmlPanelGrid containerComponent;
public void addComponent() {
//clean previous component
containerComponent.getChildren().clear();
//dynamically add Child Components to Container Component
for (int i=0;i<10;i++) {
UIColumn col = new UIColumn();
HtmlOutputText ot = new HtmlOutputText();
ot.setValue("test");
col.getChildren().add(ot);
HtmlInputText it = new HtmlInputText();
it.setValue("");
it.setId("label");
col.getChildren().add(it);
if (containerComponent == null) {
containerComponent = new HtmlPanelGrid();
}
containerComponent.getChildren().add(col);
}
}
public HtmlPanelGrid getContainerComponent() {
return containerComponent;
}
public void setContainerComponent(HtmlPanelGrid containerComponent) {
this.containerComponent = containerComponent;
}
}
4. Et sans oublier le faces-config.xml
<managed-bean>
<managed-bean-name>dynamicgrid</managed-bean-name>
<managed-bean-class>ch.devit.dynamic.DynamicGrid</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean-name>dynamicgrid</managed-bean-name>
<managed-bean-class>ch.devit.dynamic.DynamicGrid</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
merci fabou3377
intéressant d’avoir plus d’info sur JSF, mes dernières investigations dans le domaine commençant à dater ! D’ailleurs, dans l’absolu, un peu de contexte avec notamment les frameworks précis que tu recommandes serait aussi pas mal
bonne journée
++
joseph