0
好吧,标题不是很好,但我无法找到一个更好的...JSF列表<Class>是空
在java中
我有一个类型类的一些性质和另一种类型的列表...
在HTML中我有一个表,其中每行是上述列表中的一个元素,我可以成功获取每行的属性,但是当我提交表单时List是空的/ 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;
}
RIGHE
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>
打开网页,我把所有的行正确DISP奠定了每个领域的正确价值..在validateSecond
方法提交后,我得到一个空List<Righe>
我错过了什么?
谢谢
尝试加入'@ ViewScoped'或'@ SessionScoped'(取决于哟你需要的)到你的托管bean,因为没有这个注释默认将是'@ RequestScoped' – Daniel
谢谢......'@ SessionScoped'工作..我是JSF的新手......如果你发布一个答案,我会将其标记为接受:) – Marcx
欢迎您使用请求范围和datatable查看另一个问题http://stackoverflow.com/a/12620512/617373 – Daniel