在 JSF 按钮单击时执行 java 方法

Execute java method on JSF button click

当我点击按钮时没有任何反应。

 <h:commandButton value="Generate PDF" type="button"
        action="#{parseHtml12.createPdf}" />

此按钮位于我要转换为 pdf 的 XHTML 文件中。 Java class 代码在这里:

public class ParseHtml12 {
    public static final String DEST = "C:\Users\User\Desktop/report.pdf";
    public static final String HTML = "web/data.xhtml";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ParseHtml12().createPdf(DEST);
    }


    public void createPdf(String file) throws IOException, DocumentException {

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12);

        document.open();

        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));

        document.close();
    }
}

这段代码很好,唯一的问题是如何在单击按钮时执行 class。 当我在 IDE 中 运行 class 给我结果时,但问题是 XHTML 中的内容是动态的并且不检索值。

如果我在填充值时执行 class,这将得到我想要的结果。

更新:单击按钮时动态数据消失。如果我再次点击会发生这样的事情: javax.el.PropertyNotFoundException: /data.xhtml @48,45 action="#{parseHtml12.createPdf}": Target Unreachable, identifier 'parseHtml12' resolved to null

我没有足够的积分来发表评论。

尝试将命令按钮包裹在 <h:form> 标签中,然后将 type="submit" 添加到按钮。