keep getting javax.faces.application.ViewExpiredException: viewId with jsf 2

user2846482 picture user2846482 · Oct 15, 2013 · Viewed 24.4k times · Source

Whatever I do I get javax.faces.application.ViewExpiredException: viewId. I know how to handle the exception (redirect to the main page again if I get the error), the issue is that any action I call from a commandButton is not executed.

Basically I have a simple view with a commandButton with an action.. I press it and I get the viewExpiredException or if I handle the error, I don't get the error messages but still my commandButton does not work.

I tried adding this:

 <context-param>
     <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
     <param-value>client</param-value>
 </context-param>

but it doesn't work. I don't know what is going on..

This is my view:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    lang="es">
<div class="container">
    <h:form id="principal">
        <h1>Sistema de carga de publicaciones</h1>

        <h:outputText value="Ingrese el año por el que desea buscar publicaciones" />
        <h:inputText
            id="year"
            value="#{publicaciones.año}" />

        <h:commandButton
            style="display:block"
            id="boton-codigo-facultad"
            value="Buscar"
            type="submit">
        </h:commandButton>

        <h:panelGroup id="output">
            <h:outputText
                style="display:block"
                value="Total publicaciones en la DB:#{publicaciones.totalPublicacionesDB}" />
            <h:outputText
                style="display:block"
                value="Publicaciones obtenidas desde el WS:" />
            <h:outputText
                style="display:block"
                value="Publicaciones nuevas:" />
        </h:panelGroup>
        <h:commandButton
            style="display:block"
            id="insertar"
            value="Cargar publicacion"
            action="#{insert.insertPublicacionDB}">
        </h:commandButton>
    </h:form>
</div>
</html>

backing bean:

@ManagedBean(name="insert")
@ViewScoped
public class comparaDatosBean implements Serializable {

    private static final long serialVersionUID = 1L;
    Map<String, Object> parametros=new HashMap<String, Object>();
    Map<String, Object> params2=new HashMap<String, Object>();

    @PostConstruct
    public void init(){
        System.out.println("pase por aqui!!");
    }

    public void insertPublicacionDB() throws SQLException {
        ApplicationContext applicationContext = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        ControllerPublicacion controlador= (ControllerPublicacion) applicationContext.getBean("controllerPublicaciones");
        parametros.put("ut","234567876545555");
        parametros.put("py", 2013);

        params2.put("ut", "234567876545555");
        params2.put("bp","asjñldfkjsñd");
        params2.put("la", "Español");
        params2.put("ti","Publicacion de prueba");
        params2.put("py",2013);
        params2.put("sn", "234wf");
        params2.put("di", "asdfsf");
        params2.put("j9", "");
        params2.put("dt", "");
        params2.put("ga", "");
        params2.put("dt", "");
        params2.put("so", "");
        params2.put("fx", "");
        params2.put("ep", "");
        params2.put("issue", "");
        params2.put("parte","");
        params2.put("vl", "");
        params2.put("rp", "");
        params2.put("vinculada", null);
        params2.put("ji", "");
        params2.put("pd","");
        params2.put("pg", "");
        params2.put("pa","");
        params2.put("ab","");
        params2.put("pi", "");
        params2.put("numero_articulo", null);
        params2.put("tipo_carga_revista",null);
        params2.put("id",null );
        params2.put("su", "");
        System.out.println(params2.get("la"));
        if(controlador.existeEnDB(parametros)>0){
            System.out.println("La publicacion ya existe en la Base de Datos");
        } else {
            controlador.insertPublicacion(params2);
            System.out.println("la publicacion ha sido añadida");
        }

    }

    public Map<String, Object> getParametros() {
        return parametros;
    }

    public void setParametros(Map<String, Object> parametros) {
        this.parametros = parametros;
    }

    public Map<String, Object> getParams2() {
        return params2;
    }

    public void setParams2(Map<String, Object> params2) {
        this.params2 = params2;
    }

}

Error message:

GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/test] threw exception [viewId:/main.jsf - No se pudo restablecer la vista /main.jsf.] with root cause
javax.faces.application.ViewExpiredException: viewId:/main.jsf - No se pudo restablecer la vista /main.jsf.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:212)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

What I need is to know why this is happening, not how to handle the exception.. I can't call any action from a commandButton because nothing happens.

Answer

user2846482 picture user2846482 · Oct 28, 2013

I solved it with this:

 <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>client</param-value>
 </context-param>

In some places I saw it like this javax.faces.PARTIAL_STATE_SAVING but it did not work.