3

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";
    }
}
Catfish
  • 18,876
  • 54
  • 209
  • 353
  • 1
    You could use a wider scope defined by yourself using [Flash Scope](http://mkblog.exadel.com/2010/07/learning-jsf2-using-flash-scope/). Also, make sure you're using the right Mojarra version for this, as stated in [Object in Flash scope is not available after redirect](http://stackoverflow.com/a/9155297/1065197) question. – Luiggi Mendoza Jul 26 '12 at 20:46
  • Interesting option. I'm just curious what everyone else does in this situation. Search page with results has to be a pretty common enough use case and i'm curious how the majority is handling it or what is the best way to handle it. – Catfish Jul 26 '12 at 20:51
  • You could also pass those values as request parameters or via session attributes, depending on your needs, but if I want a lighter solution, I would use request parameter. – Luiggi Mendoza Jul 26 '12 at 20:53
  • 1
    I use the same page for search and results, with a `@ViewScoped` bean. I think most people does the same thing. – Elias Dorneles Jul 26 '12 at 21:38
  • @eljunior - That is what I do as well. Put a datatable on the page and when the user clicks Search, I update it by putting update="myDataTable" in the p:commandbutton. Catfish - If you want to keep 2 separate pages, you could explore the conversation scope for your bean. I have never tried using it but it sounds like what you need for your search flow. – SteveS Jul 27 '12 at 12:48

2 Answers2

0

Use Custom Scope is more reliable for your code. Here is the sample of JSF 2.0 Custom Scope.

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
0

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&amp;includeViewParams=true";
}

}

Catfish
  • 18,876
  • 54
  • 209
  • 353