Question

I need to write HTML from my .java page. Here is what I have tried

This is my tml code fragment

${testFunction()}

This is my java code fragment

public String testFunction()
{
   return "<input type='checkbox' name='leaf' id='leaf' value='leaf'/>"
}

The result I want is a checkbox. What I get is a string "input type='checkbox' name='leaf' id='leaf' value='leaf'".
Any help would be appreciated Thanks.

Was it helpful?

Solution

If you want to render string as html you need to use MarkupWriter#writeRaw() method:

void beginRender(MarkupWriter writer) {
  writer.writeRaw("<input type='checkbox' name='leaf' id='leaf' value='leaf'/>");
}

Or you can use OutputRaw component:

<t:outputraw value="testFunction()"/>

Or you can use Renderable to write markup:

@Property(write = false)
private final Renderable checkbox = new Renderable() {
  public void render(MarkupWriter writer) {
    writer.element("input",
        "type", "checkbox",
        "id", "leaf",
        "name", "leaf",
        "value", "leaf");
    writer.end();

    // if you need checked attribute
    // writer.getElement().attribute("checked", "checked");
  }
};

And on template:

<t:delegate to="checkbox"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top