I'm using Primefaces 4.0, Spring, Spring Security and Hibernate. I do not know why after making a post in an ajax="false" commandbutton the ViewScoped bean is returning to the form all the old values and not being destroyed. In other ViewScoped Beans it is working fine but this not. I have tried to solve this for quite a while and I can't make it work.
the form is quite large, it will simplify to make it clear
<h:form id="anadirForm">
<p:panel id="panel" header="Añadir factura">
<h:panelGrid id="grid" columns="2" style="margin-bottom:10px">
<f:facet name="header">
<p:messages id="messages" autoUpdate="true" />
</f:facet>
<h:outputLabel for="justificante" value="Justificante: *" />
<p:inputText id="justificante"
value="#{facturaBean.factura.justificante}" required="false"
>
<p:ajax event="change" listener="#{facturaBean.comprobarJustificante}"/>
</p:inputText>
....
</h:panelGrid>
</p:panel>
<p:dataTable style="width: 780px;" id="lineasFacturas" var="facturaLinea" value="#{facturaBean.factura.lineas}">
<p:column>
<p:inputText value="#{facturaLinea.contrapartida}" />
</p:column>
<p:column style="width: 90px;">
<p:inputText style="width: 85px;" value="#{facturaLinea.base}" >
<p:ajax event="change" process="lineasFacturas" update="lineasFacturas" listener="#{facturaBean.sumarBases}"/>
</p:inputText>
</p:column>
.....
</p:dataTable>
<p:commandButton value="Guardar" process="@all" ajax="false" action="#{facturaBean.saveFactura}" >
</h:form>
Bean
@ManagedBean
@ViewScoped
public class FacturaBean implements Serializable{
private List<FacturaLinea> facturaLineas;
private Factura factura;
private FacturaLinea facturaLin;
priv
....
@PostConstruct
public void init(){
facturaLineas = new ArrayList<FacturaLinea>();
factura = new Factura();
factura.n
....
}
....
public void saveFactura(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
DateFormat hourFormat = new SimpleDateFormat("HH:mm:ss");
Date hour = new Date();
String fechaFactura = null;
String fechaPunteo = sdf.format(hour);
String horaPunteo = hourFormat.format(hour);
try {
Date dat = dateFormat.parse(factura.getFechaFactura());
fechaFactura = sdf.format(dat);
}catch (ParseException e){
}
factura.setFechaFactura(fechaFactura);
factura.setFechaPunteo(fechaPunteo);
factura.setHoraPunteo(horaPunteo);
factura.setAgente(FacesContext.getCurrentInstance().getExternalContext().getRemoteUser());
calcularImporte();
dao.save(factura);
}
....
public void sumarBases(){
Integer suma = 0;
for(FacturaLinea fl :factura.getLineas()){
suma += fl.getBase();
}
factura.setSumaBases(suma);
calcularImporte();
}
public void comprobarJustificante(){
for(Factura f : lista){
if(factura.getJustificante() == f.getJustificante()){
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Justificante en uso"));
break;
}
}
}
I have tried to return a String in saveFactura method because i rode it on BalusC blog http://balusc.blogspot.com.es/2011/09/communication-in-jsf-20.html where says:
"@ViewScoped: a bean in this scope lives as long as you're interacting with the same JSF view in the browser window/tab. It get created upon a HTTP request and get destroyed once you postback to a different view... You need to return null or void from action (listener) methods to keep the bean alive."
But the bean not being destroyed in any way, i do not know what to do now.
Sorry for grammar or spelling mistakes.