1

我正在尝试<p:cellEditor>使用<p:fileUpload>. 当我尝试上传文件时,它以null. 这是如何引起的,我该如何解决?

我正在使用 PrimeFaces 3.5 和 JSF 2.0。

这是视图:

<p:dataTable id="question" var="question" value="#{editor.questions}" editable="true" widgetVar="carsTable" resizableColumns="true">
    <f:facet name="header">Economic</f:facet>
    <p:ajax event="rowEdit" listener="#{editor.onEdit}"  />
    <p:ajax event="rowToggle" listener="#{editor.onRowToggle}" />

    <p:column style="width:6%">
        <p:rowEditor />
    </p:column>

    <p:column style="width:2%">
        <p:rowToggler />
    </p:column>

    <p:column headerText="Performance Indicator" style="width:25%">
        <h:outputText value="#{question.performanceIndicator}" title="#{question.performanceIndicator}"/>
    </p:column>

    <p:column headerText="Description" style="heigh:200px;width:200px">
        <h:outputText value="#{question.description}" />
    </p:column>

    <p:column headerText="CSR Label" style="width:25%">
        <h:outputText value="#{question.csrLabel}" />
    </p:column>

    <p:column headerText="Response" style="width:25%">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{question.response}" />
            </f:facet>
            <f:facet name="input">
                <h:selectOneMenu value="#{question.response}" style="width:100%">
                    <f:selectItem value="#{tableBean.manufacturers}"  itemLabel="Yes" itemValue="Y" />
                    <f:selectItem value="#{tableBean.manufacturers}"  itemLabel="No" itemValue="N" />
                    <f:selectItem value="#{tableBean.manufacturers}"  itemLabel="Partial" itemValue="P" />
                </h:selectOneMenu>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column headerText="File" style="width:25%">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{question.file.fileName}" />
            </f:facet>
            <f:facet name="input">
                <p:fileUpload value="#{question.file}" mode="simple"/>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:rowExpansion>
        <h:panelGrid id="display" columns="2">
            <h:outputText id="model" value="#{question.description}" />
        </h:panelGrid>
    </p:rowExpansion>
</p:dataTable>

更新:是的,它在一个<h:form>.

这是支持bean:

package org.zicon.web.jsf;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.CellEditEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.zicon.domain.GRI;
import org.zicon.domain.User;




@ManagedBean(name="userWizard")
@RequestScoped
public class UserWizard {


    private List<GRI> questions;
        private User user = new User();

        private boolean skip;

        private String effect = "fade";

        private static Logger logger = Logger.getLogger(UserWizard.class.getName());

        public User getUser() {
                return user;
        }

        public void setUser(User user) {
                this.user = user;
        }

        public void save(ActionEvent actionEvent) {
                //Persist user

            for(GRI gri:questions)
            {
                System.out.println("Response"+gri.getResponse());
                System.out.println("File Name"+gri.getFile().getFileName());

            }

                FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getFirstname());
                FacesContext.getCurrentInstance().addMessage(null, msg);
        }

        public String getEffect() {
                return effect;
        }

        public void setEffect(String effect) {
                this.effect = effect;
        }

        public boolean isSkip() {
                return skip;
        }

        public void setSkip(boolean skip) {
                this.skip = skip;
        }

        public String onFlowProcess(FlowEvent event) {
                logger.info("Current wizard step:" + event.getOldStep());
                logger.info("Next step:" + event.getNewStep());

                if(skip)
                        return "confirm";
                else
                        return event.getNewStep();
        }

        public UserWizard() {
            questions=new ArrayList<GRI>();
                GRI gri=new GRI();
                gri.setPerformanceIndicator("EC1");
                gri.setDescription("Direct economic value generated and distributed, including " +
                        "revenues, operating costs, employee compensation, donations and other community investments, " +
                        "retained earnings, and payments to capital providers and governments.");
                gri.setCsrLabel("3.1");

                questions.add(gri);

                GRI gri1=new GRI();
                gri1.setPerformanceIndicator("EC2");
                gri1.setDescription("Financial implications and other risks and opportunities for the organization's activities due to climate change" );


                questions.add(gri1);

                GRI gri2=new GRI();
                gri2.setPerformanceIndicator("EC1");
                gri2.setDescription("Direct economic value generated and distributed, including " +
                        "revenues, operating costs, employee compensation, donations and other community investments, " +
                        "retained earnings, and payments to capital providers and governments.");
                gri2.setCsrLabel("3.1");

                questions.add(gri2);

                GRI gri3=new GRI();
                gri3.setPerformanceIndicator("EC3");
                gri3.setDescription("Coverage of the organization's defined benefit plan obligations. ");


                questions.add(gri3);

                GRI gri4=new GRI();
                gri4.setPerformanceIndicator("EC4");
                gri4.setDescription("Significant financial assistance received from government. ");


                questions.add(gri4);

                GRI gri5=new GRI();
                gri5.setPerformanceIndicator("EC5");
                gri5.setDescription("Range of ratios of standard entry level wage compared to local minimum wage at significant" +
                        " locations of operation.");
                gri5.setCsrLabel("1.2");

                questions.add(gri5);



        }

          public void onCellEdit(CellEditEvent event) {
                Object oldValue = event.getOldValue();
                Object newValue = event.getNewValue();

                if(newValue != null && !newValue.equals(oldValue)) {
                    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                }
            }

        public List<GRI> getQuestions() {
            return questions;
        }

        public void setQuestions(List<GRI> questions) {
            this.questions = questions;
        }

        public void onRowToggle(ToggleEvent event) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                                                "Row State " + event.getVisibility(),
                                                "Model:" );

            FacesContext.getCurrentInstance().addMessage(null, msg);
        }

         public void onEdit(RowEditEvent event) {
                FacesMessage msg = new FacesMessage("Question Edited", ((GRI) event.getObject()).getResponse());

                GRI gri=((GRI) event.getObject());

                FacesContext.getCurrentInstance().addMessage(null, msg);
            }

         private UploadedFile file;

            public UploadedFile getFile() {
                return file;
            }

            public void setFile(UploadedFile file) {
                this.file = file;
            }

            public void upload() {
                if(file != null) {
                    FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                }
            }
}
4

2 回答 2

1

p:fileUpload中使用属性fileUploadListener="#{Bean.handleFileUpload}"

和豆:

public void handleFileUpload(FileUploadEvent event) throws FileNotFoundException, IOException {
        upFile = event.getFile();
    }
于 2013-04-09T14:04:53.630 回答
0

您是否有一个包含 JSF 代码片段的 <h:form> 标记?否则,我在 Primefaces dataTable 中看不到任何明显的问题。

也许您应该提供您的 Bean (Java) 代码。

于 2013-03-30T18:04:00.810 回答