【问题标题】:PrimeFaces 4.0 FileUpload works with Mojarra 2.2 but not MyFaces 2.2PrimeFaces 4.0 FileUpload 适用于 Mojarra 2.2 但不适用于 MyFaces 2.2
【发布时间】:2013-12-17 11:32:30
【问题描述】:

PrimeFaces 4.0 final FileUpload 元素有一个有趣的问题。 我正在尝试运行:

  • PrimeFaces 4.0 最终版
  • 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">
<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>

使用这个支持 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);
    }
}

选择文件并上传时,什么也没有发生。

上传提交成功,响应如下:

<?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>

但是没有消息显示,如果我设置了断点,它不会被命中。

但是,如果我取出 MyFaces 2.2.0-beta 并放入 Mojarra 2.2.0,一切都会按预期运行。

我更愿意继续使用 MyFaces,因为它是我过去使用过的,所以如果有人对补丁程序有任何想法以使其工作,将不胜感激。

谢谢

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>

更新

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

udaykiran pulipati 有一部分解决方案使用 web 和 PrimeFaces 3.x 所需的web.xml 过滤器和公共文件上传和公共 io jar,但是,我们还需要将以下上下文参数添加到 @ 987654328@ 或过滤器被忽略:

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

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

话虽如此,如果有人有任何想法,我仍然想知道为什么 MyFaces 似乎无法使用 servlet Part API。我怀疑这可能与我的 Tomcat 版本有关,因为我只在 7.0.27 上,但我对此表示怀疑。


【问题讨论】:

  • 这应该不是MyFaces相关的问题,因为首先我们说的是primefaces组件,对吧? primefaces 做了什么来首先覆盖 JSF?谁知道呢,所以这超出了 MyFaces 的控制。你应该向 PrimeFaces 的人询问这个问题。
  • 实际上,它似乎与 myfaces 有关,请参阅 stackoverflow.com/questions/21948228/how-to-get-jsf-file-upload-to-work-on-tomee-1-6跨度>

标签: file-upload jsf-2 primefaces myfaces


【解决方案1】:

在web.xml 文件中提及以下过滤器,用于使用 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>

并将 jars 添加到 lib 文件夹。 PrimeFaces 需要以下 jars 来上传文件。

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

【讨论】:

  • 它在他们的用户指南中说,如果我想使用 commons-fileupload jar 而不是 servlet 3.0 api,我只需要这些过滤器。我还想指出,使用 Mojarra,文件上传在没有这些过滤器和罐子的情况下也能正常工作
【解决方案2】:

最近在MYFACES-3835 中发现了一个类似的问题,有更好的描述。这是与 webkit 浏览器相关的问题,仅当 ajax 响应足够大时才会出现。已经修好了。

【讨论】:

    【解决方案3】:

    udaykiran pulipati 的回答促使我在我的项目中用 commons-fileupload-1.3.jar 替换 commons-fileupload-1.2.2.jar,但这并没有解决我的问题,因为我使用的是 MyFaces 2.2, PrimeFaces Elite 4.0.8 和 TomEE 1.6.1-snapshot。

    另外,根据 udaykiran pulipati 的回答,我已经在我的 web.xml 中添加了 PrimeFaces FileUpload 过滤器配置,几个月前。

    所以,我查看了 PrimeFaces 4.0 用户指南,发现了一些可以在 web.xml 中指定的“新”内容。所以,我在我的 web.xml 中添加了以下内容,

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

    最后,PrimeFaces (Elite) 4.0.x FileUpload 可与 MyFaces 2.2 配合使用。

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2015-01-09
      • 2017-07-15
      相关资源
      最近更新 更多