@ViewScoped 托管 bean 在回发期间加载多次

发布于 2024-09-09 11:27:12 字数 4926 浏览 6 评论 0原文

我在 jsf Facelet 上有一个日历、编辑器、文件上传和 dataTable primefaces 控件。

代码如下,

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../templates/masterlayout.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:f="http://java.sun.com/jsf/core">

    <ui:define name="title">#{lbl.SSTitle}</ui:define>

    <ui:define name="content">
        <h:form>
            <p:panel header="Upload Script">
                <h:outputText value="Welcome #{loginActionBean.login.emp.empName}"/>
                <br />
                <p:calendar value="#{searchScriptActionBean.scheduleDate}" />
                <br />
                <p:fileUpload fileUploadListener="#{searchScriptActionBean.handleFileUpload}"
                              multiple="true" update="filsList" allowTypes="*.txt;*.init" description="Script Files">
                </p:fileUpload>
                <br />
                <p:editor value="#{searchScriptActionBean.htmlText}" />
            </p:panel>
            <p:dataTable id="filsList" value="#{searchScriptActionBean.scriptFiles}" var="file">

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="File Name" />
                    </f:facet>
                    <h:outputText value="#{file.fileName}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Size" />
                    </f:facet>
                    <h:outputText value="#{file.size}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Operation" />
                    </f:facet>
                    <h:commandLink value="Remove">
                        <p:collector value="#{file}" removeFrom="#{searchScriptActionBean.scriptFiles}" />
                    </h:commandLink>
                </p:column>

            </p:dataTable>
        </h:form>
    </ui:define>
</ui:composition>

@ViewScoped Bean如下,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ugam.crawler.web.script;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author devendra.mahajan
 */
@Named(value = "searchScriptActionBean")
@ViewScoped
public class SearchScriptActionBean implements Serializable{

    protected String htmlText;
    private Date scheduleDate;
    private List<UploadedFile> scriptFiles = new ArrayList<UploadedFile>();;
    /**
     * Get the value of scheduleDate
     *
     * @return the value of scheduleDate
     */
    public Date getScheduleDate() {
        return scheduleDate;
    }

    /**
     * Set the value of scheduleDate
     *
     * @param scheduleDate new value of scheduleDate
     */
    public void setScheduleDate(Date scheduleDate) {
        this.scheduleDate = scheduleDate;
    }

    /**
     * @return the scriptFiles
     */
    public List<UploadedFile> getScriptFiles() {
        return scriptFiles;
    }

    /**
     * @param scriptFiles the scriptFiles to set
     */
    public void setScriptFiles(List<UploadedFile> scriptFiles) {
        this.scriptFiles = scriptFiles;
    }

    /** Creates a new instance of SearchScriptActionBean */
    public SearchScriptActionBean() {
        System.out.println("In SearchScriptActionBean Constructor");

    }

    public void handleFileUpload(FileUploadEvent event) {
        //add facesmessage to display with growl
        //application code
        UploadedFile file = event.getFile();
        scriptFiles.add(file);


    }


    /**
     * Get the value of htmlText
     *
     * @return the value of htmlText
     */
    public String getHtmlText() {
        return htmlText;
    }

    /**
     * Set the value of htmlText
     *
     * @param htmlText new value of htmlText
     */
    public void setHtmlText(String htmlText) {
        this.htmlText = htmlText;
    }
}

我的问题是SearchScriptActionBean在表单加载和上传文件时加载多次。我想保留 bean 的旧值。前任。 scriptFiles(List),添加上传的文件。并且 filsList (dataTable) 没有得到更新。

I have a calendar, editor, fileUpload and a dataTable primefaces controls on a jsf facelet.

Code is as follows,

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../templates/masterlayout.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:f="http://java.sun.com/jsf/core">

    <ui:define name="title">#{lbl.SSTitle}</ui:define>

    <ui:define name="content">
        <h:form>
            <p:panel header="Upload Script">
                <h:outputText value="Welcome #{loginActionBean.login.emp.empName}"/>
                <br />
                <p:calendar value="#{searchScriptActionBean.scheduleDate}" />
                <br />
                <p:fileUpload fileUploadListener="#{searchScriptActionBean.handleFileUpload}"
                              multiple="true" update="filsList" allowTypes="*.txt;*.init" description="Script Files">
                </p:fileUpload>
                <br />
                <p:editor value="#{searchScriptActionBean.htmlText}" />
            </p:panel>
            <p:dataTable id="filsList" value="#{searchScriptActionBean.scriptFiles}" var="file">

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="File Name" />
                    </f:facet>
                    <h:outputText value="#{file.fileName}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Size" />
                    </f:facet>
                    <h:outputText value="#{file.size}" />
                </p:column>

                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Operation" />
                    </f:facet>
                    <h:commandLink value="Remove">
                        <p:collector value="#{file}" removeFrom="#{searchScriptActionBean.scriptFiles}" />
                    </h:commandLink>
                </p:column>

            </p:dataTable>
        </h:form>
    </ui:define>
