gpt4 book ai didi

jsf - 在 primefaces 中的向导中上传文件 - 上传的文件没有显示?

转载 作者:行者123 更新时间:2023-12-04 19:33:20 28 4
gpt4 key购买 nike

我使用 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>

文件 bean :
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();
}
}
}

最佳答案

从摆脱嵌套表单开始……您在向导周围获得了一个表单,在 <p:tab id="upload" title="File upload"> 中获得了一个表单。

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

关于jsf - 在 primefaces 中的向导中上传文件 - 上传的文件没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11842622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com