spring boot primefaces fileupload不起作用

时间:2014-11-26 10:52:35

标签: file-upload primefaces spring-boot

我正在尝试使用spring boot和primefaces设置应用程序,但fileupload函数不起作用。我已经尝试了几种配置而没有成功。请有人帮我解决这个问题。

这是配置:

的pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.2.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.2.RELEASE</version>
    </dependency>-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.2-1003-jdbc4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.8-02</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.8-02</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.1.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.0</version>
        <scope>compile</scope>
    </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>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>8.0.15</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>8.0.15</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>8.0.15</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>1.1.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.1.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>all-themes</artifactId>
        <version>1.0.10</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

</dependencies>

<build>
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>


<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>
</project>

Spring Boot Application class。

@Configuration
@ComponentScan
public class Application extends SpringBootServletInitializer  {
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
protected SpringApplicationBuilder configure(
             SpringApplicationBuilder application) {
    return application.sources(new Class[] { Application.class, Initializer.class});
}

@Bean
public ServletRegistrationBean servletRegistrationBean() {
    FacesServlet servlet = new FacesServlet();
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
    return servletRegistrationBean;
}

@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener()         {
    return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}

@Bean
public ServletListenerRegistrationBean<RequestContextListener> requestContextListener()         {
    return new ServletListenerRegistrationBean<RequestContextListener>(new RequestContextListener());
}

@Bean
public FilterRegistrationBean contextFilterRegistrationBean() {

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    FileUploadFilter fileUploadFilter = new FileUploadFilter();
    registrationBean.setFilter(fileUploadFilter);
    registrationBean.addServletRegistrationBeans(servletRegistrationBean());
    registrationBean.setOrder(2);
return registrationBean;
}

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.setPort(8083);
    return tomcat;
}

JSF表格

<h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#{fileBean.handleFileUpload}" />
    </h:form>

JSF Backing Bean

@ManagedBean
@RequestScoped
public class FileBean {
private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void handleFileUpload(FileUploadEvent event) {
    System.out.println("in handle file upload...");
    UploadedFile file = event.getFile();
    System.out.println(file.getFileName());
    //application code
}

0 个答案:

没有答案