java - 如何使用 Webfirmframework 将组件添加到现有 HTML?

标签 java html wffweb

我正在尝试使用 Java 和 webfirmframework 生成动态 HTML。

有没有办法构建像 Table 和 Div 这样的单独组件而不将它们包含在 Html() 中? 我有超过 10 多个业务逻辑,每个都生成一个单独的数据表。所有这 10 多个表格必须显示在 HTML 中。

创建一个方法来生成所有这 10 多个表会使代码变得不可读。

要求是能够构建各个组件,然后将它们连接在一起以生成最终的 HTML 页面。

这是我尝试过的,但它会抛出错误。

构造函数 Table(MyServiceClass, CustomAttribute, CustomAttribute) 未定义

private void generateTable(final MyCSS hcCss) {
        new Table(this,             
            new CustomAttribute("cellspacing", "0"),
            new CustomAttribute("cellpadding", "3")) {{
            new TBody(this) {{
                new Tr(this) {{
                    new Td(this,
                        new Style("padding: 3px")) {{
                        new NoTag(this, "XXXX");
                    }};
                }};
            }};
        }};
    }

最佳答案

标签类的第一个参数是它的父类,在您的情况下,Table标签没有有效的父类,因此您必须传递null而不是this 参数,如果你想生成没有外部 html 标签的表。

修改您的代码如下

private void generateTable(final MyCSS hcCss) {

        Table table = new Table(null,             
            new CustomAttribute("cellspacing", "0"),
            new CustomAttribute("cellpadding", "3")) {{
            new TBody(this) {{
                new Tr(this) {{
                    new Td(this,
                        new Style("padding: 3px")) {{
                        new NoTag(this, "XXXX");
                    }};
                }};
            }};
        }};

        System.out.println(table.toHtmlString());
}

但是根据您的实际需求,这里是示例代码

public class TableComponentMethods {

    public static void embedTable1In(Body body) {

        new Table(body,             
            new CustomAttribute("cellspacing", "0"),
            new CustomAttribute("cellpadding", "3")) {{
            new TBody(this) {{
                new Tr(this) {{
                    new Td(this,
                        new Style("padding: 3px")) {{
                        new NoTag(this, "XXXX");
                    }};
                }};
            }};
        }};

    }

    public static void embedTable2In(Body body) {

        new Table(body,             
            new CustomAttribute("cellspacing", "0"),
            new CustomAttribute("cellpadding", "3")) {{
            new TBody(this) {{
                new Tr(this) {{
                    new Td(this,
                        new Style("padding: 3px")) {{
                        new NoTag(this, "Table 2");
                    }};
                }};
            }};
        }};

    }

}

public class WffWebTest extends Html {

    private Body body;

    public WffWebTest() {
        super(null);
        setPrependDocType(true);
        develop();
    }

    private void develop() {
        body = new Body(this);
    }

    public Body getBody() {
        return body;
    }

    public static void main(String[] args) {
        WffWebTest finalHtml = new WffWebTest();

        // this will add table as a child in Body tag
        TableComponentMethods.embedTable1In(finalHtml.getBody());
        TableComponentMethods.embedTable2In(finalHtml.getBody());

        System.out.println(finalHtml.toHtmlString());

    }
}

这将打印

<!DOCTYPE html>
<html>

<body>
    <table cellspacing="0" cellpadding="3">
        <tbody>
            <tr>
                <td style="padding: 3px;">XXXX</td>
            </tr>
        </tbody>
    </table>
    <table cellspacing="0" cellpadding="3">
        <tbody>
            <tr>
                <td style="padding: 3px;">Table 2</td>
            </tr>
        </tbody>
    </table>
</body>

</html>

已更新

wffweb 2 version ,您可以使用 appendChild 方法到 add child to a tag .

关于java - 如何使用 Webfirmframework 将组件添加到现有 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39088419/

相关文章:

html - 图像部分作为背景,不缩放

javascript - jQuery .css() 附加或覆盖

java - 在自定义线程中创建的对象在主线程上运行

java - 如何两次消费HttpEntity?

java - 嵌套类中私有(private)构造函数的范围

javascript - 使用 Ink 框架固定 header

java - Jackrabbit 更新或合并节点

java - 如何在 wffweb 中正确渲染 HTML 树中的任意文本内容

java - 如何使用 wffweb 创建具有 colspan 属性的 Td?