I've managed to get a plain ViewScoped page going. I don't know if I don't understand something or this page isn't working as it should.
Every request it creates a new Test
object even though it is ViewScoped and I can never get back the theString
variable that I set.
Plain template with a input field, a button to set the value and a button to refresh the value displayed:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<f:view contentType="text/html">
<h:head>
</h:head>
<h:body>
<h:form>
<p:commandButton process="theInput" value="Set value" />
<p:inputText id="theInput" value="#{test.theString}" /> --
</h:form>
<h:form>
<p:commandButton process="theArea" update="theArea" value="Display value" />
</h:form>
<h:panelGroup id="theArea">
#{test.theString}
</h:panelGroup>
</h:body>
</f:view>
</html>
Bean:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean(name = "test")
@ViewScoped
public class Test implements Serializable {
private String theString = "initial";
public String getTheString() {
return theString;
}
public void setTheString(String theString) {
this.theString = theString;
}
}
I'm using Mojarra 2.2.7, Primefaces 5.2 and Glassfish 4.1
Shouldn't the Test
object hold its state? Is there something I'm missing here?