1

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.

4

1 に答える 1

2

文字列を html としてレンダリングする場合は、MarkupWriter#writeRaw()メソッドを使用する必要があります。

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

または、OutputRawコンポーネントを使用できます。

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

または、Renderable を使用してマークアップを記述できます。

@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");
  }
};

そしてテンプレート上:

<t:delegate to="checkbox"/>
于 2013-07-11T08:46:53.367 に答える