0

我有一个 p:selectOneRadio 的动态列表,如下所示:

1 op1   op2   op3
2 op1   op2   op3
3 op1   op2   op3

因此,当我提交时,我想知道为第 1 行选择了哪个选项,为第 2 行选择了哪个选项,为第 3 行选择了哪个选项。我遇到的问题是我只得到最后一个选择的选项。这是我的xhtml:

<ui:repeat var="o" value="#{perfilesMB.numbers}" varStatus="status">
    <p:selectOneRadio value="#{perfilesMB.number}">
        <p:ajax listener="#{perfilesMB.handleCityChange}" />
        <f:selectItem itemLabel="Option 1" itemValue="1" />
        <f:selectItem itemLabel="Option 2" itemValue="2" />
        <f:selectItem itemLabel="Option 3" itemValue="3" />
    </p:selectOneRadio>                                                     
</ui:repeat>

这是我的托管bean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.springframework.dao.DataAccessException;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import org.primefaces.event.RowEditEvent;  

@ManagedBean(name="perfilesMB")
//@RequestScoped 
@SessionScoped
public class PerfilesManagedBean implements Serializable{
    private List<String> numbers;
    private String number;

    public List<String> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<String> numbers) {
        this.numbers = numbers;
    }


    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    //private String nivel;
    //private int id_seccion;

    public PerfilesManagedBean() {
        niveles = new ArrayList<String>();
        numbers = new ArrayList<String>();
        for (int i = 0; i < 3; i++) {
            numbers.add("" + i); 
        }
    }

    public String sendData(){
        System.out.println("Number: " + number);
        return "";
    }       

}

我该如何解决?提前致谢

4

1 回答 1

2

关键是这一行:

<p:selectOneRadio value="#{perfilesMB.number}">

您实际上是将所有 3 个选择项绑定到一个 bean 属性,在该属性下发送的最后一个请求参数实际上是最后一个结束并覆盖 bean 属性的参数。因此,您需要将其绑定到数组或数字列表的相应元素,例如

<ui:repeat ... varStatus="status">
    <p:selectOneRadio value="#{perfilesMB.numberSelection[status.index]}" />
</ui:repeat>

有后盾

List<String> numberSelection;//initialized and equal to the amount of items in your UIRepeat component, i.e. with 3 empty strings added
于 2013-10-24T17:44:53.540 回答