Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 文件上传java/jsf/primefaces_Spring_Jsf_File Upload_Primefaces - Fatal编程技术网

Spring 文件上传java/jsf/primefaces

Spring 文件上传java/jsf/primefaces,spring,jsf,file-upload,primefaces,Spring,Jsf,File Upload,Primefaces,我正试图上传一个文本文件到我的服务器上,这让我疯狂了好几天。基本上,当上传文件时,假设触发#{bookController.handleFileUpload}的方法不会触发,但我没有收到任何错误,在前端,文件看起来像是上传的。我假设文件在到达我的控制器方法的过程中被消耗掉了。 这是我的web.xml <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <fi

我正试图上传一个文本文件到我的服务器上,这让我疯狂了好几天。基本上,当上传文件时,假设触发#{bookController.handleFileUpload}的方法不会触发,但我没有收到任何错误,在前端,文件看起来像是上传的。我假设文件在到达我的控制器方法的过程中被消耗掉了。 这是我的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>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>south-street</param-value>
  </context-param>
  <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>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/jpmorgan/*</url-pattern>
  </servlet-mapping>
  <context-param>

PrimeFaces文件上载筛选器
org.primefaces.webapp.filter.FileUploadFilter
PrimeFaces文件上载筛选器
Facesservlet
主题
南街
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.面孔
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/摩根大通/*
我在pom.xml中添加了必要的jar(common-io.jar和common-fileupload.jar)。 以下是我的页面,假设该页面将处理该文件:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 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"
    template="/WEB-INF/layouts/no-ajax.xhtml">

    <ui:define name="head">

    </ui:define>

    <ui:define name="title">#{msg['add_book']}</ui:define>

    <ui:define name="content">
        <h:form id="form" enctype="multipart/form-data">

            <p:messages id="messages" autoUpdate="true"/>
            <p:panelGrid id="grid">

            <p:row> <p:column>
                <h:outputLabel for="name" value="#{msg['label_book_title']}" />
                <p:spacer width="100" height="10" />
                <p:inputText id="name" value="#{bookBean.book.title}"/>
            </p:column> </p:row>    
            <p:row> <p:column>

               <p:fileUpload id="upload" value="#{bookBean.file}"
                              dragDropSupport="true"
                              update="messages"
                              fileUploadListener="#{bookController.handleFileUpload}" />

            </p:column> </p:row>
            </p:panelGrid>

            <p:toolbar>  
                <p:toolbarGroup align="left">
                    <p:commandButton value="#{msg['button_next']}" action="next" execute="@form" update="@form"/>
                    <p:commandButton value="#{msg['button_cancel']}" action="exit" execute="@form" update="@form"/>
                </p:toolbarGroup>
            </p:toolbar>

        </h:form>
    </ui:define>

    <ui:define name="help">

    <h:outputText value="#{msg['help_add_page']}" /> 

    </ui:define>

</ui:composition>

#{msg['add_book']}
以上页面的布局:

<?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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:sec="http://www.springframework.org/security/tags"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<f:view contentType="text/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>#{msg['application_name']}</title>

        <link rel="icon" href="resources/resources/images/favicon.ico" type="image/x-icon" />
        <link rel="shortcut icon" href="resources/resources/images/favicon.ico" />
        <link rel="stylesheet" href="resources/resources/styles/blueprint/screen.css" type="text/css" media="screen, projection" />
        <link rel="stylesheet" href="resources/resources/styles/blueprint/print.css" type="text/css" media="print" />
        <!--[if lt IE 8]>
            <link rel="stylesheet" href="${request.servletPath}/styles/blueprint/ie.css" type="text/css" media="screen, projection" />
        <![endif]-->
    </h:head>

    <h:body>
        <p:layout fullPage="true">

            <p:layoutUnit id="top" position="north" header="#{msg['application_name']}" size="100" collapsible="true" collapsed="false" resizable="true">
                <ui:insert name="title" />
            </p:layoutUnit>

            <p:layoutUnit id="west" position="west" size="250" header="#{msg['label_help']}" resizable="true" collapsible="true" collapsed="false">
                <ui:insert name="help" />
            </p:layoutUnit>

            <p:layoutUnit id="center" position="center" >
                <ui:insert name="content" />
            </p:layoutUnit>

        </p:layout>

    </h:body>

</f:view>
</html>

#{msg['application_name']}
有人知道我做错了什么吗?
非常感谢。

我想问题出在你的
上。使用AJAX上传文件是不可能的,因此您需要将
AJAX=“false”
添加到您的
p:commandButton
中,也许您必须使用actionListener而不是action,但在后期,我不确定。您应该开始在您的上下文中尝试这个示例。

您的代码似乎是正确的,我遇到了与JSF 2.2和Primefaces 5.3相同的问题

我上传的代码是

public void uploadPhoto(FileUploadEvent e) throws IOException {

    UploadedFile uploadedPhoto = e.getFile();

    String filePath =  "TO/THE/PATH";
    byte[] bytes = null;

    if (null != uploadedPhoto) {
        bytes = uploadedPhoto.getContents();
        String filename = FilenameUtils
                .getName(uploadedPhoto.getFileName());
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(filePath + filename)));
        stream.write(bytes);
        stream.close();
    }
    setProduitIMG(uploadedPhoto.getFileName());
    setImageSize(uploadedPhoto.getSize());

    FacesContext.getCurrentInstance().addMessage(
            "messages",
            new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Your Photo (File Name " + uploadedPhoto.getFileName()
                            + " with size " + uploadedPhoto.getSize()
                            + ")  Uploaded Successfully", ""));
}
您可以在托管bean中使用它,而不需要创建新的fileBean,您可以将它与任何managedbean类合并,它工作正常


JSF 2.1或2.0在本例中不起作用使用spring integration,您必须升级您的项目,它会很好地工作

Hi BalusC,我已经设法在不同的项目中运行代码,但一旦它转移到我的项目中,它就不起作用了。这里有一些示例,但与我的不同之处在于我在web.xml中使用了更多配置。我还尝试在我的项目中简化它,没有layout.xhtml和只包含upload元素的表单,但它仍然不起作用。我确信问题不在于编码部分,而在于配置部分(web.xml)