Spring Injection + JSF + Hibernate - spring

I'm actually starting with JSF, so go easy.
I'm trying to mix Spring 3.x + Hibernate 4 + JSF 2.2
I used to mix Spring + Hibernate in Desktop applications, and that wasn't a problem at all.
But now, I'm a little confused here.
I'm trying to map through annotations and I'm pretty sure that is a mess.
First of all, I'd like to ask you the best practice to Integrate all those technologies and keep the MVC pattern
That being said, here are my files:
web.xml
<?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>WebCD</display-name>
<!-- Spring Config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Spring/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
AppMainController.java
#Controller
public class AppMainController {
#Autowired
private CDDao cdDao;
public List<CD> findAllCDs() throws SQLException {
List<CD> cds = cdDao.findAll();
return cds;
}
}
CDDaoImpl.java
#Repository
public class CDDaoImpl extends Dao<CD> implements CDDao {
#Override
public Class<CD> getEntityClass() {
return CD.class;
}
}
And, finally, the *.xhtml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Lista de CDs</title>
</h:head>
<h:body>
<center>
<h1>Lista de CDS</h1>
</center>
<h:messages />
<h:dataTable value="#{/*Right here I really don't know what to call*/.findAllCDs()}" var="cd"
rules="rows" cellpadding="5">
<f:facet name="caption">Essa é a lista de todos os CD's cadastrados no sistema</f:facet>
<f:facet name="header">Lista de CD's</f:facet>
<f:facet name="footer">Fim da Lista de CD's</f:facet>
<h:column>
<f:facet name="header">Código</f:facet>
#{cd.id}
</h:column>
<h:column>
<f:facet name="header">Descrição</f:facet>
#{cd.descricao}
</h:column>
<h:column>
<f:facet name="header">Gravadora</f:facet>
#{cd.fkLabel}
</h:column>
<h:column>
<f:facet name="header">Preço</f:facet>
#{cd.preco}
</h:column>
<h:column>
<f:facet name="header">Data Compra</f:facet>
#{cd.dataCompra}
</h:column>
<h:column>
<f:facet name="header">Data Gravação</f:facet>
#{cd.dataGravacao}
</h:column>
</h:dataTable>
</h:body>
</html>
I've tried these approaches:
Add #ManagedBean(name="ctrl") to the AppMainController and call ctrl.findAllCDs().
Create a property private List<CD> cds and a method calling cdDao.findAll() to fill the list and retrieve to the dataTable as myBean.cds.
These were the following exceptions:
NullPointerException: cdDao was null at some points, which leads me to consider the only logical inference: cdDao is not being injected (Autowired)
#ManagedBean was not in the right place..
I'd be grateful if you could show me a guideline, a path, because I'm really confused.

After changing #ManagedBean for #Named("name") and #Autowired for #Inject, the problem was solved.

Related

JSF xhtml page not found

