1

I'm using JSF 2.1 with JSP files. I tried to use JSF 2.1 with Facelet files, but didn't get Tomahawk (MyFaces extension) to work.

I'm trying to upload a file using Tomahawk. My form looks like follows:

<f:view>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>FILE UPLOAD</title>
    </head>
    <body>
        <fieldset>
            <h:form enctype="multipart/form-data">
                <t:inputFileUpload value="#{fileUploadBean.upFile}"
                                   size="25"
                                   required="true" /><br/>
                <h:commandButton value="Validar" action="#{fileUploadBean.upload}" />
            </h:form>
        </fieldset>
    </body>
</html>
</f:view>

and my bean is (as I'm using JSF 2.1, I don't need to use faces-config.xml and I use annotations):

import java.io.IOException;
import java.io.InputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.myfaces.custom.fileupload.UploadedFile;

/**
 *
 * @author federico.martinez
 */
@ManagedBean
@RequestScoped
public class FileUploadBean {

    private UploadedFile upFile;

    public UploadedFile getUpFile(){
        return upFile;
    }

    public void setUpFile(UploadedFile upFile){
        this.upFile = upFile;
    }

    public String upload(){
        InputStream is = null;
        try {
            is = upFile.getInputStream();
            long size = upFile.getSize();
            byte[] buffer = new byte[(int)size];
            is.read(buffer,0,(int)size);
            is.close();
            System.out.println("File Upload Successful.");
            return "processing-file";
        } catch (IOException ex) {
            Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
            return "error-lf";
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }  
}

Well that's it, but finally the problem is when I click on h:commandButton, it doesn't do anything. It seems like the method "upload" from my bean isn't working or something. what am I missing?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
BRabbit27
  • 6,333
  • 17
  • 90
  • 161

1 Answers1

0

As stated in the Tomahawk documentation, you need to register ExtensionsFilter in web.xml. It's the one responsible for parsing multipart/form-data requests and converting the multipart form data parts to normal request parameters and attributes, so that the FacesServlet can continue using them. If you don't register the ExtensionsFilter, then the FacesServlet can't do anything because there are no request parameters in order to determine the model values to be updated and actions to be invoked.

See also:


Unrelated to the concrete problem, you should really consider using Facelets instead of JSP. It offers so many advantages over JSP, such as templating, composite components and ajax support.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, I'll try this and post the results. I don't want to mix questions, but I didn't find a way to use tomahawk using Facelets in Netbeans. If anyone can help me with that... – BRabbit27 Oct 27 '11 at 15:19
  • You're welcome. The "See also" link shows in detail how to do that. – BalusC Oct 27 '11 at 15:36
  • Oh My God! This post inspired me haha! Finally everything's working with Facelets! – BRabbit27 Oct 27 '11 at 16:21