2016-08-22 62 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歡迎! –

相關問題