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



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

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

这个按钮是XHTML文件,我想转换为pdf。Java类代码在这里:

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

这个代码是好的,唯一的问题是如何执行类按钮点击。当我在IDE中运行class时,会得到结果,但问题是XHTML中的内容是动态的,不检索值。

如果我在值将被填充时执行class,这将给我期望的结果。

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

我没有足够的观点来评论。

尝试在<h:form>标签内包装您的commandButton,并添加type="submit"按钮

最新更新