Skip to main content

juneau-bean-html5

The Juneau HTML5 DTO beans are simply beans with fluent-style setters that allow you to quickly construct HTML fragments as Java objects. These object can then be serialized to HTML using one of the existing HTML serializers, or to other languages such as JSON using the JSON serializers. The HtmlBuilder class is a utility class with predefined static methods that allow you to easily construct DTO bean instances in a minimal amount of code. The following examples show how to create common HTML DOM objects.

Java codeHTML
import static org.apache.juneau.bean.html5.HtmlBuilder.*;

Object mytable = table(
tr(
th("c1"), th("c2")
),
tr(
td("v1"), td("v2")
)
);

String html = Html.of(mytable);
<table>
<tr>
<th>c1</th>
<th>c2</th>
</tr>
<tr>
<td>v1</td>
<td>v2</td>
</tr>
</table>
import static org.apache.juneau.bean.html5.HtmlBuilder.*;

Object mydiv = div().align("center").onmouseover("alert(\"boo!\");")
.children(
p("Juneau supports ", b(i("mixed")), " content!")
);

String html = Html.of(mydiv);
<div align='center' onmouseover='alert("boo!");'>
<p>Juneau supports <b><i>mixed</i></b> content!</p>
</div>
import static org.apache.juneau.bean.html5.HtmlBuilder.*;

Object myform = form().action("/submit").method("POST")
.children(
"Position (1-10000): ",
input("number").name("pos").value(1),
br(),
"Limit (1-10000): ",
input("number").name("limit").value(100),
br(),
button("submit", "Submit"),
button("reset", "Reset")
);

String html = Html.of(myform);
<form action='/submit' method='POST'>
Position (1-10000):
<input name='pos' type='number' value='1'/>
<br/>
Limit (1-10000):
<input name='limit' type='number' value='100'/>
<br/>
<button type='submit'>Submit</button>
<button type='reset'>Reset</button>
</form>

Using the HTML5 DTO beans, you should be able to construct any valid HTML5 from full document bodies to any possible fragments.

The HtmlParser class can be used convert these HTML documents back into POJOs.

Other serializers and parsers (e.g. JsonSerializer) can be used to represent these POJOs in languages other than HTML.