</ui:composition>

and @ViewScoped Bean as follows,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ugam.crawler.web.script;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author devendra.mahajan
 */
@Named(value = "searchScriptActionBean")
@ViewScoped
public class SearchScriptActionBean implements Serializable{

    protected String htmlText;
    private Date scheduleDate;
    private List<UploadedFile> scriptFiles = new ArrayList<UploadedFile>();;
    /**
     * Get the value of scheduleDate
     *
     * @return the value of scheduleDate
     */
    public Date getScheduleDate() {
        return scheduleDate;
    }

    /**
     * Set the value of scheduleDate
     *
     * @param scheduleDate new value of scheduleDate
     */
    public void setScheduleDate(Date scheduleDate) {
        this.scheduleDate = scheduleDate;
    }

    /**
     * @return the scriptFiles
     */
    public List<UploadedFile> getScriptFiles() {
        return scriptFiles;
    }

    /**
     * @param scriptFiles the scriptFiles to set
     */
    public void setScriptFiles(List<UploadedFile> scriptFiles) {
        this.scriptFiles = scriptFiles;
    }

    /** Creates a new instance of SearchScriptActionBean */
    public SearchScriptActionBean() {
        System.out.println("In SearchScriptActionBean Constructor");

    }

    public void handleFileUpload(FileUploadEvent event) {
        //add facesmessage to display with growl
        //application code
        UploadedFile file = event.getFile();
        scriptFiles.add(file);


    }


    /**
     * Get the value of htmlText
     *
     * @return the value of htmlText
     */
    public String getHtmlText() {
        return htmlText;
    }

    /**
     * Set the value of htmlText
     *
     * @param htmlText new value of htmlText
     */
    public void setHtmlText(String htmlText) {
        this.htmlText = htmlText;
    }
}

My Problem is SearchScriptActionBean loads many time when the form loads and when a file is uploaded. I want to retain old values of bean. ex. scriptFiles(List), where uploaded files are added. and filsList (dataTable) is not getting updated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆躺在深渊里 2024-09-16 11:27:12

看起来很像问题 1492。这是相关性的引用:

这是一个先有鸡还是先有蛋的问题,部分状态保存。该视图被执行到
在应用增量状态之前填充视图,以便我们看到您的行为
描述的。

目前,我没有找到解决此用例的明确方法。

如果必须使用视图范围的绑定,解决方法是设置
javax.faces.PARTIAL_STATE_SAVING 为 false。

Primefaces 可能会隐式地将上传的文件与视图绑定,您需要将以下内容添加到 web.xml 中:

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

尝试一下,看看是否有帮助。如果有效,您可能需要考虑仅针对特定视图关闭。全局关闭部分状态保存将显着增加内存和/或带宽使用,具体取决于状态保存方法。假设视图ID为/upload.xhtml,则使用:

<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/upload.xhtml</param-value>
</context-param>

您可以通过分号指定多个视图ID。

Look much like issue 1492. Here's a cite of relevance:

This is a chicken/egg issue with partial state saving. The view is executed to
populate the view before delta state is applied, so we see the behavior you've
described.

At this point, I don't see a clear way to resolve this use case.

The workaround, if you must use view-scoped bindings would be setting
javax.faces.PARTIAL_STATE_SAVING to false.

Probably Primefaces is implicitly binding the uploaded file with the view and you need to add the following to the web.xml:

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

Give it a try and see if that helps. If it works, you may want to consider to turn if off for a specific view only. Globally turning off partial state saving will namely noticeably increase memory and/or bandwidth usage, depending on state saving method. Assuming that the view ID is /upload.xhtml, use this:

<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/upload.xhtml</param-value>
</context-param>

You can specify multiple view IDs by a semicolon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文