4

我使用 Primefaces 手册示例作为向导,并用一个简单的文件上传表单替换其中一个选项卡。我拥有所有必要的库并在 web-inf 中进行过滤,以使文件上传正常工作。使用 Netbeans 运行项目不会显示任何错误(显示向导并且 UI 响应),但是文件没有上传,并且我的 fileBean 类没有收到系统消息。任何线索我在哪里犯了错误?我在 index.html 中的嵌套表单有问题吗?谢谢!

索引.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>The rings of scholarship</title>
    </h:head>
    <h:body>
        <h:form>

            <p:growl id="growl" sticky="true" showDetail="true"/>

            <p:wizard widgetVar="wiz"
                      flowListener="#{userWizard.onFlowProcess}">

                <p:tab id="personal" title="Personal">

                    <p:panel header="Personal Details">

                        <h:messages errorClass="error"/>

                        <h:panelGrid columns="2" columnClasses="label, value" styleClass="grid">
                            <h:outputText value="Firstname: *" />
                            <p:inputText required="true" label="Firstname"
                                         value="#{userWizard.user.firstname}" />

                            <h:outputText value="Lastname: *" />
                            <p:inputText required="true" label="Lastname"
                                         value="#{userWizard.user.lastname}" />

                            <h:outputText value="Age: " />
                            <p:inputText value="#{userWizard.user.age}" />

                            <h:outputText value="Skip to last: " />
                            <h:selectBooleanCheckbox value="#{userWizard.skip}" />
                        </h:panelGrid>
                    </p:panel>
                </p:tab>

                <p:tab id="upload" title="File upload">
                    <p:panel header="File upload">

                        <h:messages errorClass="error"/>

                        <h:panelGrid columns="2" columnClasses="label, value">
                            <h:form enctype="multipart/form-data">
                                <p:fileUpload value="#{fileBean.file}" mode="simple" />
                                <p:commandButton value="Submit" action="#{fileBean.save}" ajax="false"/>
                            </h:form>
                        </h:panelGrid>
                        <h:outputText value="Skip to last: " />
                        <h:selectBooleanCheckbox value="#{userWizard.skip}" />
                    </p:panel>
                </p:tab>

                <p:tab id="contact" title="Contact">
                    <p:panel header="Contact Information">

                        <h:messages errorClass="error"/>

                        <h:panelGrid columns="2" columnClasses="label, value">
                            <h:outputText value="Email: *" />
                            <p:inputText required="true" label="Email"
                                         value="#{userWizard.user.email}" />

                            <h:outputText value="Phone: " />
                            <p:inputText value="#{userWizard.user.phone}"/>

                            <h:outputText value="Additional Info: " />
                            <p:inputText value="#{userWizard.user.info}"/>
                        </h:panelGrid>
                    </p:panel>
                </p:tab>

                <p:tab id="confirm" title="Confirmation">
                    <p:panel header="Confirmation">

                        <h:panelGrid id="confirmation" columns="6">
                            <h:outputText value="Firstname: " />
                            <h:outputText styleClass="outputLabel"
                                          value="#{userWizard.user.firstname}" />

                            <h:outputText value="Lastname: " />
                            <h:outputText  styleClass="outputLabel"
                                           value="#{userWizard.user.lastname}"/>

                            <h:outputText value="Age: " />
                            <h:outputText styleClass="outputLabel"
                                          value="#{userWizard.user.age}" />>

                            <h:outputText value="Email: " />
                            <h:outputText styleClass="outputLabel"
                                          value="#{userWizard.user.email}" />

                            <h:outputText value="Phone " />
                            <h:outputText styleClass="outputLabel"
                                          value="#{userWizard.user.phone}"/>

                            <h:outputText value="Info: " />
                            <h:outputText styleClass="outputLabel"
                                          value="#{userWizard.user.info}" />

                            <h:outputText />
                            <h:outputText />
                        </h:panelGrid>

                        <p:commandButton value="Submit" update="growl" 
                                         actionListener="#{userWizard.save}"/>

                    </p:panel>
                </p:tab>

            </p:wizard>

        </h:form>

    </h:body>
</html>

文件豆:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class FileBean implements Serializable{

    private UploadedFile file;
/**
 * Creates a new instance of FileBean
 */
public FileBean() {
}

public UploadedFile getFile() {
    System.out.println("we get file");
    return file;
}

public void setFile(UploadedFile file) throws FileNotFoundException, IOException {
    System.out.println("we set file");
    this.file = file;

}

public void save() throws IOException {
    System.out.println("we save file");
    IOUtils.copy(file.getInputstream(), new FileOutputStream("D:\\" + file.getFileName()));
    BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputstream()));
    String currLine = br.readLine();
    Integer counterLines = 0;
    while (currLine != null && counterLines < 5) {
        System.out.println("currLine is: " + currLine);
        counterLines++;
    }

}

}

用户向导:

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.FlowEvent;

@ManagedBean
@ViewScoped
public class UserWizard implements Serializable{

    private User user = new User();
    private boolean skip;
    private static final 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

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

    public boolean isSkip() {
        return skip;
    }

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

    public String onFlowProcess(FlowEvent event) {
        logger.log(Level.INFO, "Current wizard step:{0}", event.getOldStep());
        logger.log(Level.INFO, "Next step:{0}", event.getNewStep());

        if (skip) {
            skip = false;   //reset in case user goes back
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }
}
4

1 回答 1

2

从摆脱嵌套形式开始......你有一个围绕向导,一个在里面<p:tab id="upload" title="File upload">

尝试从选项卡中删除内部表单并使用<h:form enctype="multipart/form-data">

于 2012-08-07T09:19:35.270 回答