We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

The following steps can be used to  add HTML code dynamically, from a Java file in Wicket

1. First store the required html code as a String variable.
2. Create a Label instance by passing the above String in the constructor of the Label.
3. Set "EscapeModelStrings" false for the Label.
    label.setEscapeModelStrings(false);
4. Add a Label to Form or Panel or any Container in wicket.
5. Now go to the Html file where you want to add this dynamic html code and write the markup for this Label under <span> tag.


Code Example:-
In this example we add a dynamic <div> tag in markup from our java file "AddHtmlPanel". Here AddHtmlPanel extends Panel, so it also becomes a panel and we can add directly the Label here.

 

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


Related Tags:

Java, Wicket Framework, HTML, Javascript

Author: Pranav Kumar

top

Java

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login