0

我按照BalusC的一步一步教程做了:JSF 2.0 File upload

但是UploadedFile当我单击commandButton.

网页.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>web</display-name>
<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>/faces/*</url-pattern>
</servlet-mapping>

<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>
</filter-mapping>
.....some <context-param> generated automatically
</web-app>

网页:

<h:form id="register_doctor_form" encrypt="multipart/form-data">
    <table>
        <tr>
            <td><h:outputText value="&#160;&#160;Image"></h:outputText></td>
            <td>
                <t:inputFileUpload value="#{DoctorRegisterBean.image}"></t:inputFileUpload>
            </td>
        </tr>
        <tr>
            <td><h:outputText id="submit_message" value="#{DoctorRegisterBean.message_submit}"></h:outputText></td>
            <td id="last_td">
                <h:commandButton class="button" value="Create Doctor" action="#{DoctorRegisterBean.registerDoctor}">
                    <f:ajax execute="register_doctor_form" render="submit_message"></f:ajax>
                </h:commandButton>
            </td>
        </tr>
    </table>
</h:form>

我还阅读了这篇文章:UploadedFile is null when submit jsf upload request with Tomahawk,但我的表单没有嵌套。

ManagedBean

public class DoctorRegisterBean implements Serializable{
    private UploadedFile image;
    public void registerDoctor() {
        byte[] imageBytes = image.getBytes();
        Blob blob = new SerialBlob(imageBytes);
    }
}

这里image是NULL。那么这段代码有什么问题呢?

我在tomcat下使用.war文件运行这个项目,当我点击时给出错误信息commandButton,这里是错误信息:

javax.faces.el.EvaluationException: javax.el.ELException: /admin_doctorregister.xhtml at line 124 and column 115 action="#{DoctorRegisterBean.registerDoctor}": java.lang.NullPointerException
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:937)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:271)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1249)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:675)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.ELException: /admin_doctorregister.xhtml at line 124 and column 115 action="#{DoctorRegisterBean.registerDoctor}": java.lang.NullPointerException
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:95)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
... 29 more
Caused by: java.lang.NullPointerException
at edu.upenn.cis.rtg.ohc.web.DoctorRegisterBean.registerDoctor(DoctorRegisterBean.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
... 30 more
4

1 回答 1

4
<h:form id="register_doctor_form" encrypt="multipart/form-data">

应该是问题

<h:form id="register_doctor_form" enctype="multipart/form-data">
于 2013-04-10T04:38:43.253 回答