【问题标题】:JSF List<Class> on submit the list is null提交列表时的 JSF List<Class> 为空
【发布时间】:2012-09-27 12:20:22
【问题描述】:

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

在java中

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

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

这是我的代码:

TESTATA

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;
}

righe.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&lt;Righe&gt; 我错过了什么?

谢谢

【问题讨论】:

  • 尝试将@ViewScoped 或`@SessionScoped`(取决于您的需要)添加到您的托管bean 中,因为没有该注释,默认值为@RequestScoped
  • 谢谢...@SessionScoped 工作...我是 JSF 的新手...如果您发布答案,我会将其标记为已接受 :)
  • 不客气,例如看看另一个关于请求范围和数据表的问题stackoverflow.com/a/12620512/617373
  • 会话范围不是正确的选择。使用视图范围。另见stackoverflow.com/questions/7031885/…

标签: jakarta-ee jsf-2 richfaces form-submit


【解决方案1】:

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

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

看看这个The benefits and pitfalls of @ViewScoped - BalusC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多