p:fileUpload不能使用jsf 2.0和PF 4.0

时间:2013-11-22 08:32:46

标签: ajax file-upload jsf-2 primefaces view-scope

因为我找不到其他问题的解决方案(请参阅p:fileUpload calls bean constructor for each file),我创建了一个全新且干净的Project(带有PrimeFaces 4.0的JSF 2.0),并试图让p:fileupload运行。

没有成功 - 组件显示正确(即使我没有指定过滤器),每次点击“上传”,它都会重新创建Bean(可能是ajax问题?)

长话短说,我在这里提出了代码,所以任何人都可以看到它不起作用 - 希望有人能找到错误。

输出是(例如,如果我上传三个文件:

  • @constructor(OK - 显示页面的第一个bean构建)
  • @load(好的 - 显示页面的第一个bean构建)
  • @constructor
  • @constructor
  • @constructor
  • @load
  • @load
  • @load

(甚至postconstruct被调用三次......)

我该怎么办?我再也忍不住了......

test.xhtml: (我删除了fileUploadListener =“#{fileUploadBean.handleFileUpload}”来简化代码......你可以添加它,但问题出在其他地方)

<!DOCTYPE html>
<html 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" >
    <h:head> 
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </h:head>
    <f:event type="preRenderView" listener="#{fileUploadBean.load}" />
    <h:body>
            <h:form>  
                    <p:fileUpload mode="advanced" dragDropSupport="true" multiple="true" />  
            </h:form>
    </h:body>
</html>   

FileUploadBean.java:

package beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

@ManagedBean
@ViewScoped
public class FileUploadBean implements Serializable{

    private static final long serialVersionUID = 1L;
    private List<String> uploadedFiles; 

    public FileUploadBean() {
            super();
            System.out.println("@constructor");
            uploadedFiles = new ArrayList<String>();
    }

    public void load(){
            if(FacesContext.getCurrentInstance().isPostback()){
                    System.out.println("@load postback abort");
                    return; //ajax überspringen!
            }
            System.out.println("@load");
    }

    public void handleFileUpload(FileUploadEvent event) {
        System.out.println("! HANDLE FILE UPLOAD !");
        uploadedFiles.add(event.getFile().getFileName());
        System.out.println("! FILE ADDED !");
    } 

    public List<String> getUploadedFiles() {
        return uploadedFiles;
    }  

}

的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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0" >

  <display-name>TestProjekt</display-name>
  <welcome-file-list>
    <welcome-file>test.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>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping> 
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>facelets.SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
    </context-param>

  <context-param>
    <description>
    This parameter tells MyFaces if javascript code should be allowed in
    the rendered HTML output.
    If javascript is allowed, command_link anchors will have javascript code
    that submits the corresponding form.
    If javascript is not allowed, the state saving info and nested parameters
    will be added as url parameters.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param> 
    <description> 
    If true, rendered HTML code will be formatted, so that it is 'human-readable'
    i.e. additional line separators and whitespace will be written, that do not
    influence the HTML code.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
    If true, a javascript function will be rendered that is able to restore the
    former vertical scroll on every request. Convenient feature if you have pages
    with long lists and you do not want the browser page to always jump to the top
    if you trigger a link or button action that stays on the same page.
    Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>


</web-app>

如前所述,当我包含过滤器时没有区别:

<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

0 个答案:

没有答案