问题
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";
}
}
回答1:
Use Custom Scope is more reliable for your code. Here is the sample of JSF 2.0 Custom Scope.
回答2:
I decided to use f:viewParam
's and f: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:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:metadata>
<f:viewParam name="mainNumber" value="#{documentBacking.document.mainNumber}" />
<f:viewParam name="secNumber" value="#{documentBacking.document.secNumber}" />
<f:viewParam name="formType" value="#{documentBacking.document.formType}" />
<f:viewParam name="subFormType" value="#{documentBacking.document.subFormType}" />
<f:event type="preRenderView" listener="#{documentBacking.generateResults}" />
</f:metadata>
...
<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: @ViewScoped public class DocumentBacking {
private Document document = new Document();
private List<Document> results = null;
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 void generateResults() {
results = new ArrayList<Document>();
// dummy data
Document doc = new Document();
doc.setMainNumber("9343");
doc.setSecNumber("71254");
doc.setFormType("OTHER FORMS");
doc.setSubFormType("CALGB");
results.add(doc);
doc = new Document();
doc.setMainNumber("1234");
doc.setSecNumber("4321");
doc.setFormType("SOMETHING");
doc.setSubFormType("MAYO");
results.add(doc);
}
public String search() {
return "results.xhtml?faces-redirect=true&includeViewParams=true";
}
}
来源:https://stackoverflow.com/questions/11676224/what-is-the-best-way-to-pass-data-from-page1-to-page2-using-the-same-backing-bea