You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by le3000 <bi...@yahoo.com> on 2008/01/26 01:47:06 UTC

tomahawk-1.1.6 UploadedFile not working.

I follow the example in "Upload Files with JSF and MyFaces" from ONJava.com
to do the file upload. I am having problem on UploadedFile null pointer. 
The UploadedFile always return null. actually, the setter-setMyFile never
even get called.

Here is what I have in my jsp:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> 

<f:subview id="sendJobsub">
	  <h:form id="sendJob" enctype="multipart/form-data">	  	                     
          <h:messages globalOnly="true" styleClass="message"/>
    	  <h:panelGrid columns="3" border="0" cellspacing="5">
              <h:outputLabel for="myFileId" value="File: "/>
              <t:inputFileUpload id="myFileId" value="#{SendJobBean.myFile}"
storage="file" required="true"/>
        
              <h:outputLabel for="myParamId" value="Param: "/>
              <h:selectOneMenu id="myParamId" value="#{SendJobBean.myParam}" 
required="true">
                  <f:selectItem itemLabel="" itemValue=""/>
                  <f:selectItem itemLabel="MD5" itemValue="MD5"/>
                  <f:selectItem itemLabel="SHA-1" itemValue="SHA-1"/>
                  <f:selectItem itemLabel="SHA-256" itemValue="SHA-256"/>
                  <f:selectItem itemLabel="SHA-384" itemValue="SHA-384"/>
                  <f:selectItem itemLabel="SHA-512" itemValue="SHA-512"/>
              </h:selectOneMenu>
              <h:message for="myParamId"/>

              <h:outputText value=" "/>
              <h:commandButton value="Upload XML File"
action="#{SendJobBean.processXMLFile}"/>
              <h:outputText value=" "/>       
          </h:panelGrid>          
    </h:form>    
</f:subview>

Here is my web.xml:
   <filter>
        <filter-name>ExtensionsFilter</filter-name>
       
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
        <init-param>                
                <param-name>uploadMaxFileSize</param-name>
                <param-value>100m</param-value>
        </init-param>
        <init-param>                
                <param-name>uploadThresholdSize</param-name>
                <param-value>100k</param-value>
        </init-param>  
    </filter>
    <filter-mapping>
		<filter-name>ExtensionsFilter</filter-name>
		<servlet-name>Faces Servlet</servlet-name>
	</filter-mapping>
	<filter-mapping>
		<filter-name>ExtensionsFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

My bean is very simple:
........
private UploadedFile myFile;
........
public void setMyFile(UploadedFile myFile){
        System.out.println("setting uploaded file");
        this.myFile=myFile;
    }

    public UploadedFile getMyFile(){
        System.out.println("getting uploaded file");
        return this.myFile;
    }
    public void processXMLFile() throws Exception {
        String myResult="";
        try {
            MessageDigest md  = MessageDigest.getInstance(myParam);            
            InputStream in = new
BufferedInputStream(myFile.getInputStream());
            try {
                byte[] buffer = new byte[64 * 1024];
                int count;
                while ((count = in.read(buffer)) > 0)
                    md.update(buffer, 0, count);
            } finally {
                in.close();
            }
            byte hash[] = md.digest();
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < hash.length; i++) {
                int b = hash[i] & 0xFF;
                int c = (b >> 4) & 0xF;
                c = c < 10 ? '0' + c : 'A' + c - 10;
                buf.append((char) c);
                c = b & 0xF;
                c = c < 10 ? '0' + c : 'A' + c - 10;
                buf.append((char) c);
            }
            myResult = buf.toString();
            System.out.println(myResult);
        } catch (Exception x) {
            x.printStackTrace();
            FacesMessage message = new FacesMessage(
                FacesMessage.SEVERITY_FATAL,
                x.getClass().getName(), x.getMessage());
            FacesContext.getCurrentInstance().addMessage( null, message);            
        }
    }

It always always catch null pointer on UploadedFile, and the
UploadedFile(myFile) getter being call when I load that page, but the setter
never being called.

Please help. I've been struggling on this for days. :-(
-- 
View this message in context: http://www.nabble.com/tomahawk-1.1.6-UploadedFile-not-working.-tp15100128p15100128.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: tomahawk-1.1.6 UploadedFile not working.

Posted by le3000 <bi...@yahoo.com>.
I finally figure out the problem. I accidentally put the <t:inputFileUpload>
inside a nested form. 

Thank you so much for the help. :-)
-- 
View this message in context: http://www.nabble.com/tomahawk-1.1.6-UploadedFile-not-working.-tp15100128p15167022.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: tomahawk-1.1.6 UploadedFile not working.

Posted by david delbecq <de...@oma.be>.
le3000 a écrit :
> And the subview get imported using something like: <c:import
> url="${param.service}.jsp"/>
>   
check subview does exist upon submit, i suspect your param.service does 
not exist upon submit, leading to upload component not existing in tree, 
itself leading to setters not called. Difficult to tell without full 
code. Try to use some tool like faces trace to get dump of component 
tree and lifecycle steps actually executed.

Re: tomahawk-1.1.6 UploadedFile not working.

Posted by le3000 <bi...@yahoo.com>.
And the subview get imported using something like: <c:import
url="${param.service}.jsp"/>
-- 
View this message in context: http://www.nabble.com/tomahawk-1.1.6-UploadedFile-not-working.-tp15100128p15148057.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: tomahawk-1.1.6 UploadedFile not working.

Posted by le3000 <bi...@yahoo.com>.
I just found that if I use it in a top level <f:view>, it works just fine.
But once I use it in a <f:subview>, it failed. why is that?
-- 
View this message in context: http://www.nabble.com/tomahawk-1.1.6-UploadedFile-not-working.-tp15100128p15148005.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: tomahawk-1.1.6 UploadedFile not working.

