{title:'@Html(render) Annotation'}

The {@link oaj.html.annotation.Html#render @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.dto.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 < 80) | return "background-color:lightgreen;text-align:center"; | if (value < 90) | return "background-color:yellow;text-align:center"; | return "background-color:red;text-align:center;border:;animation:color_change 0.5s infinite alternate"; | } | | @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")); | } | } | }