@Html Annotation
The @Html annotation can be used to customize how POJOs are serialized to HTML on a per-class/field/method basis.
The @Html(link) annotation adds a hyperlink to a bean property when rendered as HTML.
public class FileSpace {
// Add a hyperlink to this bean property.
@Html(link="servlet:/drive/{drive}")
public String getDrive() {...}
}
The @Html(anchorText) annotation is used to specify the anchor text of a hyperlink.
// Produces CLICK ME! when serialized to HTML.
public class FileSpace {
// Add a hyperlink to this bean property.
@Html(link="servlet:/drive/{drive}", anchorText="CLICK ME!")
public String getDrive() {...}
}
The @Html(format) annotation is used to specify what
format to use for HTML elements.
For example, the HTML beans defined in the html5 package use
format=XML so that the beans get serialized as standard XML:
// Parent class of all HTML DTO bean classes.
@Html(format=XML)
public abstract class HtmlElement {...}
The @Html(noTableHeaders) annotation is used to prevent beans from being serialized with table headers.
The @Html(noTables) annotation is used to force beans to be serialized as trees instead of tables
@Html(render) annotation
The @Html(render) annotation allows for custom rendering of bean property values when serialized as HTML. Using this class, you can alter the CSS style and HTML content of the bean property.
The following example shows two render classes that customize the appearance of the pctFull and status columns in
the code below:
import static org.apache.juneau.bean.html5.HtmlBuilder.*;
// Our bean class
public class FileSpace {
private final String drive;
private final long total, available;
public FileSpace(String drive, long total, long available) {
this.drive = drive;
this.total = total;
this.available = available;
}
@Html(link="drive/{drive}")
public String getDrive() {
return drive;
}
public long getTotal() {
return total;
}
public long getAvailable() {
return available;
}
@Html(render=FileSpacePctRender.class)
public float getPctFull() {
return ((100 * available) / total);
}
@Html(render=FileSpaceStatusRender.class)
public FileSpaceStatus getStatus() {
float pf = getPctFull();
if (pf < 80) return FileSpaceStatus.OK;
if (pf < 90) return FileSpaceStatus.WARNING;
return FileSpaceStatus.SEVERE;
}
}
// Possible values for the getStatus() method
public enum FileSpaceStatus {
OK, WARNING, SEVERE;
}
// Custom render for getPctFull() method
public class FileSpacePctRender extends HtmlRender<Float> {
@Override
public String getStyle(SerializerSession session, Float value) {
if (value >= 90) return "background-color:red;color:white;";
if (value >= 80) return "background-color:yellow;";
return null;
}
@Override
public Object getContent(SerializerSession session, Float value) {
if (value >= 90)
return div(
String.format("%.0f%%", value),
style("@keyframes color_change { from { background-color: red; } to { background-color: yellow; }")
);
return String.format("%.0f%%", value);
}
}
// Custom render for getStatus() method
public class FileSpaceStatusRender extends HtmlRender<FileSpaceStatus> {
@Override
public String getStyle(SerializerSession session, FileSpaceStatus value) {
return "text-align:center";
}
@Override
public Object getContent(SerializerSession session, FileSpaceStatus value) {
switch (value) {
case OK: return img().src(URI.create("servlet:/htdocs/ok.png"));
case WARNING: return img().src(URI.create("servlet:/htdocs/warning.png"));
default: return img().src(URI.create("servlet:/htdocs/severe.png"));
}
}
}