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

Ternary Operator 

The Spring Expression Language (SpEL) is a simple and powerful expression language which helps to query and manipulate objects at runtime. The following example shows using ternary operator.

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

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

public class TernaryOperatorTest {

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

    StandardEvaluationContext stdContext = new StandardEvaluationContext();
    ExpressionParser parser = new SpelExpressionParser();
    String value = null;
    
    for (int i = ; i < 10 ; i ++) {
      stdContext.setVariable("num"new Integer(i));
      value = parser.parseExpression(
          "#num + ' is ' + (#num%2 == 0 ? 'even' : 'odd') "
          ).getValue(stdContext, String.class);
      System.out.println(value);
    }
    
  }

}
   

It gives the following output,
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd



 
  


  
bl  br