Write HTML file using Java

前端 未结 10 902
我寻月下人不归
我寻月下人不归 2020-11-28 23:55

I want my Java application to write HTML code in a file. Right now, I am hard coding HTML tags using java.io.BufferedWriter class. For Example:

BufferedWrite         


        
10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 00:24

    I had also problems in finding something simple to satisfy my needs so I decided to write my own library (with MIT license). It's mainly based on composite and builder pattern.

    A basic declarative example is:

    import static com.github.manliogit.javatags.lang.HtmlHelper.*;
    
    html5(attr("lang -> en"),
      head(
        meta(attr("http-equiv -> Content-Type", "content -> text/html; charset=UTF-8")),
        title("title"),
        link(attr("href -> xxx.css", "rel -> stylesheet"))
      )
    ).render();
    

    A fluent example is:

    ul()
      .add(li("item 1"))
      .add(li("item 2"))
      .add(li("item 3"))     
    

    You can check more examples here

    I also created an on line converter to transform every html snippet (from complex bootstrap template to simple single snippet) on the fly (i.e. html -> javatags)

提交回复
热议问题