@Retention(value=SOURCE) @Target(value=TYPE) public @interface Model
className(). The generated class contains
getters and setters for properties defined via properties() and
getters for other, derived properties defined by annotating methods
of this class by ComputedProperty. Each property
can be of primitive type, an enum type or (in order to create
nested JSON structure)
of another class generated by @Model annotation. Each property
can either represent a single value or be an array of its values.
The generated class's toString method
converts the state of the object into
JSON format. One can
use Models.parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream)
method to read the JSON text stored in a file or other stream back into the Java model.
One can also use @OnReceive annotation to read the model
asynchronously from a URL.
An example where one defines class Person with four
properties (firstName, lastName, array of addresses and
fullName) follows:
The generated model class has a default constructor, and also quick instantiation constructor that accepts all non-array properties (in the order used in the@Model(className="Person", properties={@Property(name = "firstName", type=String.class),@Property(name = "lastName", type=String.class)@Property(name = "addresses", type=Address.class, array = true) }) static class PersonModel {@ComputedPropertystatic String fullName(String firstName, String lastName) { return firstName + " " + lastName; }@ComputedPropertystatic String mainAddress(List<Address>addresses) { for (Address a : addresses) { return a.getStreet() + " " + a.getTown(); } return "No address"; }@Model(className="Address", properties={@Property(name = "street", type=String.class),@Property(name = "town", type=String.class) }) static class AddressModel { } }
properties() attribute) and vararg list
for the first array property (if any). One can thus use following code
to create an instance of the Person and Address classes:
Person p = new Person("Jaroslav", "Tulach",
new Address("Markoušovice", "Úpice"),
new Address("V Parku", "Praha")
);
// p.toString() then returns equivalent of following JSON object
{
"firstName" : "Jaroslav",
"lastName" : "Tulach",
"addresses" : [
{ "street" : "Markoušovice", "town" : "Úpice" },
{ "street" : "V Parku", "town" : "Praha" },
]
}
In case you are using Knockout technology
for Java then you can associate such model object with surrounding HTML page by
calling: p.applyBindings();. The page can then use regular
Knockout bindings to reference your
model and create dynamic connection between your model in Java and
live DOM structure in the browser:
Name: <span data-bind="text: fullName"> <div data-bind="foreach: addresses"> Lives in <span data-bind="text: town"/> </div>
Visit an on-line demo
to see a histogram driven by the Model annotation or try
a little math test.
| Modifier and Type | Required Element and Description |
|---|---|
String |
className
Name of the model class.
|
Property[] |
properties
List of properties in the model.
|
Copyright © 2014 NetBeans. All Rights Reserved.