1

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.

Uyak
  • 27
  • 8
  • What do you want to happen? I think what you experience is normal. If you want to show the same page with reset fields you can just call init() last in saveFactura(). Or maybe you can make the bean RequestScoped. If you want to navigate to another page use an action instead of actionListener on the button, and return a navigation-string from the method. As far as I know it has no meaning to return a string when it is called from an actionListener, in fact it is not permitted. – Jaqen H'ghar Jun 20 '14 at 13:22
  • You are right in the message is written actionListener but I put it doing tests and forgot to change, I know It has to be an action, but as action it does not work. Sorry for that – Uyak Jun 20 '14 at 13:44
  • 1
    You are right, you cannot make it RequestScoped as Ajax will not work. So I believe your options are manually resetting the bean, or call saveFactura() via the action (not actionListener) and return a redirect to the same page as mentioned here: http://stackoverflow.com/questions/16483214/how-can-i-reload-the-same-page-from-a-managedbean-in-jsf – Jaqen H'ghar Jun 20 '14 at 14:21
  • 1
    Does it redirect to a new view on return of string (URL changes)? if so then could it be that FacturaBean is reinitialized. You can test it by adding print statements in init() method – Avinash Singh Jun 20 '14 at 14:23
  • If you are not using ajax request why don't you try setting the scope of your managed bean to `@RequestScoped`? – LarsBauer Jun 20 '14 at 14:58
  • Ok, I'll opt to redirect and refresh the page from backing bean. Here is the code as mentioned in the link `ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());` Thank you both :) – Uyak Jun 20 '14 at 14:58
  • @QueryLars I'm using ajax request on this view, anyway thank you. It's solved – Uyak Jun 20 '14 at 15:04

0 Answers0