【问题标题】:Primefaces File Upload Stop Working After I have added PrettyFaces添加 PrettyFaces 后 Primefaces 文件上传停止工作
【发布时间】:2018-05-13 07:09:27
【问题描述】:

首先,我使用Primefaces 6.1PrettyFaces 3.3.3Wildfly 11 的技术。我只是想用 Primefaces 将文件上传到我的数据库 <p:fileUpload> 组件。这是我用过的代码sn-p。

<h:form>
    <p:fileUpload fileUploadListener="#{imageopbean.uploadImage}"  mode="advanced" auto="true" style="display:none;" oncomplete="uppicture()" styleClass="imageUploadButton" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" >
          <f:attribute name="uniqueid" value="#{profileBean.theUser.userId}"/>
     </p:fileUpload>
</h:form>

在我使用 PrettyFaces 库之前它就可以工作。现在,它不会调用fileUploadListener。我已阅读 this 问题和其他一些解决方案。但我不能让他们工作。我究竟做错了什么?这是我的代码的其他部分。谢谢。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>extranet</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
        <param-value>2048576</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>2</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>

    <filter>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <servlet-name>facesServlet</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <url-pattern />
    </filter-mapping>

    <filter>
        <filter-name>Pretty Filter</filter-name>
        <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
    </filter>

    <filter-mapping> 
        <filter-name>Pretty Filter</filter-name> 
        <url-pattern>/*</url-pattern> 
        <dispatcher>FORWARD</dispatcher> 
        <dispatcher>REQUEST</dispatcher> 
        <dispatcher>ERROR</dispatcher>
        <dispatcher>ASYNC</dispatcher>
    </filter-mapping>

    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>

    <session-config>
        <session-timeout>
            60
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>    

    <error-page>
        <error-code>404</error-code>
        <location>/error-404.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/login</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/login</location>
    </error-page>
</web-app>

ImageBean.java

@Named("imageopbean")
@RequestScoped
public class ImageBean implements Serializable {

    @EJB
    private ImageManager imageManager;

    /**
     *
     * @param event
     */
    public void uploadImage(FileUploadEvent event) {
        System.out.println("UPLOAD WORK");
        ....
    }
}

【问题讨论】:

  • 如果您“自动”添加了 PrettyFaces 过滤器(通过添加 jar),也许您需要添加一些显式排序(您可以在您在问题中链接到的帖子,在“疑难解答”中排名第一)
  • 我在副本中的 #1 中添加了一些额外的注释。对你有帮助吗?
  • 我正在使用带注释的网络过滤器,所以您的意思是网络过滤器可能会消耗上传请求?
  • 我更新我的 web.xml 仍然无法正常工作。您可以查看问题@Kukeltje

标签: file-upload primefaces jsf-2 prettyfaces


【解决方案1】:

我已经尝试了 web.xml 中的大多数过滤器组合,但没有任何效果。 我找到了这个,

我已经删除了我在web.xmlpom.xml 中插入的所有内容。 再次给出解决方案:

1- 将enctype="multipart/form-data" 添加到您的&lt;h:fom&gt; 标签

2- 根据您的 JSF 页面编辑脚本

<script type="text/javascript">
     $(document).ready(function() {
         $("form[enctype='multipart/form-data']").attr("action","#{request.contextPath}/test/fileupload.xhtml");
    });
</script>

仅此而已..

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    相关资源
    最近更新 更多