【问题标题】:CommandButton action not invoked when using Tomahawk t:inputFileUpload使用 Tomahawk t:inputFileUpload 时未调用 CommandButton 操作
【发布时间】:2011-10-27 14:58:43
【问题描述】:

我将 JSF 2.1 与 JSP 文件一起使用。我尝试将 JSF 2.1 与 Facelet 文件一起使用,但没有让 Tomahawk(MyFaces 扩展)工作。

我正在尝试使用 Tomahawk 上传文件。我的表单如下所示:

<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>

我的 bean 是(因为我使用的是 JSF 2.1,所以我不需要使用 faces-config.xml 并且我使用注释):

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);
            }
        }
    }  
}

就是这样,但最后的问题是当我点击 h:commandButton 时,它什么也没做。似乎从我的 bean 中“上传”的方法不起作用或其他什么。我错过了什么?

【问题讨论】:

    标签: jsp jsf file-upload jsf-2 tomahawk


    【解决方案1】:

    如Tomahawk documentation中所述,您需要在web.xml中注册ExtensionsFilter。它负责解析multipart/form-data 请求并将多部分表单数据部分转换为正常的请求参数和属性,以便FacesServlet 可以继续使用它们。如果您不注册ExtensionsFilter,那么FacesServlet 将无法执行任何操作,因为没有请求参数来确定要更新的模型值和要调用的操作。

    另见:


    与具体问题无关,您应该真正考虑使用 Facelets 而不是 JSP。它提供了很多优于 JSP 的优势,例如 templating、composite components 和 ajax support。

    【讨论】:

    • 谢谢,我会试试这个并发布结果。我不想混淆问题,但我没有找到在 Netbeans 中使用 Facelets 使用 tomahawk 的方法。如果有人可以帮助我...
    • 不客气。 “另请参阅”链接详细说明了如何执行此操作。
    • 天哪!这篇文章给了我灵感哈哈!终于,一切都可以使用 Facelets 了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多