webfirmframework for Java Experts

Custom Attributes

As explained in attributes section, custom attribute may also be considered as two, com.webfirmframework.wffweb.tag.html.attributewff.CustomAttribute class can be used to create a new attribute similar to the existing attribures like Id and com.webfirmframework.wffweb.tag.html.attribute.event.CustomEventAttribute which can be used to create a new event attribute similar to existing event attributes like OnClick.


CustomAttribute

Custom attribute with value :

Div div = new Div(null, new CustomAttribute("attribute-with", "value"));
System.out.println(div);
prints
<div attribute-with="value"></div>

Custom attribute without value :

Div div = new Div(null, new CustomAttribute("attribute-without-value"));
System.out.println(div);
prints
<div attribute-without-value></div>

You can also keep your own implementation class by extending CustomAttribute class, see the below samples

To create custom attribute as a separate class

import com.webfirmframework.wffweb.tag.html.attributewff.CustomAttribute;

public class CommonDataAttribute extends CustomAttribute {

    private static final long serialVersionUID = 1L;

    public CommonDataAttribute(String attributeValue) {
        super("common-data", attributeValue);
    }

    // TODO write the feature methods for this attribute

}
public static void main(String[] args) {

    Div div = new Div(null, new CommonDataAttribute("data1")) {
        Div div = new Div(this, new CommonDataAttribute("data2"));
    };

    System.out.println(div.toHtmlString());
}
this will print
<div common-data="data1">
    <div common-data="data2"></div>
</div>

ImmutableCustomAttribute since 12.0.0-beta.5

It is similar to CustomAttribute but it doesn't contain any method to modify its value. It accepts value through the constructor argument.

CustomEventAttribute

Let us create an onclick event attribute which will work similar to the existing OnClick attribute. It will also support all generic arguments (such as jsPreFunctionBody , serverAsyncMethod , jsFilterFunctionBody and jsPostFunctionBody) supported by an event attribute.

Button btn = new Button(null, new CustomEventAttribute("onclick", "alert('js alert');"));

We can also keep it as a separate class by extending CustomEventAttribute