Primefaces FileUpload在Spring Boot中不起作用 [英] Primefaces FileUpload not working in Spring Boot

查看:131
本文介绍了Primefaces FileUpload在Spring Boot中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行我的JSF项目,并使用Spring Boot启动它,并利用整个Spring环境.配置为:Mojarra 2.2.8 + Primefaces 5.1 + Spring Boot 1.1.9.这就是我的POM.xml文件的样子:

I'm running my JSF project launching it with Spring Boot and taking advantage of the whole Spring environment. The configuration is: Mojarra 2.2.8 + Primefaces 5.1 + Spring Boot 1.1.9. That's how my POM.xml file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsf.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.1</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.tesicnor.workplace.monitor.Application</start-class>
        <java.version>1.7</java.version>
        <tomcat.version>7.0.57</tomcat.version>
        <jsf.version>2.2.8</jsf.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

如上所述,我正在配置项目以使其在tomcat 7.0.57(与Servlet 3.0兼容)启动器中运行.所有JSF功能都可以正常工作,但是问题是我无法使Primefaces p:fileUpload组件正常工作,无论是基本版本还是高级版本.文件上传侦听器不会被调用,也不会引发任何错误.

As it's specified above, I'm configuring the project to run in a tomcat 7.0.57 (Servlet 3.0 compatible) launcher. All the JSF functions are properly working, but the problem is I can't get the Primefaces p:fileUpload component work, neither the basic or the advanced versions. The file upload listener doesn't get invoked and no error thrown.

那是我的bean的代码:

That's my bean's code:

@ManagedBean
@javax.faces.bean.ViewScoped
public class Bean {

    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage message = new FacesMessage("Succesful", event.getFile()
                .getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        System.out.println("Uploaded!");
    }

}

这就是我的xhtml文件在模板下的样子:

And that's how my xhtml file looks like, under a template:

<ui:composition template="/WEB-INF/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <ui:define name="content">
        <h:form>
            <p:fileUpload fileUploadListener="#{bean.handleFileUpload}" />
        </h:form>
    </ui:define>
</ui:composition>

对于代码没有什么特别的,它必须是服务器配置问题.当我放下文件并单击上载时,FacesServlet被选中.因此,该请求已执行,并且未显示javascript错误.但是,当我在应该调用我的方法的InvokeApplicationPhase类中执行调试时,我发现没有要处理的事件.因此FileUploadEvent未被附加到循环中.

Nothing special about the code at all, it must be a server configuration issue. When I drop the file and click on upload, the FacesServlet gets hit. So the request is performed and no javascript errors shown. But when I perform a debugging into the InvokeApplicationPhase class, where my method should be invoked, I find no events to be processed. So the FileUploadEvent is not being attached to the cycle.

此外,这是另一个项目的调试堆栈,该项目正确执行了Tomcat 7和该JSF版本的文件上传:

Furthermore, that's the debugging stack of another project that properly performs the file upload with Tomcat 7 and that JSF version:

在这里NativeFileUploadDecoder被调用.但是,这在我的Spring启动项目中没有发生,并且没有调用FileUpload#visitTree以下的方法.

Here the NativeFileUploadDecoder is being called. However that's not happening in my Spring boot project and no method below FileUpload#visitTree is invoked.

我尝试了其他选择,发现当我使用某些<h:form enctype="multipart/form-data">时,即使我放置了简单的h:commandButton,也不会调用内部组件的任何操作方法.

I tried other choices and found out that when I use some <h:form enctype="multipart/form-data"> none of the action methods for components inside are invoked, even when I place a plain h:commandButton.

推荐答案

最后,我使用Apache Commons库使它正常工作.类似地,我们可能会在标准的 web.xml 文件中执行的操作,这就是我的上下文初始化程序:

Finally, I got it working using the Apache Commons library. Similarly that what we might do in a standard web.xml file, that's my context initializer:

@Bean
public ServletContextInitializer initializer() {
    return new ServletContextInitializer() {
        @Override
        public void onStartup(ServletContext servletContext)
                throws ServletException {
            servletContext.setInitParameter("primefaces.THEME", "bluesky");
            servletContext.setInitParameter(
                    "javax.faces.FACELETS_SKIP_COMMENTS", "true");
            servletContext.setInitParameter(
                    "com.sun.faces.expressionFactory",
                    "com.sun.el.ExpressionFactoryImpl");
            servletContext.setInitParameter("primefaces.UPLOADER",
                    "commons");
        }
    };
}

我明确地告诉Primefaces使用 commons 上传器,如docs中所述(另一种选择是使用 native ,这是行不通的.)

I explicitly tell Primefaces to use the commons uploader, like said in docs (the other choice is to use native, which is not working).

然后,只需将这两个依赖项添加到项目中,就可以开始了:

Then, just adding this two dependencies to the project, we're ready to go:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.2</version>
</dependency>

我将线程保持打开状态以基于 native 模式进行响应.

I keep the thread opened for some response based in the native mode.

另请参见:

这篇关于Primefaces FileUpload在Spring Boot中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