I am trying to inject JSF ViewScoped bean as ManagedProperty into a RequestScoped bean which implements javax.faces.validator.Validator. But always a fresh copy of ViewScoped bean is injected.
ViewScoped Bean
@ViewScoped
@ManagedBean
public class Bean {
private Integer count = 1;
private String field2;
public String action(){
++count;
return null;
}
public String anotherAction(){
return null;
}
//getter and setter
}
Validator
@RequestScoped
@ManagedBean
public class SomeValidator implements Validator {
public void validate(FacesContext context, UIComponent comp, Object value)
throws ValidatorException {
//logging bean.getCount() is always one here. Even after calling ajax action a few times
}
@ManagedProperty(value = "#{bean}")
private Bean bean;
}
xhtml page
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
<h:panelGroup layout="block" id="panel1">
<h:commandButton type="submit" value="Action" action="#{bean.action}">
<f:ajax render="panel1"></f:ajax>
</h:commandButton>
<h:outputText value="#{bean.count}"></h:outputText>
</h:panelGroup>
<h:panelGroup layout="block" id="panel2">
<h:inputText type="text" value="#{bean.field1}">
<f:validator binding="#{someValidator}" />
</h:inputText>
</h:panelGroup>
<h:commandButton type="submit" value="Another Action" action="#{bean.anotherAction}">
<f:ajax execute="panel2" render="panel2"></f:ajax>
</h:commandButton>
</h:form>
</h:body>
</html>
As mentioned in code, even after calling ajax action a few times, when logging bean.getCount() always display one.
But the same scenario works if I change ViewScoped to SessionScoped. Also if I remove Validator implementation of RequestScoped bean and use a logger in PostConstruct, the count does increment for every ajax request as expected.
Am I doing something wrong? or is this how it should work? Thanks in advance