我在高级模式下使用Primefaces fileUpload组件来上传多个图像。当我选择几个文件并按上传按钮时,我的@ViewScoped托管bean会重新创建多次。这对我来说是一个问题,因为我想将一个视图交互中上传的所有文件存储在单独的文件夹中,但我得到多个文件夹 - 每个文件一个。当我创建托管bean @SessionScoped时,问题就消失了,我发现只有在第一次上传服务器重启后才出现这个错误,所以当我第二次重新加载视图并上传数据时一切正常。
我正在使用Tomcat 7,jsf 2.2和主要面孔5.1
这是jsf页面:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<f:facet name="first">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</f:facet>
<title>Jsf Upload</title>
</h:head>
<h:body>
<h:form id="growlForm">
<p:growl id="growl" showDetail="true" showSummary="true" />
</h:form>
<h:form id="uploadForm" enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{uploadController.upload}"
mode="advanced" multiple="true" label="Choose"
uploadLabel="Upload" cancelLabel="Cancel" />
</h:form>
</h:body>
</html>
托管Bean:
@ManagedBean
@ViewScoped
public class UploadController implements Serializable {
private static final long serialVersionUID = 5711090879027971547L;
private static final Logger logger = LoggerFactory
.getLogger(UploadController.class);
private List<UploadedFile> files;
public UploadController() {
logger.info("UploadController constructor call");
files = new ArrayList<UploadedFile>();
}
@PostConstruct
public void init() {
logger.info("Post Construct init() method call");
}
public void upload(FileUploadEvent event) {
logger.info("upload() method call, file = {}", event.getFile()
.getFileName());
files.add(event.getFile());
logger.info("Added file = {}", event.getFile().getFileName());
}
}
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>JsfUpload</display-name>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>bootstrap</param-value>
</context-param>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<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>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
这是我的日志输出,显示多次调用构造函数和@PostConstruct方法:
2014-12-08 18:57:24,629 [http-bio-8380-exec-7] INFO ru.duytsev.test.upload.UploadController - UploadController constructor call
2014-12-08 18:57:24,629 [http-bio-8380-exec-7] INFO ru.duytsev.test.upload.UploadController - Post Construct init() method
2014-12-08 18:57:24,645 [http-bio-8380-exec-7] INFO ru.duytsev.test.upload.UploadController - upload() method call, file = brick-yellow.png
2014-12-08 18:57:24,645 [http-bio-8380-exec-7] INFO ru.duytsev.test.upload.UploadController - Added file = brick-yellow.png
2014-12-08 18:57:24,666 [http-bio-8380-exec-6] INFO ru.duytsev.test.upload.UploadController - UploadController constructor call
2014-12-08 18:57:24,666 [http-bio-8380-exec-6] INFO ru.duytsev.test.upload.UploadController - Post Construct init() method
2014-12-08 18:57:24,666 [http-bio-8380-exec-6] INFO ru.duytsev.test.upload.UploadController - upload() method call, file = brick-purple.png
2014-12-08 18:57:24,666 [http-bio-8380-exec-6] INFO ru.duytsev.test.upload.UploadController - Added file = brick-purple.png
2014-12-08 18:57:24,678 [http-bio-8380-exec-4] INFO ru.duytsev.test.upload.UploadController - UploadController constructor call
2014-12-08 18:57:24,679 [http-bio-8380-exec-4] INFO ru.duytsev.test.upload.UploadController - Post Construct init() method
2014-12-08 18:57:24,679 [http-bio-8380-exec-4] INFO ru.duytsev.test.upload.UploadController - upload() method call, file = brick-green.png
2014-12-08 18:57:24,679 [http-bio-8380-exec-4] INFO ru.duytsev.test.upload.UploadController - Added file = brick-green.png
2014-12-08 18:57:24,693 [http-bio-8380-exec-5] INFO ru.duytsev.test.upload.UploadController - upload() method call, file = brick-orange.png
2014-12-08 18:57:24,694 [http-bio-8380-exec-5] INFO ru.duytsev.test.upload.UploadController - Added file = brick-orange.png
2014-12-08 18:57:24,715 [http-bio-8380-exec-8] INFO ru.duytsev.test.upload.UploadController - upload() method call, file = brick-blue.png
2014-12-08 18:57:24,715 [http-bio-8380-exec-8] INFO ru.duytsev.test.upload.UploadController - Added file = brick-blue.png
我的pom.xml
<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>ru.duytsev.test.upload</groupId>
<artifactId>JsfUpload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<!-- PrimeFaces -->
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.8</version>
<type>pom</type>
</dependency>
<!-- JSF -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8-02</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8-02</version>
</dependency>
<!-- Java EE -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- EL -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2.1-b05</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<!-- APACHE COMMONS -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<finalName>${pom.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:-3)
更改为@ViewScoped。 这会导致构造函数只被调用一次