2016-03-01 62 views
0

我试图实现primefaces文件上传,但没有调用bean方法,顺便说一下我使用Spring框架和prettyfaces:Primefaces <P:文件上传不调用Bean方法

faces-config.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 
    <application> 
     <resource-bundle> 
      <base-name>label</base-name> 
      <var>msg</var> 
     </resource-bundle> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
    </application> 
</faces-config> 

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_3_0.xsd" 
    version="3.0"> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/application-context.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
     <param-value>.xhtml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>log4j-config-location</param-name> 
     <param-value>WEB-INF/log4j.properties</param-value> 
    </context-param> 
    <listener> 
     <listener-class>co.com.core.commons.LogContextListener</listener-class> 
    </listener> 
    <!-- ############################################# --> 
    <!-- # File upload          # --> 
    <!-- ############################################# --> 
    <filter> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
     <init-param> 
      <param-name>thresholdSize</param-name> 
      <param-value>2097152</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <servlet-name>Faces Servlet</servlet-name> 
     <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> 
    <!-- ############################################# --> 
    <!-- # QUARTZ         # --> 
    <!-- ############################################# --> 
    <!-- listener> 
     <listener-class> 
      org.quartz.ee.servlet.QuartzInitializerListener 
     </listener-class> 
    </listener--> 
    <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> 
</web-app> 

.xhtml代码:

<h:form enctype="multipart/form-data" prependId="false"> 
          <p:fileUpload mode="simple" value="#{templateController.file}" /> 
          <p:commandButton value="Upload" actionListener="#{templateController.upload}" ajax="false" /> 
         </h:form> 

豆方法:

public void upload() { 
    FacesMessage msg = new FacesMessage("Success! is uploaded."); 
    FacesContext.getCurrentInstance().addMessage(null, msg); 
} 

我尝试添加转发给过滤器,因为我还使用漂亮的面孔,但仍然没有工作,谢谢。

+0

托管bean的范围是什么? – SAP

+0

它有会话范围 –

回答

0

好我解决了很多问题,我的解决方案是在web.xml文件中添加一个context-param(primefaces.UPLOADER)(检查this答案请),这个文件看起来是这样的,也可能是有帮助的设置项目阶段发展(在此文件中注释),这可以给你相关的调试过程中的一些附加信息:

<?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_3_0.xsd" 
    version="3.0"> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- context-param> 
      <param-name>javax.faces.PROJECT_STAGE</param-name> 
      <param-value>Development</param-value> 
    </context-param--> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/application-context.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
     <param-value>.xhtml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>log4j-config-location</param-name> 
     <param-value>WEB-INF/log4j.properties</param-value> 
    </context-param> 
    <listener> 
     <listener-class>co.com.core.commons.LogContextListener</listener-class> 
    </listener> 
    <!-- ############################################# --> 
    <!-- # File upload          # --> 
    <!-- ############################################# --> 
    <filter> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
     <init-param> 
      <param-name>thresholdSize</param-name> 
      <param-value>2097152</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <servlet-name>Faces Servlet</servlet-name> 
     <dispatcher>FORWARD</dispatcher> 
    </filter-mapping> 

    <context-param> 
     <param-name>primefaces.UPLOADER</param-name> 
     <param-value>commons</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>*.xhtml</url-pattern> 
    </servlet-mapping> 
</web-app> 
0

尝试执行以下,

此方法添加到你的bean,

public void handleProfileFileUpload(FileUploadEvent event) { 
    if (event != null) { 


     try { 
      InputStream inputStream = event.getFile().getInputstream(); 
      // Set inputstream to your object 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

删除命令按钮并修改类似于这样的FileUpload配置,fileUploadListener最后经过处理上传

<p:fileUpload fileUploadListener="#{<your bean>.handleProfileFileUpload}" 
          mode="advanced" sizeLimit="2097152" 
          allowTypes="/(\.|\/)(gif|jpe?g|png)$/" /> 
+0

已经尝试过,但仍然无法正常工作。 –

+0

您可以在handleProfileFileUpload方法中的事件对象的调试中看到什么? – SAP

+0

好吧不多说真的,无论是在网络浏览器控制台都没有任何的js错误 –

0

如果你正在使用Spring MVC,你的过滤器映射应该指向Spring DispatcherServlet而不是Faces Servet。

<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>Spring MVC Servlet</servlet-name> 
</filter-mapping> 
+0

你的意思是Spring MVC? OP没有使用它。据我所知,使用JSF和MVC同时是一个不错的选择(如果有的话,请参阅http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc) – Kukeltje

+0

我使用Spring MVC/Webflow和Primefaces和JPA,没有任何问题。我不相信这是一个不错的选择 当你说“OP不使用它”时,你是什么意思? –

+0

你使用Spring MVC还是Spring Webflow?两种不同的技术,如果我阅读我仔细发布的链接。你可以使用Spring的Webflow与JSF,但并非Spring MVC框架(尽管你可以,如果我正确地读它运行它们并排)。所以,如果你真的这样做比这更多的,我会很感兴趣,如果有一些代码公开访问,看看它是如何实际使用,并可能纠正我发布的链接 – Kukeltje