我环顾四周寻找完整的指南。我已经使用 JSF 2 天了,我正在尝试制作一个页面,将 pdf 文件上传到服务器,将其文件路径与其他用户输入的文本字段一起放入数据库中。
这是我的表格(到目前为止)
<h:form enctype="multipart/form-data">
<h:outputText value="Name: " />
<h:inputText id="name" size="40" value="#{resumeBean.name}" required="true"/> <br/>
<h:outputText value="Position Sought: "/>
<h:inputText id="position" size="40" value="#{resumeBean.position}" required="true"/> <br/>
<h:outputText value="Date: " />
<h:inputText id="date" size="40" value="#{resumeBean.date}" required="true" /> <br/>
<h:outputText value="File to upload: " />
<t:inputFileUpload value="#{bean.resume}" /> <br/>
<h:commandButton value="submit" action="#{bean.submit}" />
<h:messages />
</h:form>
这个bean来处理文件
package com.example;
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
@ManagedBean
@RequestScoped
public class Bean {
private String name, position, date;
private String fileName;
private UploadedFile resume;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public void setResume(UploadedFile resume) {
this.fileName = resume.getName();
this.resume = resume;
}
public UploadedFile getResume() {
return resume;
}
public String getFileName() {
return fileName;
}
public void submit() throws IOException {
String fileName = FilenameUtils.getName(resume.getName());
String contentType = resume.getContentType();
byte[] bytes = resume.getBytes();
// Now you can save bytes in DB (and also content type?)
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
}
}
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="timesheet2" version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</web-app>
但从这里我不知道如何继续,请帮忙?提前致谢