Java 如何使用Webfirmframework向现有HTML添加组件?

Java 如何使用Webfirmframework向现有HTML添加组件?,java,html,wffweb,Java,Html,Wffweb,我正在尝试使用Java和webfirmframework生成动态HTML 有没有一种方法可以构建单独的组件,比如Table和Div,而不将它们封装在Html()中? 我有超过10个业务逻辑,每个都生成一个单独的数据表。所有这10+个表格都必须显示在HTML中 创建一个方法来生成所有这10多个表会使代码变得不可读 需求是能够构建单个组件,然后将它们连接在一起以生成最终的HTML页面 这就是我尝试过的,它会抛出错误 构造函数表(MyServiceClass、CustomAttribute、Custo

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

有没有一种方法可以构建单独的组件,比如Table和Div,而不将它们封装在Html()中? 我有超过10个业务逻辑,每个都生成一个单独的数据表。所有这10+个表格都必须显示在HTML中

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

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

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

构造函数表(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
标记没有有效的父类,因此如果要生成没有外部html标记的表,必须传递
null
而不是
参数

按如下方式修改代码

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>

XXXX
表2
已更新


因为,您可以使用
appendChild
方法来。

@user811433欢迎!