0

好吧,标题不是很好,但我找不到更好的......

在java中

我有一个带有一些属性的类型化类和另一个类型的列表......

在 HTML 中,我有一个表格,其中每一行都是上面列表的一个元素,我可以成功获取每一行的属性,但是当我提交表单时,列表为空/null

这是我的代码:

测试

package rip;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean
public class Testata {
    private String codice = "asd"; // set up for test
    private String descrizione = "descrizione"; // set up for test
    private Date data_inizio = new Date(); // set up for test
    private Date data_fine = new Date(); // set up for teset

    private List<Righe> righe;


    public Testata()
    {  // set up righe for test
            righe = new ArrayList<Righe>();
            righe.add(new Righe("001", "descrizione riga 1", "G", true));
            righe.add(new Righe("002", "descrizione riga 2", "G", false));
            righe.add(new Righe("003", "descrizione riga 3", "P", false));
    }

       // base setter/getter
     public List<Righe> getRighe() {
        return (righe);
    }
    public void setRighe(List<Righe> righe) {
        System.out.println("Set Righe"); // used for debug, in my tests 
                                             // it was never printed
        this.righe = righe;
    }

    public String validateSecond()
    {
        if (righe != null)
        {
            for (int i = 0 ; i < righe.size() ; i++)
            {
                System.out.println(righe.get(i).getCodice());
            }
        }
        else
        {
            System.out.println("Righe is null"); // usually submitting the 
                                        // form I got this message in console
        }
        return null;
    }

public class Righe {

    private String codice;
    private String descrizione;
    private String tipo;
    private Boolean YESNO;      

    private static String [] codiceLists = {"001","002","003","004","005","006","007","008"};
    private static String [] descrizioneLists = {"desc 001","desc 002","desc 003","desc 004","desc 005","desc 06","desc 007","desc 008"};
    private static String [] tipoLists = {"P", "G"};

// getter / setter

    // getter for static lists

public String [] getCodiceLists() {
    return codiceLists;
}

public String [] getDescrizioneLists() {
    return descrizioneLists;
}

righthe.xhtml

<h:form id="form_righe">                        
    <h:dataTable var="righe"  styleClass="table_dettaglio"
        rowClasses="table_dettaglio_row"            
            value="#{testata.righe}"            
            columnClasses="column_generic,column_generic,tipo_column,column_generic,column_generic"             
            >               
                <h:column>
                    <f:facet name="header">Codice</f:facet>                                         
                    <rich:select id="codice" enableManualInput="false" value="#{righe.codice}">
                            <f:selectItems value="#{righe.codiceLists}" />
                            <a4j:ajax event="selectitem" render="@form" execute="@this"/>
                </rich:select>                      
                </h:column>         
                <h:column>
                    <f:facet name="header">Descrizione</f:facet>                        
                    <rich:select enableManualInput="true"   id="descrizione"                        
                            defaultLabel="#{righe.descrizione}">
                            <f:selectItems value="#{righe.descrizioneLists}" />
                </rich:select>
                </h:column>     
                <h:column>
                <h:outputStylesheet>
                    .mySelectStyle input{
                    width: 100px;
                }</h:outputStylesheet>
                    <f:facet name="header">Tipo</f:facet>                       
                    <rich:select enableManualInput="false"                                  
                        styleClass="mySelectStyle"                          
                    defaultLabel="#{righe.tipo}">
                    <f:selectItems value="#{righe.tipoLists}" />
                    </rich:select>
                </h:column>         
                <h:column>
                    <f:facet name="header">Valore</f:facet>
                    <h:inputText value="#{rige.valore}" style="width: 150px;"/>                     
                </h:column>

                <h:column>
                    <f:facet name="header">YesNo?</f:facet>
                    <h:selectOneRadio id="flag_medico" value="#{righe.flag_medico}" layout="pageDirection"                                  
                                styleClass="radio_check"/> 

                </h:column>
            </h:dataTable>                          
      <h:commandButton class="button btn btn-warining btn-large" action="#{testata.validateSecond}"
                                       value="Salva" />
       <h:commandButton type="reset" class="button btn btn-warining btn-large" value="Pulisci"/>
  </div>
</h:form>

打开页面,我正确显示了所有行,并且正确评估了每个字段。在validateSecond方法中提交后,我得到一个空值List<Righe> 我错过了什么?

谢谢

4

1 回答 1

4

尝试将@ViewScopedor @SessionScoped(取决于您的需要)添加到您的托管 bean 中,因为没有该注释,默认值将是@NoneScoped

使用数据表时,至少使用它更常见@ViewScoped

看看这个@ViewScoped 的好处和陷阱 - BalusC

于 2012-09-27T12:46:19.427 回答