Posted by le3000 <bi...@yahoo.com>.
I import org.apache.myfaces.custom.fileupload.* in my Java Bean.

The file I am trying to upload is a 4kb xml file.

I am using tomahawk-1.1.6.jar and commons-fileupload-1.2.jar package.
-- 
View this message in context: http://www.nabble.com/tomahawk-1.1.6-UploadedFile-not-working.-tp15100128p15147577.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: tomahawk-1.1.6 UploadedFile not working.

Posted by david delbecq <de...@oma.be>.
Check you are correctly using 
"org.apache.myfaces.custom.fileupload.UploadedFile" for the type of you 
java bean. Since you didn't provide us with import rules, it might be 
you are using the Trinidad Uploadedfile or 
org.apache.commons.fileupload.FileUpload, which are different classes / 
interfaces with similar API. Since they wouldn't match the tomahawk 
fileupload type, the setter wouldn't be found by EL, and wouldn't be called.

Another possibility, you are uploading file greater than the limit you 
set in your web.xml





le3000 a écrit :
> I follow the example in "Upload Files with JSF and MyFaces" from ONJava.com
> to do the file upload. I am having problem on UploadedFile null pointer. 
> The UploadedFile always return null. actually, the setter-setMyFile never
> even get called.
>
> Here is what I have in my jsp:
> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
> <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> 
>
> <f:subview id="sendJobsub">
> 	  <h:form id="sendJob" enctype="multipart/form-data">	  	                     
>           <h:messages globalOnly="true" styleClass="message"/>
>     	  <h:panelGrid columns="3" border="0" cellspacing="5">
>               <h:outputLabel for="myFileId" value="File: "/>
>               <t:inputFileUpload id="myFileId" value="#{SendJobBean.myFile}"
> storage="file" required="true"/>
>         
>               <h:outputLabel for="myParamId" value="Param: "/>
>               <h:selectOneMenu id="myParamId" value="#{SendJobBean.myParam}" 
> required="true">
>                   <f:selectItem itemLabel="" itemValue=""/>
>                   <f:selectItem itemLabel="MD5" itemValue="MD5"/>
>                   <f:selectItem itemLabel="SHA-1" itemValue="SHA-1"/>
>                   <f:selectItem itemLabel="SHA-256" itemValue="SHA-256"/>
>                   <f:selectItem itemLabel="SHA-384" itemValue="SHA-384"/>
>                   <f:selectItem itemLabel="SHA-512" itemValue="SHA-512"/>
>               </h:selectOneMenu>
>               <h:message for="myParamId"/>
>
>               <h:outputText value=" "/>
>               <h:commandButton value="Upload XML File"
> action="#{SendJobBean.processXMLFile}"/>
>               <h:outputText value=" "/>       
>           </h:panelGrid>          
>     </h:form>    
> </f:subview>
>
> Here is my web.xml:
>    <filter>
>         <filter-name>ExtensionsFilter</filter-name>
>        
> <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
>         <init-param>                
>                 <param-name>uploadMaxFileSize</param-name>
>                 <param-value>100m</param-value>
>         </init-param>
>         <init-param>                
>                 <param-name>uploadThresholdSize</param-name>
>                 <param-value>100k</param-value>
>         </init-param>  
>     </filter>
>     <filter-mapping>
> 		<filter-name>ExtensionsFilter</filter-name>
> 		<servlet-name>Faces Servlet</servlet-name>
> 	</filter-mapping>
> 	<filter-mapping>
> 		<filter-name>ExtensionsFilter</filter-name>
> 		<url-pattern>/*</url-pattern>
> 	</filter-mapping>
>
> My bean is very simple:
> .......
> private UploadedFile myFile;
> .......
> public void setMyFile(UploadedFile myFile){
>         System.out.println("setting uploaded file");
>         this.myFile=myFile;
>     }
>
>     public UploadedFile getMyFile(){
>         System.out.println("getting uploaded file");
>         return this.myFile;
>     }
>     public void processXMLFile() throws Exception {
>         String myResult="";
>         try {
>             MessageDigest md  = MessageDigest.getInstance(myParam);            
>             InputStream in = new
> BufferedInputStream(myFile.getInputStream());
>             try {
>                 byte[] buffer = new byte[64 * 1024];
>                 int count;
>                 while ((count = in.read(buffer)) > 0)
>                     md.update(buffer, 0, count);
>             } finally {
>                 in.close();
>             }
>             byte hash[] = md.digest();
>             StringBuffer buf = new StringBuffer();
>             for (int i = 0; i < hash.length; i++) {
>                 int b = hash[i] & 0xFF;
>                 int c = (b >> 4) & 0xF;
>                 c = c < 10 ? '0' + c : 'A' + c - 10;
>                 buf.append((char) c);
>                 c = b & 0xF;
>                 c = c < 10 ? '0' + c : 'A' + c - 10;
>                 buf.append((char) c);
>             }
>             myResult = buf.toString();
>             System.out.println(myResult);
>         } catch (Exception x) {
>             x.printStackTrace();
>             FacesMessage message = new FacesMessage(
>                 FacesMessage.SEVERITY_FATAL,
>                 x.getClass().getName(), x.getMessage());
>             FacesContext.getCurrentInstance().addMessage( null, message);            
>         }
>     }
>
> It always always catch null pointer on UploadedFile, and the
> UploadedFile(myFile) getter being call when I load that page, but the setter
> never being called.
>
> Please help. I've been struggling on this for days. :-(
>