使用 Tomahawk t:inputFileUpload 时未调用 CommandButton 操作

发布于 2024-12-12 00:18:18 字数 2258 浏览 0 评论 0原文

我将 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 的“上传”方法不起作用或者什么的。我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-12-19 00:18:18

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

另请参阅:


与具体问题无关,您确实应该考虑使用 Facelets 而不是 JSP。它比 JSP 提供了很多优势,例如 模板化复合组件ajax 支持

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文