Expr (attribute of property)
From JRapid
expr is an attribute of property.
Contents |
Description
The expr attribute for a property is an EL expression that will be executed every time the value of this property is accessed. This means the getter for this property evaluates the expression.
Use the this keyword to refer to the current object or entity. Read the EL documentation to learn more about available methods to use in this type of expressions.
Usage
<entity ...>
<property
name = NAME
label = LABEL
...
expr = EL_EXPR
/>
</entity>
Example
This example shows hot to build the value for a transient property based on the concatenation of the values of the other properties. Note that the concat function provided by the EL implementation does only support two parameters and that's why we need to write the expression in this way. You need to use "f:" before every function call.
<entity label="Company" name="Company">
<property display="primary" label="Name" name="name"/>
</entity>
<entity label="Employee" name="Employee">
<property display="primary" label="First Name" name="firstName"/>
<property display="primary" label="Last Name" name="lastName"/>
<property display="secondary" entity="Company" label="Company" name="company"/>
<property expr="f:concat(f:concat(this.firstName, f:concat(' ', this.lastName)), f:concat(' ', this.company.name))" label="Full Name" name="title"/>
</entity>
If you look at the code generated in the EmployeeAbstract class you'll see that the getter for the title property is:
public String getTitle() {
java.util.Map<String, Object> map = new java.util.HashMap<String, Object>();
map.put("this", this);
ExpressionVariableResolver evr = new ExpressionVariableResolver(map);
FunctionMapper fm = new FunctionMapper();
ExpressionEvaluatorImpl el = new ExpressionEvaluatorImpl();
try {
title = (String) el.evaluate("${f:concat(f:concat(this.firstName, f:concat(' ', this.lastName)), f:concat(' ', this.company.name))}", Object.class, evr, fm);
} catch (ELException e) {
}
return title;
}

