webfirmframework for Java Experts

Methods of Tags

These are some of the basic tag building methods but not limited to that.

appendChild

Div div = new Div(null);        
Span span = new Span(null);

div.appendChild(span);

String htmlString = div.toHtmlString();
//prints 
//<div><span></span></div>
System.out.println(htmlString);

appendChildren

Div div = new Div(null);
Span span = new Span(null);
H1 h1 = new H1(null);

div.appendChildren(span, h1);

String htmlString = div.toHtmlString();
//prints 
//<div><span></span><h1></h1></div>
System.out.println(htmlString);

addInnerHtml

This method will remove all children and add the given tag as child.

Div div = new Div(null);         
Span span = new Span(null);
div.appendChild(span);

div.addInnerHtml(new NoTag(null, "Some Text"));

String htmlString = div.toHtmlString();
//prints 
//<div>Some Text</div>
System.out.println(htmlString);

addInnerHtmls

This method will remove all children and add the given tags as children.

Div div = new Div(null); 
				       
P p = new P(null);
div.appendChild(p);

H1 h1 = new H1(null);
h1.addInnerHtml(new NoTag(null, "h1 heading"));

H2 h2 = new H2(null);
h2.addInnerHtml(new NoTag(null, "h2 subheading"));


div.addInnerHtmls(h1, h2);

String htmlString = div.toHtmlString();
//prints 
//<div><h1>h1 heading</h1><h2>h2 subheading</h2></div>
System.out.println(htmlString);

removeChild

This method will remove the given child from the tag.

Div div = new Div(null);
P p = new P(null);
div.appendChild(p);

div.removeChild(p);

String htmlString = div.toHtmlString();
//prints 
//<div></div>
System.out.println(htmlString);

removeChildren

This method will remove the given children from the tag.

Div div = new Div(null);
Span span = new Span(null);
P p = new P(null);

div.appendChild(span);
div.appendChild(p);

div.removeChildren(Arrays.asList(p, span));

String htmlString = div.toHtmlString();
//prints 
//<div></div>
System.out.println(htmlString);

removeAllChildren

This method will remove the all children from the tag.

Div div = new Div(null);
Span span = new Span(null);
P p = new P(null);

div.appendChild(span);
div.appendChild(p);

div.removeAllChildren();

String htmlString = div.toHtmlString();
//prints 
//<div></div>
System.out.println(htmlString);

getChildren

This method will return all children from the tag.

Div div = new Div(null);
Span span = new Span(null);
P p = new P(null);

div.appendChild(span);
div.appendChild(p);

final List children = div.getChildren();

//true
System.out.println(children.get(0).equals(span));
//true
System.out.println(children.get(1).equals(p));

getFirstChild since 3.0.1

This method will return the first child from its children.

Div div = new Div(null);
Span span = new Span(null);
P p = new P(null);

div.appendChild(span);
div.appendChild(p);

final AbstractHtml firstChild = div.getFirstChild();

//true
System.out.println(firstChild.equals(span));

getChildAt since 3.0.1

This method will return the child at the given index in its children. The below example will get the child at first index.

Div div = new Div(null);
Span span = new Span(null);
P p = new P(null);

div.appendChild(span);
div.appendChild(p);

final AbstractHtml childAtIndexOne = div.getChildAt(1);

//true
System.out.println(childAtIndexOne.equals(p));

prependChildren since 3.0.1

This method will return the child at the given index in its children. The below example will get the child at first index.

Div div = new Div(null, new Id("one")) {{                                   
              new Div(this, new Id("child1"));
          }};                                      
                                        
Span span = new Span(null);             
P p = new P(null);                      
Br br = new Br(null);                   
div.prependChildren(span, p, br);       
                                        
System.out.println(div.toHtmlString()); 
This will print:
<div id="one">                   
    <span></span>                    
    <p></p>                          
    <br/>                                  
    <div id="child1"></div>
</div>

addInnerHtml​(SharedTagContent<T>) & subscribeTo​​(SharedTagContent<T>) since 3.0.6

Same SharedTagContent object may be passed to multiple tags to change content from a single point. Read more about its usage from its javadoc.

replaceWith since 3.0.7 & bug free since 3.0.14

This method will replace the current tag with the given tags. Obviously, the current tag must have a parent. This sample coding video contains its usage.

give since 3.0.7

This method may be used to write functional style coding instead of anonymous style coding.

Eg:-

Html rootTag = new Html(null).give(html -> {
    new Head(html);
    new Body(html).give(body -> {           	 
        new NoTag(body, "Hello World");           	 
    });
});
// prepends the doc type 
rootTag.setPrependDocType(true);
System.out.println(rootTag.toHtmlString(true));
				

getIndexByChild since 3.0.7

To find the index of given tag if it is a child tag of this tag.

getLastChild since 3.0.15

This method will return the last child from its children.

The methods listed here are not only the methods available in AbstractHtml class, to know complete list of methods please refer its API javadoc.

There some methods in tags which have some special purpose in the development.

toBigHtmlString & toBigOutputStream

The toHtmlString and toBigHtmlString both can be used to return HTML string for the corresponding tag. If the tag hierarchy is extremely complex (i.e. having around 10000 nested tags in it) then toBigHtmlString is the better choice because toHtmlString will consume memory while generating HTML and if there is no enough memory available then it may throw an error. toHtmlString is faster than toBigHtmlString so it's always preferred for normal purpose. There could be some cases where the tag hierarchy is used to generate very complex HTML report, in such case toBigHtmlString is better.

toBigOutputStream is for the same purpose the only difference is instead of returning HTML string it writes HTML bytes directly to the given output stream.

setData(Object) & getData()

It's simply to set and get any object on the tag. It the set object is saved in the tag itself.

setSharedData(Object) & getSharedData()

It's simply to set and get any object common to the whole tag hierarchy. The given object is saved in a private area of the root tag of the whole hierarchy. Eg:-

getRootTag()

getRootTag() method of a tag returns the root tag (i.e. top level base tag) in the whole tag hierarchy. Eg:-

resetHierarchy()

If a tag is used under a BrowserPage instance and the same tag object needs to be used in another instance of BrowserPage then the tag must be removed from its parent and its resetHierarchy method must be called before using in another instance of BrowserPage otherwise it will make an unusual behavour in the UI. To avoid compromising performance resetHierarchy doesn't handle it internally.