简体   繁体   中英

CommandButton action not invoked when using Tomahawk t:inputFileUpload

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM