I have been using preRenderView (+ the re-direct and skip postback hacks) in my app and I'm trying to replace it with viewAction that comes with JSF2.2 (MyFaces 2.2.4). However I found it triggers earlier than command actionListener and action that it's rather useless for my purpose. For example:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestBean2 implements Serializable {
int count = 0;
public void actionFunc() {
System.out.println(count);
}
public String addCount() {
count++;
return null;
}
public int getCount() {
return count;
}
public void initView() {
System.out.println("initView");
}
public void setCount(int count) {
this.count = count;
}
}
and then a very simple .xhtml:
<!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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xmlns:pt="http://xmlns.jcp.org/jsf/passthrough" xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:t="http://myfaces.apache.org/tomahawk" xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:event type="preRenderView" listener="#{testBean2.initView}" />
<f:viewAction action="#{testBean2.actionFunc}" onPostback="true" />
</f:metadata>
<ui:composition template="/WEB-INF/template.xhtml">
<ui:define name="title">Test</ui:define>
<ui:define name="content">
#{testBean2.count}
<h:form>
<h:commandButton styleClass="btn btn-primary" action="#{testBean2.addCount}" value="addCount">
</h:commandButton>
</h:form>
</ui:define>
</ui:composition>
</html>
So the addCount() System.out output is always 1 behind what's on the rendered view, because actionFunc() always triggers before addCount().
If I want to do something like, if (count == 3) do a return "newpage" for a redirect, it ends up way too early for such decision. I can do the check in initView() and do ConfigurableNavigationHandler.performNavigation("newpage"), but that's the kind of hack I'm trying to eliminate with JSF 2.2.
Therefore, it seems like viewAction is rather useless if I want it to work with any value that I'm going to change with action or actionListener. Is it supposed to work like this?
Thanks!