I would like a JSF menu with h:selectOneRadio and information of Java ArrayList. This is my code:
XHTML:
<h:selectOneRadio value="#{bean.element}">
<ui:repeat value = "#{bean.items}" var = "i">
<f:selectItem itemValue="#{bean.elements[i]}" itemLabel="#{bean.elements[i]}" />
</ui:repeat>
</h:selectOneRadio>
Bean:
package bean.controlador;
import java.io.IOException;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@SessionScoped
public class Bean {
private String element;
private ArrayList<Integer> items;
private ArrayList<String> elements;
public Bean() {
element = "hello";
elements = new ArrayList<String>();
elements.add("world");
elements.add("hello");
elements.add("thanks");
items = new ArrayList<Integer>();
items.add(1);
items.add(2);
items.add(3);
}
public void load() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("link.xhtml");
}
public void listenerFuncion() {
System.out.println("Listener");
}
public String getElement() {
return element;
}
public void setElement(String element) {
this.element = element;
}
public ArrayList<Integer> getItems() {
return items;
}
public void setItems(ArrayList<Integer> items) {
this.items = items;
}
public ArrayList<String> getElements() {
return elements;
}
public void setElements(ArrayList<String> elements) {
this.elements = elements;
}
}
The XHTML is empty, I don't see any item. I would like load all list and with a click on any selectItem, the element
variable change its value.
Thank you!
Instead of using a ui:repeat
and f:selectItem
, use f:selectItems
, which can take a list of items.
Here is an example for your situation:
<h:selectOneRadio value="#{bean.element}">
<f:selectItems value="#{bean.items}" var="i" itemValue="#{bean.elements[i]}" itemLabel="#{bean.elements[i]}" />
</h:selectOneRadio>
I'm not sure I fully understand the contents of the variables you are using, but this may also work better for you:
<h:selectOneRadio value="#{bean.element}">
<f:selectItems value="#{bean.elements}" var="i" itemValue="#{i}" itemLabel="#{i}" />
</h:selectOneRadio>