user811433 user811433 - 2 months ago 6
HTML Question

How to add a component to an existing HTML using Webfirmframework?

I am trying to generate a dynamic HTML using Java and the webfirmframework.

Is there a way to build individual components like Table and Div without enclosing them in Html() ?
I have over 10+ pieces of business logic and each generates an individual table of data. All these 10+ tables must be displayed inside a HTML.

Creating a single method for generating all these 10+ tables is making the code unreadable.

Requirement is to be able to build individual components and then concatenate them together to generate the final HTML page.

This is what I tried and it throws errors.

The constructor Table(MyServiceClass, CustomAttribute, CustomAttribute) is undefined


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

Answer

The first argument of a tag class is its parent class, in your case Table tag has no valid parent so you have to pass null instead of this argument if you want to generate table without outer html tag.

Modify your code as follows

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

But for your actual requirement, here is the sample code

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

    }
}

This will print

<!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>

Updated

Since wffweb 2 version, you can use appendChild method to add child to a tag.

Comments