-
Notifications
You must be signed in to change notification settings - Fork 6
Bridje EL
The Expression Language (EL) is a programming language with special natures, utilized generally in web applications, allowing us to insert expressions in our web pages. Java Unified Expression Language (JUEL) is an implementation of the EL, JUEL is ready for use in non-JSP applications. We can create and evaluating complex expressions with JUEL, expressions that determined moments speed up our implementations for us, Bridje framework makes use of JUEL for this purpose. If you wish to deepen more about JUEL , you can visit the following Web: http://juel.sourceforge.net/.
Now we will focus our attention in Bridje-EL API. Bridje-EL API makes use of the JUEL API, we utilized it at work with EL. The same can be used with maven from central repository:
<dependencies>
....
<dependency>
<groupId>org.bridje</groupId>
<artifactId>bridje-el</artifactId>
<version>${bridje.version}</version>
</dependency>
....
</dependencies>
ElService elServ = Ioc.context().find(ElService.class); //Acceding to ELService class.
ElEnvironment elEnv = elServ.createElEnvironment(Ioc.context()); //Creates a new expression language environment. The new created EL environment will take the values from the components of this context.
ElService elServ = Ioc.context().find(ElService.class); //Acceding to ELService class.
ElEnvironment elEnv = elServ.createElEnvironment(Ioc.context()); //Creates a new expression language environment. The new created EL environment will take the values from the components of this context.
elEnv.setVar("myVar", "Hello"); //Sets the value of the specified variable, myVar is a String variable.
String result = elEnv.get("${myVar}", String.class); //Evaluates the given expression and cast the result of it to the given result class.
//the variable result is equal to "Hello".
elEnv.setVar("myList", new ArrayList<>()); //Sets the value of the specified variable, myList is a empty list.
List lst = elEnv.get("${myList}", List.class); //lst is a empty list.
Let's suppose that we have the following component:
@Component
@ElModel("myModel") //This annotation defines a model that has visibility in the expression language context.
public class MyModel
{
public String getName()
{
return "Some Name";
}
public List getList()
{
List arr = new ArrayList();
arr.add(new Object());
return arr;
}
}
Definition of the ELModel annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ElModel
{
/**
* The name for this model.
*
* @return The name for this model.
*/
String value();
}
Now we will create a expression language with MyModel component.
ElService elServ = Ioc.context().find(ElService.class);
ElEnvironment elEnv = elServ.createElEnvironment(Ioc.context());
String result = elEnv.get("${MyModel.name}", String.class); //In this moment is called resolveAllModels method, after is called getName method.
/**We needed to have implemented ModelResolver interface, this interface contain resolveAllModels method,
*this method is called when the ElEnviroment need to resolve the names of the components
*that will participate in the expressions.
*/
List lst = elEnv.get("${myModel.list}", List.class); //is called getList method.
/**
* the result variable is equal to "Some Name".
* the lst variable is List Object.
*/
Implementation of ModelResolver interface:
@Component
public class ModelResolverImpl implements ModelResolver
{
@Override
public void resolveAllModels(IocContext<?> ctx, Map<String, Class<?>> result)
{
ctx.getClassRepository()
.forEachClass(ElModel.class, (c, a) -> result.put(a.value(), c) );
}
}