Search code examples
jsfjsf-2viewexpiredexception

Why the fields in my login page disappear when I redirect after a ViewExpiredException?


I add the following lines to web.xml

<session-config>
    <session-timeout>1</session-timeout>
</session-config>

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/login/login.xhtml</location>
</error-page>

to redirect to login page when the session expires and it is in fact redirecting but the fields name and password and the button "fazer login" disappear: enter image description here enter image description here

Heres the relevant code in the login page:

<div class="conteudo">
    <div class="icone-produto"></div>
    <div class="coluna-direita">
        <div class="logo-produto" alt="Wplex-EP"></div>
        <div class="card signin-card">

            <h:panelGrid columns="1">

                <h:dataTable value="#{funcionarioController.lista}"
                    rendered="false"></h:dataTable>
                <h:dataTable value="#{escalaController.lista}" rendered="false"></h:dataTable>
                <h:dataTable value="#{indisponibilidadeController.lista}"
                    rendered="false"></h:dataTable>
                <h:dataTable value="#{programacoesController.programacoes}"
                    rendered="false"></h:dataTable>
                <h:dataTable value="#{funcionarioController.lista}"
                    rendered="false"></h:dataTable>

                <h:inputText class="texto" placeholder="Usuário" 
                    value="#{loginController.login}" />
                <h:inputSecret class="texto" placeholder="Senha" 
                    value="#{loginController.senha}"/>

                <h:commandLink id="fazerLoginId" action="#{loginController.isLoginOk}"
                    styleClass="btn btn-wplex mouseon">
                    Fazer Login
                </h:commandLink>

            </h:panelGrid>
        </div>
    </div>
</div>

Solution

  • The disappearing elements are actually all JSF components. This part,

    <location>/login/login.xhtml</location>
    

    must match the <url-pattern> of the FacesServlet entry in the very same web.xml in order to get it to parse the XHTML and render the JSF components as HTML. Apparently you don't have any one on *.xhtml. Perhaps it's *.jsf or /faces/*. You'd need to alter the <location> accordingly to match the <url-pattern> of the FacesServlet, so that it gets invoked. E.g. in case of *.jsf:

    <location>/login/login.jsf</location>
    

    An alternative is to just use an <url-pattern> of *.xhtml, so that you never need to fiddle and get confused with virtual URLs.

    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    

    See also: