h:不调用commandButton动作方法



我有下面的按钮:

<h:commandButton value="Download" action="#{listFiles.downloadFile}" />

和下面的动作方法:

public void downloadFile()
    {
        // Some code.
    }

但是当我按下按钮时什么也没有发生。操作方法没有被调用。

在服务器日志中我看到:

表单组件需要在它的祖先中有一个UIForm。建议:将需要的组件封装在<h:form>

这是我的工作

我的文件在文件夹路径

WEB-INFpagesforms

cod in xhtml file

<h:commandLink value="Download" action="#{formBean.downloadFile('FileName.doc')}" styleClass="link"/>
豆子

public String downloadFile(String fileName) {
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpServletResponse response = ((HttpServletResponse) facesContext.getExternalContext().getResponse());
            String reportPath = facesContext.getExternalContext().getRealPath("\pages\forms") + File.separator + fileName;
            response.setHeader("Content-Disposition", "attachment; filename="" + fileName+ """);
            File file = new File(reportPath);
            BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(file));
            int rLength = -1;
            byte[] buffer = new byte[1000];
            while ((rLength = bIn.read(buffer, 0, 100)) != -1) {
                response.getOutputStream().write(buffer, 0, rLength);
            }
            FacesContext.getCurrentInstance().responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

最新更新