The @Named annotation is used when you want to specify the way something is named on the
user interface i.e. when you do not want to use the name generated automatically by the system.
It can be applied to objects, members (properties, collections, and actions) and to parameters
within an action method.
Specifying the name of an object
By default the name of an object is derived, reflectively from the class name. To specify a
different name for an object, use the @Named annotation in front of the class declaration. For
example:
@Named("Customer")
public class CustomerImpl implements Customer{
...
}
See also: @Plural.
Specifying the name of a member
By default, the name of a member (a property, collection or action) presented to the user is
derived, reflectively, from the name of the member defined in the program code. to specify a
different name use the @Named annotation immediately before the member declaration. For
example:
public class Customer {
@Named("Given Name")
public String getFirstName() { ... }
@Named("Family Name")
public String getSurname() { ... }
public CreditRating getCreditRating() { ... }
}
Specifying the name for an action parameter
The most common usage of @Named will be to specify names for the parameters of an action:
because, by default, the user interface will use the type of the parameter as the name. (This is
Reference 95
Draft (reference/recognised-annotations.xml) Recognised Annotations
because the parameter name declared in the code for the action method cannot be picked up
reflectively.) To specify the name of a parameter, the @Named annotation is applied 'in-line' (i.e.
preceding the individual parameter declaration). For example:
public class Customer {
public Order placeOrder(
Product product,
@Named("Quantity")
int quantity) {
Order order = new Order();
order.modifyCustomer(this);
order.modifyProduct(product);
order.setQuantity(quantity);
return order;
}
...
}