PrimeFaces 4.0 FileUpload适用于Mojarra 2.2,但不适用于MyFaces 2.2 [英] PrimeFaces 4.0 FileUpload works with Mojarra 2.2 but not MyFaces 2.2

查看:123
本文介绍了PrimeFaces 4.0 FileUpload适用于Mojarra 2.2,但不适用于MyFaces 2.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PrimeFaces 4.0最终的FileUpload元素有一个有趣的问题。
我尝试运行:


  • PrimeFaces 4.0 final

  • Apache MyFaces 2.2。 0-beta

  • Tomcat 7.0.27


    我现在有一个非常简单的设置,



    XHTML页面:

     <?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>
    xmlns:h =http://java.sun.com/jsf/html
    xmlns:f =http://java.sun.com/jsf/core
    xmlns:p =http://primefaces.org/ui>
    < h:head>
    < / h:头>
    < h:body>
    < h:form>
    fileUploadListener =#{fileUploadController.handleFileUpload}
    mode =advancedupdate =messagessizeLimit =100000
    allowTypes =/ (\。| \ /)(gif | jpe?g | png)$ //>

    < p:growl id =messagesshowDetail =true/>
    < / h:表格>
    < / h:body>
    < / html>

    使用此支持bean:

      import javax.faces.application.FacesMessage; 
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import javax.faces.context.FacesContext;

    import org.primefaces.event.FileUploadEvent;

    @ManagedBean
    @RequestScoped
    public class FileUploadController
    {
    public void handleFileUpload(FileUploadEvent event)
    {
    FacesMessage msg = new FacesMessage(Succesful,event.getFile()
    .getFileName()+is uploaded。);
    FacesContext.getCurrentInstance()。addMessage(null,msg);


    $ / code $ / pre

    当选择一个文件并上传时,没有任何反应。

    上传提交通过以下响应成功:

     < ?xml version =1.0encoding =UTF-8?>< partial-response>< changes>< update id =j_id__v_0:javax.faces.ViewState:1><![CDATA [2C7ZmtwSmrlbgI / wJLI2CLBaMOQP9R / pYkIXpHlXkhSKIhtfFM0sx0HmL8o9MQY2MdHXg4t1vUjJbUYkAdFBmOQUaFy7hFhPr34Za4hOuLW4CPNx]] GT;< /更新>< /更改>< /部分响应> 

    但没有显示消息,如果我设置了一个断点, p>

    但是,如果我拔出MyFaces 2.2.0-beta并放入Mojarra 2.2.0,一切都按预期工作。



    我宁愿继续使用MyFaces,因为它是我以前使用过的,所以如果有人有任何想法,以补丁来得到这个工作,它将不胜感激。



    谢谢

    web.xml

     <?xml version =1.0encoding =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
    id =WebApp_IDversion =3.0>信息信范范范范范内中读挥读亦范范当间范范
    < welcome-file-list>
    < welcome-file> index.html< / welcome-file>
    < welcome-file> index.htm< / welcome-file>辛辛范范范中范范辛辛程范中的区范辛辛亦方程区读亦
    < welcome-file> default.html< / welcome-file>
    < welcome-file> default.htm< / welcome-file>辛辛范范范范范范范中内预亦或预定/内范读范辛
    < / welcome-file-list>
    < servlet>
    < servlet-name> Faces Servlet< / servlet-name>
    < servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
    <加载启动> 1< /加载启动>
    < / servlet>
    < 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>状态保存方法:'client'或'server'(= default)。见JSF规范2.5.2< / description>
    < param-name> javax.faces.STATE_SAVING_METHOD< / param-name>
    < param-value>客户端< /参数值>
    < / context-param>
    < context-param>
    < description>信息读lain辛范范内。范范范范范教范内预范范范范uring范范范范范文,篇章范范,
    如果javascript被允许,command_link定位符将会有javascript代码
    提交相应的表单。
    如果javascript不允许,状态保存信息和嵌套参数
    将被添加为url参数。
    默认值为'true'< / description>
    < param-name> org.apache.myfaces.ALLOW_JAVASCRIPT< / param-name>
    < param-value> true< /参数值>
    < / context-param>
    < context-param>
    < description>
    如果为true,呈现的HTML代码将被格式化,以便它是人类可读的
    ,即额外的行分隔符和空格将被写入,
    不影响HTML代码。
    默认值为'true'< / description>
    < param-name> org.apache.myfaces.PRETTY_HTML< / param-name>
    < param-value> true< /参数值>
    < / context-param>
    < context-param>
    < param-name> org.apache.myfaces.DETECT_JAVASCRIPT< / param-name>辛读范范范范图中范读亦作范亦预目亦信息
    < / context-param>
    < context-param>
    < description>
    如果为true,则会呈现一个javascript函数,可以在每个请求中恢复
    前一个垂直滚动。方便的功能,如果你有页面
    与长列表,你不希望浏览器页面总是跳转到顶部
    ,如果你触发一个链接或按钮操作,保持在同一页面上。
    默认为'false'
    < / description>
    < param-name> org.apache.myfaces.AUTO_SCROLL< / param-name>
    < param-value> true< /参数值>
    < / context-param>
    < listener>
    < listener-class> org.apache.myfaces.webapp.StartupServletContextListener< / listener-class>
    <! - < listener-class> com.sun.faces.config.ConfigureListener< / listener-class> - >
    < / listener>





    更新



    似乎Myfaces 2.2.0-beta在使用Servlet 3.x中的Part API方面存在问题。



    udaykiran pulipati 有一个解决方案的一部分使用网络 web.xml 过滤器需要PrimeFaces 3.x和公用文件上传& commons io jars,但是,我们还需要在 web.xml 中添加以下context-param,否则过滤器将被忽略:

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

    这将强制PrimeFaces使用公共库来解决问题。

    这就是说,我仍然想知道为什么MyFaces似乎无法使用Servlet Part API,如果有人有任何想法。我怀疑它可能与我的Tomcat版本,因为我只在7.0.27,但我怀疑这。





    >

     <! -  PrimeFaces FileUpload Filter  - > 
    < 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>

    并将jar添加到lib文件夹。 PrimeFaces需要下面的jar文件上传。



    commons-fileupload-1.3.jar
    commons-io-2.4.jar

    I am having an interesting problem with the PrimeFaces 4.0 final FileUpload element. I am trying to run:

    • PrimeFaces 4.0 final
    • Apache MyFaces 2.2.0-beta
    • Tomcat 7.0.27

    I have a very simple setup right now,

    XHTML page:

    <?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">
    <h:head>
    </h:head>
    <h:body>
    <h:form>
        <p:fileUpload
            fileUploadListener="#{fileUploadController.handleFileUpload}"
            mode="advanced" update="messages" sizeLimit="100000"
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
    
        <p:growl id="messages" showDetail="true" />
    </h:form>
    </h:body>
    </html>
    

    With this backing bean:

    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import javax.faces.context.FacesContext;
    
    import org.primefaces.event.FileUploadEvent;
    
    @ManagedBean
    @RequestScoped  
    public class FileUploadController
    {
        public void handleFileUpload(FileUploadEvent event)
        {
            FacesMessage msg = new FacesMessage("Succesful", event.getFile()
                    .getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    

    When selecting a file and uploading it, nothing happens.

    The upload submit succeeds with the following response:

    <?xml version="1.0" encoding="UTF-8"?><partial-response><changes><update id="j_id__v_0:javax.faces.ViewState:1"><![CDATA[2C7ZmtwSmrlbgI/wJLI2CLBaMOQP9R/pYkIXpHlXkhSKIhtfFM0sx0HmL8o9MQY2MdHXg4t1vUjJbUYkAdFBmOQUaFy7hFhPr34Za4hOuLW4CPNx]]></update></changes></partial-response>
    

    but no message is displayed, and if I set a breakpoint, it does not get hit.

    If, however, I pull out MyFaces 2.2.0-beta and put in Mojarra 2.2.0, everything works as expected.

    I would prefer to continue to use MyFaces as it is what I've used in the past, so if anyone has any ideas as to a patch to get this to work, it would be much appreciated.

    Thank you

    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"
        id="WebApp_ID" version="3.0">
    <display-name>UploadTest</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</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>*.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>
        <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>
    <listener>
        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
    <!--        <listener-class>com.sun.faces.config.ConfigureListener</listener-class> -->
    </listener>
    

    Update

    It seems that Myfaces 2.2.0-beta has problems using the Part API present in servlet 3.x.

    udaykiran pulipati has part of a solution with using web the web.xml filters that PrimeFaces 3.x required and the commons file upload & commons io jars, however, we also need to add the following context-param to the web.xml or the filters get ignored :

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

    This will force PrimeFaces to use the commons library which fixes the problem

    That being said, I would still like to know why MyFaces can't seem to use the servlet Part API if anyone has any ideas. I suspect it may have to do with my Tomcat version as I am only on 7.0.27, but I doubt that.


    解决方案

    Mention below filters in web.xml file for uploading a file using PrimeFaces

    <!-- PrimeFaces FileUpload Filter -->
    <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>
    

    and add jars to lib folder. PrimeFaces needs below jars for fileuploading.

    commons-fileupload-1.3.jar, commons-io-2.4.jar

    这篇关于PrimeFaces 4.0 FileUpload适用于Mojarra 2.2,但不适用于MyFaces 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