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:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
#{myBean.state}
<h:form>
<h:commandButton value="test" action="#{myBean.action}"/>
</h:form>
</h:body>
</html>
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.
Seam Faces 3 provides a @ViewScoped annotation for CDI beans, along with a ton of other features to bridge the gap between CDI and JSF.
Based on advice posted here, I have started using MyFaces CODI to solve this problem. I can't tell you if Seam or CODI is better, but at least it lets me move past trying to puzzle out scopes and get on with coding the application.