I've been trying to follow various articles on setting up my first JSF application and can't get my first xhtml page to be found. I keep getting the error "This application has no explicit mapping for /error, so you are seeing this as a fallback" in the browser.
In the browser, I type http://localhost:8080/modelSimulation.xhtml to get this error, and in the console, I see:
... :GET "/modelSimulation.xhtml", parameters={}
... :No mapping for GET /modelSimulation.xhtml
My spring boot application has added the various things required to setup JSF with spring boot, but clearly I'm missing something to be able to view my modelSimulation.xhtml page. What am I missing? Thanks!
I also tried http://localhost:8080 but get corresponding no mapping error too.
src/main/webapp/WEB-INF/modelSimulation.xhtml:
<?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://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<ui:composition template="layout.xhtml">
<ui:define name="content">
<h:form id="mainForm">
<p:panel header="Details">
<h:panelGrid columns="1">
<p:outputLabel for="symbol" value="Symbol: " />
<p:inputText id="symbol" value="#{tradingModelSimulationController.symbol}" />
<p:outputLabel for="ranking" value="ranking: " />
<p:inputNumber id="ranking" value="#{tradingModelSimulationController.ranking}" />
<h:commandButton value="apply" action="#{tradingModelSimulationController.apply}" />
</h:panelGrid>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
</html>
src/main/webapp/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
</web-app>
I added the 2 required beans to the SpringBootApplication class:
#Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
return new ServletRegistrationBean(servlet, "*.jsf");
}
#Bean
public FilterRegistrationBean rewriteFilter() {
FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
rwFilter.setDispatcherTypes(EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST,
DispatcherType.ASYNC, DispatcherType.ERROR));
rwFilter.addUrlPatterns("/*");
return rwFilter;
}
Here's the backing bean (I don't understand the #Join...)
#Scope(value = "session")
#Component(value = "tradingModelSimulationController")
#ELBeanName(value = "tradingModelSimulationController")
#Join(path = "/modelSimulation", to = "/modelSimulation.jsf")
public class TradingModelSimulationController {
ModelSimulation modelSimulation = new ModelSimulation();
String symbol;
int ranking;
public void apply() {
System.out.println("Applied: " + modelSimulation.toString());
RequestContext.getCurrentInstance()
.execute("handleMsg('applied!');");
}
public ModelSimulation getModelSimulation() {
return modelSimulation;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public int getRanking() {
return ranking;
}
public void setRanking(int ranking) {
this.ranking = ranking;
}
}

Xhtml page mapping from ManagedBean

Here Is Managed Bean Class
package Controller;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import Model.Employee;
#ManagedBean(name="employeeController")
#SessionScoped
public class Employeecontroller implements Serializable{
public String show(){
System.out.println("hello");
return "Login";
}
}
Here Is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Secondproject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>/faces/*</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>homepage.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Here id home.xhtml page
<!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:rich="http://richfaces.org/rich"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:h="http://java.sun.com/jsf/html">
<h:head></h:head>
<body>
<rich:panel>
<f:facet name="header">
Write your own custom rich components with built-in AJAX support
</f:facet>
<h:outputText value="Panel Body" />
<h:form>
<h:commandButton action="#{employeeController.show()}" value="Login" />
</h:form>
</rich:panel>
</body>
</html>
Here is the code for mapping xhtml page from ManagedBean.
I just want when the button is clicked then the login page will show....But when the button is clicked there is only home.xhtml page reloading...Login page is not loading....
The selection of the next view depends on the value of the action attribute of the UICommand components. If the navigation handler finds a navigation rule for this page with this outcome, the next view will be the element content. If there is no any rule like this it looks for a page with the value (or it appended with ".xhtml" file extension).
The variations:
1., Direct view definition:
<h:commandButton action="next_page_name"/>
It must be a full qualified name (page names + file name), but the xhtml could be omitted.
2., Indirect view definition:
In this case the action value is contains a bean method invocation:
<h:commandButton action="#{myBean.methodName}"/>
This method should be a function without any parameter and passes back a String value. This result will be the income for the navigation handler. It could return a page name or a navigation rule.
Now let us see your problem. If the name of your current view is id_home.xhml and you want to navigate to the login.xhtml page (both are in the x package):
1., Direct view definition. Return value "x/login" or "x/login.xhtml" from the show() method:
public String show()
{
//...
return "x/login";
}
2., Indirect view definition. Define a page navigation rule in the faces-config.xml like this:
<navigation-rule>
<from-view-id>/x/id_home.xhtml</from-view-id>
<navigation-case>
<from-outcome>go_to_login</from-outcome>
<to-view-id>x/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
In this case myBean.methodName passes back the content of the form-outcome element:
public String show()
{
//...
return "go_to_login";
}

JSF 2 Managed bean concurrent access between logged in users

Basic example to showcase my problem :
Two users A and B connect to the JSF web app from different browsers, and the welcoming page shows their documents and a search panel.
Both of them want to search for documents using a word or sentence.
(for instance) A wants to search for the word "sheets" and B wants to search for the word "virus".
They write down the word in the form input and they simultaneously submit the form.
A gets the result of her search, while B suddenly gets also the same result as A with a word he didn't type which is "sheets".
Here's my managedBean :
#ManagedBean
#RequestScoped
public class DocumentController implements Serializable {
private String motSearch= null;
private boolean searching = false;
private boolean rechercheStructure = true;
private DocumentDao dao; //Dependency injection with Spring
public void onload(Utilisateur user)
{
if(!searching)
{
motSearch = null;
documentsListForConnectedUser(user);
}
else{
search(user);
}
}
public void search(Utilisateur user)
{
documentsListForConnectedUser(user);// getting documents list from database
searching = true;
List<Document> list = new ArrayList<>();
list.addAll(user.getDocuments());
user.setDocuments(new ArrayList<>());
//...
if(motSearch != null && !motSearch.isEmpty())
{
LOGGER.info("motSearch : "+motSearch.toLowerCase());
List<Document> docs = user.getDocuments();
for(Document doc : list)
{
if(!docs.contains(doc) && (doc.getNom().toLowerCase().contains(motSearch.toLowerCase()) ||
doc.getDescription().toLowerCase().contains(motSearch.toLowerCase()) ||
doc.getTheme().toLowerCase().contains(motSearch.toLowerCase()) ||
doc.getKeywords().contains(motSearch.toLowerCase())))
{
docs.add(doc);
}
}
docs = filter(docs);
if(docs.size() == 0)
{
user.setDocuments(null);
}
}
else
{
user.setDocuments(list);
}
}
}
The JSF page ListeDocuments.xhtml :
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:metadata>
<f:viewParam name="searching" value="#{documentController.searching}" />
<f:viewParam name="structure" value="#{documentController.rechercheStructure}"/>
<f:viewParam name="q" value="#{documentController.motSearch}"/>
<f:viewAction action="#{documentController.onload(user)}"/>
</f:metadata>
</h:head>
<h:body>
<h:form id="myForm" prependId="false" >
<h:inputHidden value="true" id="searching"/>
<div class="center">
<h:selectOneRadio converter="javax.faces.Boolean"
value="#{documentController.rechercheStructure}" onchange="check()" id="structure">
<f:selectItem itemLabel="Recherche structurée" itemValue="true" />
<f:selectItem itemLabel="Recherche plein texte" itemValue="false" />
</h:selectOneRadio>
</div>
<div class="row">
<div class="input-field col s12 m7 l8">
<p:inputTextarea styleClass="materialize-textarea" id="q" value="#{documentController.motSearch}"/>
<label for="q">Mot/phrase à chercher</label>
</div>
</div>
<p:commandButton styleClass="btn blue-grey" update=":table"
type="submit" value="Chercher"
action="ListeDocuments?faces-redirect=true&includeViewParams=true" />
</h:form>
<p:dataTable id="table" tableStyleClass="hoverable striped" rows="10" paginator="true"
value="#{user.documents}" var="doc"
paginatorPosition="bottom" paginatorAlwaysVisible="false" styleClass="borderless">
<!--Columns-->
</p:dataTable>
</h:body>
</html>
My web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/serverError.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/serverError.xhtml</location>
</error-page>
<!-- Add Support for Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>none</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>
faces-config.xml file :
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
PS : I'm using :
Mojarra 2.2.9 JSF implementation.
Apache Tomcat 7 server with Servlet 3.0.
Spring but I only use it to inject interface implementations like DAOs.
Hibernate 4/JPA as ORM.
Since I needed to use Omnifaces 2.4 library for its converters I was obliged to integrate CDI (weld-servlet-2.3.5 final) and create an empty beans.xml (guidelines from BalusC blog), and I don't use any of its annotations in my managedBeans.
The only annotations I use are #ManagedBean and #xxxxScoped from javax.faces.bean and sometimes #PostConstruct.
Finally Primefaces 6.0
Any help is appreciated !

Can't see spring controllers from XHTML file

I am getting "can not resolve variable 'indexController'" error.
My xhtml file;
<?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:p="http://primefaces.org/ui">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<title>Title Goes Here</title>
</f:facet>
</h:head>
<h:body>
<h:form>
<p:panel header="Send">
<p:inputText value="Hi"></p:inputText>
<p:commandButton value="Send" id="btnDisplay" actionListener="#{indexController}"/>
</p:panel>
</h:form>
</h:body>
My controller;
#Controller("indexController")
#Scope("session")
public class IndexController extends MainController {
private String name;
#Override
public void init() {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public void destroy() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void sayHello(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My applicationContext.xhtml file;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="tr.source.controllers"/>
<context:annotation-config/>
</beans>
My web.xml file;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
Your problem here is that JSF resolves beans through CDI or ManagedBeans, not Spring-managed beans.
The solution is to configure an EL name resolver for Spring beans:
Put this in your faces-config.xml
<application>
...
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
See Spring SpringBeanFacesELResolver javadocs for more info.

PrimeFaces 3.0 + Mojarra 2.1.6 javascript ajax package jsf.ajax.* not available - ViewExpiredException

I've tried to investigate the problem but haven't found solution at all. I use primefaces datatable with pagination. I wrote and exception handler for viewexpiredexception error as Ed Burns suggested in his blog but when used in combiation with ajax submits (eg. datatable page navigation) the page redirection on vee is not handled. I used the solution suggested by BalusC here JSF Status bar / connection status information but chrome says it's missing jsf.ajax.* javascript namespace. The page runs on tomcat 7.0.22 with no error/warning at startup and back-end is based on spring and mybatis. Here is the web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Discontinui</display-name>
<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>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<session-config>
<session-timeout>2</session-timeout>
</session-config>
</web-app>
here is the faces-config.xml
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<factory>
<exception-handler-factory>it.dipvvf.vi.app.common.ViewExpiredExceptionExceptionHandlerFactory</exception-handler-factory>
</factory>
</faces-config>
and here is the index.xhtml page:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Prova</title>
<script type="text/javascript">
var statusUpdate = function statusUpdate(data) {
var statusArea = document.getElementById("statusArea");
var text = statusArea.value;
text = text + "Name: "+data.source.id;
if (data.type === "event") {
text = text +" Event: "+data.name+"\n";
} else { // otherwise, it's an error
text = text + " Error: "+data.name+"\n";
}
statusArea.value = text;
};
// Setup the statusUpdate function to hear all events on the page
alert("0");
jsf.ajax.addOnEvent(statusUpdate);
alert("1");
jsf.ajax.addOnError(statusUpdate);
alert("2");
</script>
</h:head>
<h:body>
<h:form>
Selezionare il tipo di accesso:<br />
<p:selectOneMenu id="somAccessMode" value="#{accessMode.mode}">
<f:selectItem itemValue="#{accessMode.ufficioPersonale}"
itemLabel="Ufficio Personale" />
<f:selectItem itemValue="#{accessMode.capoTurno}"
itemLabel="Capo Turno" />
</p:selectOneMenu>
<br />
<p:commandButton id="bEnter" value="Entra"
action="#{accessMode.onEntraClick}" update="growl" />
<br />
<p:dataTable var="nominativo" value="#{accessMode.elencoNominativi}" paginator="true" rows="1"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column>
<f:facet name="header">
Cognome
</f:facet>
<h:outputText value="#{nominativo.cognome}" />
</p:column>
<p:column>
<f:facet name="header">
Nome
</f:facet>
<h:outputText value="#{nominativo.nome}" />
</p:column>
<p:column>
<f:facet name="header">
Data Iscrizione
</f:facet>
<h:outputText value="#{nominativo.iscrizione}">
<f:convertDateTime pattern="dd-MM-yyyy" />
</h:outputText>
</p:column>
</p:dataTable>
<hr />
<br />
<!-- <p:messages showDetail="true" autoUpdate="true"/> -->
<p:growl id="growl" showDetail="true" sticky="true" />
<h3>Status:</h3>
<textarea id="statusArea" cols="40" rows="10" readonly="readonly" />
</h:form>
</h:body>
</html>
I've put more than needed just for tests. The alert("1") and "2" does not show and jsf.ajax.add... raise error in browser. If I look and generated page in chrome I can't see any inclusion of jsf.js bundled into mojarra package. Where is the flaw?
Thanks to anyone and sorry for the lenght!
The problem is that you are using primefaces components (<p:commandButton)
<p:commandButton id="bEnter" value="Entra" action="#{accessMode.onEntraClick}" update="growl"/>
try this:
<h:commandButton id="bEnter" value="Entra" action="#{accessMode.onEntraClick}">
<f:ajax execute="#form" render="growl"/>
</h:commandButton>
primefaces register their own callbacks and any jsf.ajax.* won't be able to be executed from primefaces components.

Resources