Skip to content

3) Project Setup and Maven Dependecies:

pranithcodes edited this page Apr 6, 2018 · 1 revision

In this session we will write a simple JEXl program with maven.


Step 1: Create a maven project with “maven-archetype-quickstart” as archetype.

Step 2: Add the “commons-jexl3” dependency to the project.

   `<dependency>`
	`<groupId>org.apache.commons</groupId>`
	`<artifactId>commons-jexl3</artifactId>`
	`<version>3.0</version>`
`</dependency>`

Step 3: Create a new class “JexlDemoSystemOut” in the pacakge “com.mine.learning.dvipa” and add the following code.

public class JexlDemoSystemOut {

`public static void main(String[] args) {`
	`JexlEngine jexl = new JexlBuilder().create();`

	`JexlContext context = new MapContext();`

	`context.set("out", System.out);`

	`JexlExpression e = jexl`
			`.createExpression("out.println(\"Printing Message\")");`
	`e.evaluate(context);`

`}`

}

In order to create and evalute JexlExpression, we need JexlEngine. JexlEngine can be configured and created using JexlBuilder. Default instance of JexlEnigne can be created simply using create() method of JexlBuilder.

JexlContent holds the data and objects as a Map. JexlContext has to passed as a parameter to JEXLExpression. All the data and object, that JEXLEngine needed to evalute and execute the JexlExpression should be set to JexlContext.

In the above example, “System.out” object is as “out” variable. So, when JexlEnigne evalution the expression (out.println("Printing Message")), it uses the println function of the System’s PrintStream.

Step 4: Output: Printing Message

Clone this wiki locally