Home  >  Spring Core

#this and #root in Spring Expression Language (SpEL)

By Arvind Rai, October 06, 2013
In this page, we will learn how to use #this and #root variables in Spring Expression Language. #this refers the current evaluation and #root is defined by us. In our example we have taken a list of even integer and this will be our root. We will parse all even greater than four. This is done by StandardEvaluationContext.

SpELTest
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		List<Integer> even = new ArrayList<Integer>();
		even.add(2);
		even.add(4);
		even.add(6);
		even.add(8);
		even.add(10);
		
		StandardEvaluationContext seContext = new StandardEvaluationContext();
		seContext.setVariable("even",even);
		
		List<Integer> evenGtfour = 
	    (List<Integer>) parser.parseExpression("#even.?[#this>4]").getValue(seContext);
		
		for(Integer i: evenGtfour){
			System.out.println(i);
		}
	}
}
 
Output

6
8
10
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us