Im sure why, but my viewscoped-bean doesnt get persisted when redisplaying the same page. Im wondering if this is because of the use of facelet templates ?
Here\'s w
The problem seems to be that you are declaring your bean to be a CDI managed bean, not a JSF managed bean. @ViewScoped is a JSF specific scope that is not natively supported by CDI.
CDI does allow you to create custom scopes, so you can build support for it. In fact, this has already been done. See this: http://seamframework.org/Community/JSF2ViewScopeInCDI
Without using any extensions, the following code works perfectly well:
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class MyBean {
String state = "";
@PostConstruct
public void test() {
System.out.println("pc called");
state = "state set";
}
public String getState() {
return state;
}
public String action() {
return "";
}
}
And the following Facelet:
#{myBean.state}
The post construct method will only be called once now, and after you press the command button the page will refreshed but the state will be retained.