简体   繁体   English

使用相同的辅助bean将数据从page1传递到page2的最佳方法是什么?

[英]What is the best way to pass data from page1 to page2 using the same backing bean?

I'm creating a simple search form on page1 and on page2 i will be showing the results. 我正在第1页和第2页创建一个简单的搜索表单,我将显示结果。

I'm wondering what the best way to do that with a @ViewScoped backing bean. 我想知道使用@ViewScoped支持bean的最佳方法是什么。 Previously i've had to use @SessionScope to achieve this. 以前我必须使用@SessionScope来实现这一目标。

Page1 - search page: 第1页 - 搜索页面:

<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: 第2页 - 结果页面:

<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 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";
    }
}

Use Custom Scope is more reliable for your code. 使用自定义范围对您的代码更可靠。 Here is the sample of JSF 2.0 Custom Scope. 以下是JSF 2.0自定义范围的示例。

I decided to use f:viewParam 's and f:event type="preRenderView" . 我决定使用f:viewParamf: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. 这样我通过查询字符串params可以收藏书签,我在preRenderView监听器的结果页面上生成结果。

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 { @ViewScoped Backing Bean:@ViewScoped公共类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";
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM