WFF

wffweb - Java framework to develop web applications

View sample projects on GitHub
Use latest version of wffweb to get better stability, performance and thread-safety.

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));

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.

Next >>



Subscribe on youtube for technical videos