制作#{list.choose}
一个由当前迭代类别标识的数组、集合或映射。其中Map<String, String>
键代表类别,值代表所选选项的 A 可能是最简单的。
这是一个在这里工作的MCVE。
package com.stackoverflow.q2493671;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.enterprise.context.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;
@Named
@RequestScoped
public class Bean {
private List<String> categories;
private List<String> selectItems;
private Map<String, String> selectedItemsByCategory = new HashMap<>();
@PostConstruct
public void init() {
categories = Arrays.asList("cat1", "cat2", "cat3");
selectItems = Arrays.asList("item1", "item2", "item3");
}
public void submit() {
for (Entry<String, String> entry : selectedItemsByCategory.entrySet()) {
String category = entry.getKey();
String selectedItem = entry.getValue();
System.out.println(category + "=" + selectedItem);
}
}
public List<String> getCategories() {
return categories;
}
public List<String> getSelectItems() {
return selectItems;
}
public Map<String, String> getSelectedItemsByCategory() {
return selectedItemsByCategory;
}
}
结合
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<title>SO question 2493671</title>
</h:head>
<h:body>
<h:form>
<ui:repeat value="#{bean.categories}" var="category">
<h:selectOneRadio value="#{bean.selectedItemsByCategory[category]}" layout="pageDirection">
<f:selectItems value="#{bean.selectItems}" />
</h:selectOneRadio>
</ui:repeat>
<h:commandButton value="submit" action="#{bean.submit}" />
</h:form>
</h:body>
</html>