1
votes


I am trying to resolve an apparently simple issue but so far no success. I am developing a web application in JSF 2.2 with PrimeFaces 5.3 and tomcat 8 . I am trying to deal with ViewExpiredException, so that I can redirect the user to index or login page whenever session timeout occurs. I tried adding error-page tag in web.xml to catch the exception and redirect on login page, but no success.

I followed this link (along with several others) to seek guidance but as I mentioned earlier, it did not work for me.

Any help will be highly appreciated.

Below is my application's web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ricpacs</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <context-param>  
        <param-name>primefaces.THEME</param-name>  
        <param-value>#{themeSwitcherView.selectedTheme}</param-value>  
    </context-param> 
    <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>      

   <session-config>
      <!-- Session idle timeout in min. -->
      <session-timeout>1</session-timeout>
    </session-config>        

    <error-page>
        <exception-type>
            javax.faces.application.ViewExpiredException
        </exception-type>
        <location>/index.jsp</location>
    </error-page>


    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>
    <mime-mapping> 
        <extension>ttf</extension> 
        <mime-type>application/x-font-ttf</mime-type> 
    </mime-mapping>     
    <mime-mapping> 
        <extension>woff</extension> 
        <mime-type>application/x-font-woff</mime-type> 
    </mime-mapping>
    <mime-mapping> 
        <extension>svg</extension> 
        <mime-type>image/svg+xml</mime-type> 
    </mime-mapping>
    <context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

I have tried both values (Development and Production) for PROJECT_STAGE. But that did not help either.

2
Hi Fahim, any reason to don't use the last version of primefaces? Look at primefaces.org/downloads to find the last one.Duloren
@Doleron, no special reason :-) maybe we will use the latest version in our next project. ThanksFahim Ashraf

2 Answers

2
votes

Its always better to study books and detailed guides before implementing something new:-). After reading the PrimeFaces 5.3 user guide, I found out that in order to utilize built-in exceptionHandler feature of PrimeFaces, one must first configure it in faces config.xml file. The configuration is as given below:

<faces-config>
.
.
.
<application>
<el-resolver>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
</el-resolver>
</application>
<factory>
<exception-handler-factory>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
</exception-handler-factory>
</factory>
.
.
.
</faces-config>

After that, configure web.xml for ViewExpiredException as under:

<web-app>
.
.
.

<error-page>
        <exception-type>
            javax.faces.application.ViewExpiredException
        </exception-type>
        <location>/index.jsp</location> <!-- type whatever suits your environment and requirements -->
</error-page>
.
.
.
</web-app>

And thats it. Your ViewExpiredException will now be caught by built-in ExceptionHandler.

1
votes

Try to use this for capture ViewExpiredException on ajax requests:

<p:ajaxExceptionHandler type="javax.faces.application.ViewExpiredException"

You can add this code to footer section in your template, so you do not have to place it on every page.