我在PrimeFaces 4.0最终的FileUpload元素中遇到了一个有趣的问题。 我想跑:
我现在有一个非常简单的设置,
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使用了PrimeFaces 3.x所需的web.xml
过滤器以及公共文件上传&amp;但是,我们还需要将以下context-param添加到web.xml
或者忽略过滤器:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
这将强制PrimeFaces使用修复问题的公共库
话虽如此,我仍然想知道为什么MyFaces似乎无法使用servlet Part API,如果有人有任何想法。我怀疑它可能与我的Tomcat版本有关,因为我只在7.0.27,但我怀疑。
答案 0 :(得分:3)
下面提到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>
并将jar添加到lib文件夹。 PrimeFaces需要低于jars才能进行文件上载。
答案 1 :(得分:0)
最近发现了一个类似的问题,在MYFACES-3835中有更好的描述。这是一个与webkit浏览器相关的问题,只有在ajax响应足够大时才会出现。它已经修好了。
答案 2 :(得分:0)
另外,根据udaykiran pulipati的回答,我已经在几个月前将PrimeFaces FileUpload过滤器配置添加到了我的web.xml。
所以,我查看了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配合使用。