使用相同的支持bean将数据从page1传递到page2的最佳方法是什么? [英] What is the best way to pass data from page1 to page2 using the same backing bean?

查看:68
本文介绍了使用相同的支持bean将数据从page1传递到page2的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在第1页和第2页上创建一个简单的搜索表单,我将显示结果.

I'm creating a simple search form on page1 and on page2 i will be showing the results.

我想知道使用@ViewScoped支持bean的最佳方法是什么.以前,我不得不使用@SessionScope来实现这一点.

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.

第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>

第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后备豆:

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

推荐答案

我决定使用f:viewParamf:event type="preRenderView".这样,我就可以通过查询字符串参数获得可收藏的页面,并且可以在preRenderView侦听器的结果页面上生成结果.

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.

结果页面:

<!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后备豆: @ViewScoped 公共类DocumentBacking {

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

}

这篇关于使用相同的支持bean将数据从page1传递到page2的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