我正在开发一个移动设备的Web应用程序,用户可以在多个场合上传文件(图像)。
由于我使用的是Primefaces移动渲染工具包,因此我必须使用Primefaces的简单p:fileupload
而不使用ajax。
问题:
当我想上传图像时,我可以选择文件(组件正确呈现),然后点击“上传”按钮。点击“上传”后,页面会刷新,但永远不会调用myBean.saveImage()
。 myBean.fileUploadController.setFile
也未被调用。我没有在Netbeans控制台中收到任何错误消息,也没有在Chromes JS控制台中收到任何错误消息。
到目前为止,我尝试使用自5.0版以来的primefaces支持的本机HTTP Servlet上传和旧的commons.fileupload方式 - 结果相同。
我按this great guide对p:fileupload
进行了问题排查,但在完成指南中建议的所有内容后,我仍然得到相同的结果。
我创建了一个fileuploadtest.xhtml页面来测试上传功能,如下所示:
<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"
xmlns:pm="http://primefaces.org/mobile">
<f:view renderKitId="PRIMEFACES_MOBILE" />
<h:head>
</h:head>
<h:body>
<pm:page>
<pm:header title="Title"></pm:header>
<pm:content>
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{myBean.fileUploadController.file}" mode="simple"/>
<p:commandButton value="Upload" ajax="false" action="#{myBean.saveImage}"/>
</h:form>
</pm:content>
<pm:footer title="Footer"></pm:footer>
</pm:page>
</h:body>
</html>
这是我的bean和我的FileUploadController
类:
MyBean.java:
@Component
@Scope("session")
@Named
public class MyBean implements Serializable {
@Autowired(required = true)
private FileUploadController fileUploadController;
//Bunch of unrelated methods
public void saveImage() {
this.fileUploadController.upload();
//some more code after upload() is complete
}
//getters&setters
}
FileUploadController.java:
@Component
@Scope("session")
public class FileUploadController {
private UploadedFile file;
public boolean upload() {
if (file != null) {
//upload here
return true;
} else {
return false;
}
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
}
这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<description>Webflow Archetype</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/applicationContext.xml,/WEB-INF/config/web-application-config.xml
</param-value>
</context-param>
<!-- Causes Facelets to refresh templates during development -->
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.sendPoweredByHeader</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>cupertino</param-value>
</context-param>
<context-param>
<param-name>primefaces.FONT_AWESOME</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>Define partial state saving as true/false.</description>
<param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
<!-- Custom MarqueeComponent -->
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/marquee-taglib.xml</param-value>
</context-param>
<context-param>
<param-name>BootsFaces_USETHEME</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>charEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/ *</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Allows Jetty to serve Faces applications -->
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/ *</url-pattern>
</filter-mapping>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<session-config>
<session-timeout>180</session-timeout>
<cookie-config>
<secure>true</secure>
</cookie-config>
</session-config>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value/>
</init-param>
<load-on-startup>2</load-on-startup>
<multipart-config>
<max-file-size>10485760</max-file-size>
<max-request-size>20971520</max-request-size>
<file-size-threshold>5242880</file-size-threshold>
</multipart-config>
</servlet>
<!-- Map all /spring requests to the Dispatcher Servlet for handling -->
<servlet>
<servlet-name>Jersey Spring Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>at.managementpartners.icosysmobile.rest</param-value>
</init-param>
</servlet>
<!-- <servlet>
<servlet-name>GetClientHostnameServlet</servlet-name>
<servlet-class>at.managementpartners.icosysmobile.web.util.GetClientHostnameServlet</servlet-class>
</servlet> -->
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>at.managementpartners.icosysmobile.web.util.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/ *</url-pattern>
</servlet-mapping>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config>
<max-file-size>10485760</max-file-size>
<max-request-size>20971520</max-request-size>
<file-size-threshold>5242880</file-size-threshold>
</multipart-config>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/javax.faces.resource/ *</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/ *</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/do/ *</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!--servlet>
<servlet-name>Primefaces Resource Servlet</servlet-name>
<servlet-class>
org.primefaces.resource.ResourceServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Primefaces Resource Servlet</servlet-name>
<url-pattern>/primefaces_resource/ *</url-pattern>
</servlet-mapping-->
<!-- Welcome files -->
<servlet-mapping>
<servlet-name>Jersey Spring Servlet</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Jersey Spring Servlet</servlet-name>
<url-pattern>/rest/ *</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/files/ *</url-pattern>
</servlet-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>false</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>
我将此web.xml配置为使用commons.fileupload而不使用HTTP Servlet本机上载功能。
请注意,我必须用空格('/ *')分隔'/ *',这样我的其余代码就不会被格式化为注释。 (如果有更聪明的方法,请不要犹豫告诉我)
我的网络应用程序依赖于以下技术/库:
JSF 2.2.9
HTTP Servlet 3.0.1
Spring MVC
Spring Webflow
使用<f:view renderKitId="PRIMEFACES_MOBILE" />
Omnifaces 1.5
Bootsfaces 0.8.1
Netbeans as IDE
出于测试和调试目的,我通过Netbeans将应用程序部署到Jetty 8.1.5。
但是我尝试将应用程序部署到Tomcat服务器并获得相同的结果。
请原谅长篇文章,我只想包含一些看起来很有用的东西。如果您还有其他需要请告诉我。
非常感谢你。
我的问题似乎与p:fileupload
没有任何关系,而是当我禁用ajax时,我的p:commandButton
根本没有调用任何动作事件。为了测试这一点,我将ajax="false"
属性添加到另一个页面上的一个comandButtons中。一旦我添加它,我的commandButton就停止了工作,它只刷新了网站,但从未处理过action
属性中指定的事件。有没有人知道为什么我的命令按钮停止使用ajax="false"
?
我发布了new question,因为我发现问题与略有不同的问题有关