tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Expression Language > Parser Context

Parser Context 

The Spring Expression Language (SpEL) is a simple and powerful expression language which helps to query and manipulate objects at runtime. Expression templates allows us to define evaluation blocks. Each evaluation block is delimited by prefix and suffix. So we can mix literal text with one or more evaluation blocks. The most preferred choice is #{ } which has been implemented by ParserContext.TEMPLATE_EXPRESSION. However we can define our own prefix and suffix by implementing ParserContext interface.

File Name  :  
com/bethecoder/tutorials/spring3/tests/spel/ParserContextTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.spel;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class ParserContextTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    StandardEvaluationContext stdContext = new StandardEvaluationContext();
    ExpressionParser parser = new SpelExpressionParser();

    String value = parser.parseExpression(
        "Int MIN_VALUE :  #{ T(Integer).MIN_VALUE }"
        ParserContext.TEMPLATE_EXPRESSION).getValue(stdContext, String.class);
    System.out.println(value);
    
    
    value = parser.parseExpression(
        "Int MAX_VALUE :  %{ T(Integer).MAX_VALUE }"
        new TemplateParserContext()).getValue(stdContext, String.class);
    System.out.println(value);
    
  }

}

class TemplateParserContext implements ParserContext {

  public String getExpressionPrefix() {
    return "%{";
  }

  public String getExpressionSuffix() {
    return "}";
  }

  public boolean isTemplate() {
    return true;
  }
}
   

It gives the following output,
Int MIN_VALUE :  -2147483648
Int MAX_VALUE :  2147483647



 
  


  
bl  br