2016-08-22 65 views
2

我正在尝试使用Java和webfirmframework生成动态HTML。如何使用Webfirmframework将组件添加到现有的HTML中?

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

创建一个生成所有这些10+表的方法是使代码不可读。

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

这是我试过的,它会引发错误。

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

回答

2

一个标签类的第一个参数是它的父类,你的情况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

+2

非常有用和非常明确的答案。谢谢! – user811433

+0

@ user811433欢迎! –

相关问题