为什么我的PrimeFaces上传工具不起作用?

时间:2011-03-28 18:33:22

标签: java jsf file-upload java-ee primefaces

我试图使用primefaces上传工具(http://www.primefaces.org/showcase/ui/fileUploadSingle.jsf)。我设法正确显示它,看起来它的工作原理。但是当我去检查数据库时它是空的。 为什么不上传?我确信访问数据库的EJB编程良好。

当我转到从数据库中检索的页面时,我也会在控制台中注意到此消息。

  

严重:第1:61行没有可行的替代角色';'

我不确定这是否与它有关。我没有看到其他错误。

我上传工具的代码如下所示:

<p:fileUpload fileUploadListener="#{uploadController.handleFileUpload}"   
        allowTypes="*.txt;*.doc;*.pdf;" description="Text Documents"/>

这是托管bean:

@ManagedBean
@RequestScoped
public class UploadController {

@EJB
private IFileUploaderEJB fileUploaderEJB;
    private Logger logger = LoggerFactory.getLogger(UploadController.class);

public void handleFileUpload(FileUploadEvent event) {
    String fileName = event.getFile().getFileName();
    String contentType = event.getFile().getContentType();
    byte[] bytes = event.getFile().getContents();

    Garbage garbage = new Garbage();
    garbage.setFilename(fileName);
    garbage.setFile(bytes);
    garbage.setDescription("info about the file");
    garbage.setFileType(contentType);
    fileUploaderEJB.uploadGarbage(garbage);

    logger.info("Uploaded: {}", event.getFile().getFileName());

    FacesMessage msg = new FacesMessage("Succesful", event.getFile()
            .getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

这是EJB到DB的访问(我确定这部分工作正常):

@Stateless(name = "ejbs/FileUploaderEJB")
public class FileUploaderEJB implements IFileUploaderEJB {

@PersistenceContext
private EntityManager em;


public Garbage uploadGarbage(Garbage garbage) {
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");        

    garbage.setUploadDate(dateFormat.format(date));     


    em.persist(garbage);

    return garbage; 
}

}

为什么没有将数据添加到数据库中?

------------------------------------------- UPDATE --- -------------------------------------

private UploadedFile uploadedFile;

public void handleFileUpload(FileUploadEvent event) {
    System.out.println("INSIDE THE METHOD!!!!");
    uploadedFile = event.getFile();
    String fileName = FilenameUtils.getName(uploadedFile.getFileName());
    String contentType = uploadedFile.getContentType();
    byte[] bytes = uploadedFile.getContents();

    // Now you can save bytes in DB (and also content type?)
    Garbage garbage = new Garbage();
    garbage.setFilename(fileName);
    garbage.setFile(bytes);
    garbage.setDescription("info about the file");
    garbage.setFileType("File extension");
    fileUploaderEJB.uploadGarbage(garbage);

    FacesContext.getCurrentInstance().addMessage(
            null,
            new FacesMessage(String.format(
                    "File '%s' of type '%s' successfully uploaded!",
                    fileName, contentType)));
}

这是我的web.xml:

<web-app version="3.0" 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">

<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>/pages/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>        
<servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping> 
    </web-app> 

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,我解决了在WEB-INF / lib中添加jar:

  • 公地-IO-2.2.jar
  • 公地文件上传-1.2.2.jar

并在web.xml中添加过滤器:

<filter> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
</filter-mapping>

我希望它适合你!

答案 1 :(得分:1)

我只需要将它添加到我的web.xml中,源代码是正确的。

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/uploaded</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

感谢您的时间