AddHtmlPanel.java
public class AddHtmlPanel extends Panel {
/*
* Constructor here
*/
public AddHtmlPanel (String id) {
super(id);
//Saved the dynamic html code in a String Variable "dynamicHtml " here by calling the createDynamicHtml() method
String dynamicHtml = createDynamicHtml("divIdName");
/*
* created label here for adding dynamic html code as a string. Constructor of Label contains
* two parameters, first parameter is "wicket:id" by which label is identified in markup.
* Second parameter is the String variable "dynamicHtml " which contains our dynamic html code as a string.
*/
Label divLabel = new Label("divLabel",dynamicHtml );
//Setted EscapeModelStrings False for the label
divLabel.setEscapeModelStrings(false);
//added label to panel here
add(divLabel);
}
/*
* method which returns <div> tag content as a string required to add in html file
*/
public String createDynamicHtml(String div_id) {
//created a instance named "divSB " of StringBuilder here
StringBuilder divSB = new StringBuilder(512);
/*
*appended dynamic html code to divscriptSB instance of StringBuilder
*/
divSB.append("<div id=\"");
divSB.append(div_id);
divSB.append("\"></div>");
//return StringBuilder instance as a String which contains our dynamic html code
return divSB.toString();
}
}
AddHtmlPanel.html
<wicket:panel>
<span wicket:id="divLabel"></span>
</wicket:panel>
This is a very simple but powerful technique for adding markup code to your HTML file from your Java file in Wicket. I found it while looking for a way to implement Google charts in Wicket but it can be used effectively in many situations as you can also dynamically add Javascript code too