UploadedFile is null when submit jsf upload reques

2019-08-03 03:35发布

trying to upload a file on a facelets page using Tomahawk2.0 1.1.11. But I get a null pointer exception and I see in the debugger, the uploadedFile variable is not being set in the bean. Any ideas why the bean isn't being set? The t:inputFileUpload component is otherwise rendering and functioning as I would expect.

page:

<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{Bean.uploadedFile}" />
<h:commandButton type="submit" id="Submit" value="Submit" action="#{Bean.submit}"/>
<h:messages />  
</h:form>

bean:

protected UploadedFile uploadedFile;
public UploadedFile getUploadedFile() { return this.uploadedFile; }
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;}

public String submit()  throws IOException { 
String fileName = FilenameUtils.getName(uploadedFile.getName()); // exception here!!
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
FacesContext.getCurrentInstance().addMessage(null, 
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", 
fileName, contentType)));

return "success";     
}

The exception happens on the first line of submit. In the debugger I see that uploadedFile is null. The jsf page looks fine and I can use the browse button to choose a file. After I press submit I see uploadedFile in the bean is null.

Error message:

16:06:59,699 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Framework].[Faces Servlet]] (http--127.0.0.1-8080-2) Servlet.service() for servlet Faces Servlet threw exception: java.lang.NullPointerException
    at net.gui.bean.Bean.submit(Bean.java:118) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_30]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_30]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_30]
    at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_30]
    at org.apache.el.parser.AstValue.invoke(AstValue.java:196) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final] 
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357) [tomahawk20-1.1.11.jar:] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at net.gui.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:34) [classes:] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at net.gui.filter.NAISAuthFilter.doFilter(NAISAuthFilter.java:59) [classes:] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.1.Final.jar:7.0.2.Final] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.0.2.Final] 
    at java.lang.Thread.run(Thread.java:662) [:1.6.0_30]

web.xml:

<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>    

<filter>
    <filter-name>NaisAuth</filter-name>
    <filter-class>net.gui.filter.NAISAuthFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>NaisAuth</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class> net.gui.filter.HibernateSessionRequestFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1条回答
smile是对你的礼貌
2楼-- · 2019-08-03 04:28

That can happen when either NAISAuthFilter or HibernateSessionRequestFilter have accessed the request body beforehand by getParameter(), getReader(), etc. This way the request body is already parsed before the ExtensionsFilter ever get chance to do it.

Either fix those filters, or rearrange the mapping of ExtensionsFilter in web.xml to be declared before those filters.

Another possible cause is that you're actually nesting multiple <h:form> compnents in each other. This will result in illegal HTML syntax and the browser behaviour is unspecified as to processing form submits. You need to ensure that you aren't nesting forms (multiple forms in parallel is however perfectly valid).

查看更多
登录 后发表回答