I’m creating a simple search form on page1 and on page2 i will be showing the results.
I’m wondering what the best way to do that with a @ViewScoped backing bean. Previously i’ve had to use @SessionScope to achieve this.
Page1 – search page:
<h:form id="documents">
<h4 class="dkblue u-case">Documents Search</h4>
<h:outputLabel for="mainNum" value="mainNumber" />
<p:inputText id="mainNum" value="#{documentBacking.document.mainNumber}"/>
<h:outputLabel for="secNum" value="secNumber" />
<p:inputText id="secNum" value="#{documentBacking.document.secNumber}"/>
<h:outputLabel for="formType" value="Form Type" />
<p:inputText id="formType" value="#{documentBacking.document.formType}"/>
<p:commandButton value="Search" action="#{documentBacking.search}" />
<p:commandButton id="clear" value="Clear" type="reset"/>
</h:form>
Page2 – results page:
<p:dataTable value="#{documentBacking.results}" var="results">
<p:column headerText="Main Number">
<h:outputText value="#{results.mainNumber}" />
</p:column>
<p:column headerText="Secondary Number">
<h:outputText value="#{results.secNumber}" />
</p:column>
<p:column headerText="Form Type">
<h:outputText value="#{results.formType}" />
</p:column>
</p:dataTable>
@ViewScoped Backing Bean:
@ManagedBean
@ViewScoped
public class DocumentBacking {
private Document document = new Document();
private List<Document> results = new ArrayList<Document>();
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public List<Document> getResults() {
return results;
}
public void setResults(List<Document> results) {
this.results = results;
}
public String search() {
results = new ArrayList<Document>();
// dummy data
Document doc = new Document();
doc.setMainNumber("1111");
doc.setSecNumber("2222");
doc.setFormType("OTHER");
doc.setSubFormType("TEST");
results.add(doc);
doc = new Document();
doc.setMainNumber("1234");
doc.setSecNumber("4321");
doc.setFormType("SOMETHING");
doc.setSubFormType("TESTER");
results.add(doc);
return "results.xhtml?faces-redirect=true";
}
}
I decided to use
f:viewParam‘s andf:event type="preRenderView". This way i have bookmarkable pages via the query string params, and i’m generating the results on the results page in the preRenderView listener.I’m using the same search page as in my question.
Results page:
@ViewScoped Backing Bean:
@ViewScoped
public class DocumentBacking {
}