Reputation: 371
I'm creating a project with Spring 3.2.0, PrimeFaces 3.3.1, Mojarra 2.1.11 and then Spring Security 3.1.1.
My problem is the whenever an ajax call is fired by PrimeFaces, like for example in table pagination, the screen components are not refreshed.
EDIT #4
Whenever an ajax called is fired, an status 302 is returned from the server and the page is refreshed
I think the problem is related to Spring security, this are my settings:
<security:http entry-point-ref="authenticationEntryPoint" pattern="/spring/**">
<security:anonymous />
<security:http-basic />
<security:custom-filter position="FORM_LOGIN_FILTER" ref="authenticationProcessingFilter" />
<security:logout logout-url="/spring/logout" logout-success-url="/spring/login?logout=true" />
<security:intercept-url pattern="/spring/login" access="ROLE_ANONYMOUS,ROLE_USER,ROLE_ADMIN" />
<security:intercept-url pattern="/spring/logoutSuccess" access="ROLE_ANONYMOUS,ROLE_USER,ROLE_CLIENT" />
<security:intercept-url pattern="/spring/intro" access="ROLE_ANONYMOUS,ROLE_USER" />
<security:intercept-url pattern="/spring/flows/admin_main_flow/**" access="ROLE_ADMIN" />
<security:intercept-url pattern="/spring/flows/client_main_flow/**" access="ROLE_CLIENT" />
</security:http>
<security:http security="none" pattern="/javax.faces.resource/**" />
<security:http security="none" pattern="/ajax/**" />
<security:http security="none" pattern="/static/**" />
<security:http security="none" pattern="/resources/**" />
This happen just after the login so I don't think the session should be already expired.
EDIT #4 end
I stripped down the application to a minimum and tried this code:
<h:form id="testAjax" >
<h:panelGrid columns="4" cellpadding="5">
<h:outputLabel for="test" value="test:" style="font-weight:bold"/>
<p:inputText id="test" value="#{testBean.test}" />
<p:commandButton value="Submit" update="@form"/>
<h:outputText value="#{testBean.test}" id="display" />
</h:panelGrid>
</h:form>
note that I double checked that no forms are nested, I tried both "process" and "update" with @form or the elements id.
What happens is that the setter of TestBean is called, the attribute setted, but then the getter is never called again to display the new value. This are the logs on server side:
org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Spring MVC Dispatcher Servlet' processing POST request for [/TryAjax/spring/WEB-INF/login.xhtml]
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/WEB-INF/login.xhtml] to HandlerExecutionChain with handler [org.springframework.web.servlet.mvc.UrlFilenameViewController@2f46ccac] and 1 interceptor
org.springframework.web.servlet.mvc.UrlFilenameViewController - Returning view name 'WEB-INF/login' for lookup path [/WEB-INF/login.xhtml]
org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.faces.mvc.JsfView: name 'WEB-INF/login'; URL [/WEB-INF/WEB-INF/login.xhtml]] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
org.springframework.faces.support.RequestLoggingPhaseListener - Entering JSF Phase: RESTORE_VIEW 1
org.springframework.faces.mvc.JsfView - Asking faces lifecycle to render
org.springframework.faces.support.RequestLoggingPhaseListener - Entering JSF Phase: RENDER_RESPONSE 6
org.springframework.faces.mvc.JsfView - View rendering complete
org.springframework.web.servlet.DispatcherServlet - Successfully completed request
I fear is a problem of configuration, I followed the basic instruction but then made some modifications to correct some errors, here's the web.xml stripped of some not relevant part
<?xml version = '1.0'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_context.xml</param-value>
</context-param>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener </listener-class>
</listener>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/tags/synaptic.taglib.xml</param-value>
</context-param>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
Faces config
<?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>
other spring flow configuration
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces-2.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<faces:resources />
<context:annotation-config />
<context:component-scan base-package="com.synaptic"/>
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/flows/*=flowController
</value>
</property>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<bean id="faceletsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.faces.mvc.JsfView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".xhtml"/>
</bean>
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="flowFacesContextLifecycleListener" />
<!-- <webflow:listener ref="facesContextListener"/>-->
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<bean id="flowFacesContextLifecycleListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
<webflow:flow-registry id="flowRegistry" flow-builder-services="facesFlowBuilderServices">
<webflow:flow-location-pattern value="/WEB-INF/flows/**/*.xml"/>
</webflow:flow-registry>
<bean id="conversionService" class="org.springframework.faces.model.converter.FacesConversionService"/>
<bean id="expressionParser" class="org.springframework.webflow.expression.el.WebFlowELExpressionParser">
<constructor-arg>
<bean class="org.jboss.el.ExpressionFactoryImpl"/>
</constructor-arg>
<property name="conversionService" ref="conversionService"/>
</bean>
<faces:flow-builder-services id="facesFlowBuilderServices" expression-parser="expressionParser" conversion-service="conversionService"/>
</beans>
Please if you see anything wrong or you have any idea help me, this issue is bugging me from days. Thank you!
EDIT #1
I add the code for the backing bean and the webflow code where I declare it:
public class TestBean implements Serializable {
private String test = "xxx";
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
<view-state id="home" view="../views/adminHome.xhtml">
<on-entry>
<evaluate expression="textManager.getTest()" result="viewScope.test"/>
</on-entry>
</view-state>
Now this of testBean is just the simplest test case I can make that doesn't work, the main problem I have is that no ajax call works, for example pagination for tables, adding or removing or modifying an element of a datatable, tabView etc.
I generally create the beans for the page and put them in the viewScope or flowScope, this also for lists of data coming from the database. I tryed also having the bean managed by Spring with scope Session but it doesn't work neither. Another note is that the default value display on the page correctly.
EDIT #2
using firebug and the eclipse debug i see this:
Initial value of TestBean.test is "xxx", I change to aaa and press submit.
An ajax post request is sent to the server:
javax.faces.ViewState e2s1
javax.faces.partial.ajax true
javax.faces.partial.execu... @all
javax.faces.partial.rende... testAjax testAjax:display
javax.faces.source testAjax:testButton
testAjax testAjax
testAjax:test aaa
testAjax:testButton testAjax:testButton
When the POST is sent I firt debug a call to TestBean.getTest() and then a call to TestBean.setTest("aaa") that has the correct value.
At this point I would expect another call to TestBean.getTest() when the response is built but the call does not happens and the response does not contain the correct value but the old value "xxx".
If now I refresh the page the getter is called and the new value "aaa" is displayed correctly.
EDIT #3
I still can sort this out. I noticed now with firebug an unexpected behaviour:
when I click the command button 2 requests are sent to the server, a POST and a GET.
The POST is sent first and return an status "302 Moved Temporarily", then the GET start and return a response without the updated data. Anyone knows what status 302 means in this case and what could be the cause?
Debugging in the same way the primefaces demo only a POST request is fired that return no status 302
Thank you
Upvotes: 1
Views: 3543
Reputation: 1
<webflow:flow-execution-attributes>
<webflow:redirect-in-same-state value="false"/>
</webflow:flow-execution-attributes>
Solved my problem. I was using org.springframework.web.servlet.handler.SimpleUrlHandlerMapping for navigation with JSF 1.x RichFaces 3.x. When moving to JSF 2.x and PrimeFaces/RichFaces 4 the ajax aspects of the DataTables did not work (e.g. displayed but would not sort) Adding this xml worked like magic (as it would need to since I don't know what I am doing)
Upvotes: 0
Reputation: 371
You need to add a parameter to the flow-executor to disable the redirect to the same state. The configuration becomes:
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="flowExecutionListener" />
<webflow:listener ref="facesContextListener"/>
<!-- <webflow:listener ref="flowFacesContextLifecycleListener" />-->
</webflow:flow-execution-listeners>
<webflow:flow-execution-attributes>
<webflow:redirect-in-same-state value="false"/>
</webflow:flow-execution-attributes>
</webflow:flow-executor>
This prevents the flow executor to refresh the page completely after the ajax request is sent.
Upvotes: 1
Reputation: 10463
When you integrate JSF with Spring then your JSF managed beans become Spring beans. This is why Spring includes their own implementation of an ELResolver so that you can reference these Spring beans from within EL expressions.
Unfortunately though the only scopes that Spring supports out of box are @Request, @Session and @Application.
Fortunately a simple implementation of ViewScope that integrates with Spring can be integrated into your application. Cagatay Civici, the lead developer at PrimeFaces wrote a blog article showing an example of a simple ViewScope implementation that can be easily integrated into your application.
http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/
With a managed bean that is view scoped then your beans will have enough persistence to dynamically render page elements on ajax updates.
Upvotes: 0