0

我环顾四周寻找完整的指南。我已经使用 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>

但从这里我不知道如何继续,请帮忙?提前致谢

4

2 回答 2

2

BalusC通过构建一个自定义标记组件(基于 JSF 2.0 和 2.1 的纯 JSF )发表了一篇关于文件上传的博客文章。从 JSF 2.2 开始,有一个用于文件上传的 JSF 组件:<h:inputFile>.

此外,您可以查看PrimeFaces FileUploadRichFaces FileUpload等其他实现。

于 2012-07-24T21:02:57.100 回答
1

我可以推荐PrimeFaces FileUploader吗?

于 2012-07-24T20:54:06.767 回答