Home  >  Spring Core

Ternary Operator (if-then-else) in Spring Expression Language (EL)

By Arvind Rai, October 24, 2013
Find a simple example that is showing how to use ternary operator. Write expression as string. And parse it by parseExpression() method.

SpELTest.java
package com.concretepage;
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();
		String str = parser.parseExpression("2<5?'Ram':'Shyam' ").getValue(String.class);
		System.out.println(str);

	}
}
 
Output
Ram
Find another example which is more use full in real world while using Ternary Operator.

Student.java
package com.concretepage.bean;
public class Student {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isTall(String name){
		if("Ram".equalsIgnoreCase(name)){
		   return	false;
		}else{
		   return true;	
		}
	}
}
 


SpELTest.java
package com.concretepage;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.concretepage.bean.Student;
public class SpELTest {
	public static void main(String[] args) {
		ExpressionParser parser = new SpelExpressionParser();
		Student std = new Student();
		StandardEvaluationContext context = new StandardEvaluationContext(std);
		parser.parseExpression("name").setValue(context, "Ram");
		context.setVariable("tallGroup", "Tall Group");
		String expression = "isTall(#queryName)?  Name + ' belongs to '+ #tallGroup :  Name + ' does not belong to '+ #tallGroup";
		String queryResultString = parser.parseExpression(expression).getValue(context, String.class);
		System.out.println(queryResultString);
	}
}
 
Output
Ram belongs to Tall Group
Find pom.xml for JAR support.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.concretepage</groupId>
  <artifactId>spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring</name>
  
  <properties>
     <spring.version>3.2.2.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
      <scope>compile</scope>
    </dependency>
    
     <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
     <groupId>javax.inject</groupId>
     <artifactId>javax.inject</artifactId>
     <version>1</version>
    </dependency>
    
    <dependency>
	 <groupId>org.aspectj</groupId>
	 <artifactId>aspectjweaver</artifactId>
	 <version>1.5.4</version>
   </dependency>
   
   <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-agent</artifactId> 
     <version>2.5.6</version> 
     <scope>provided</scope> 
    </dependency> 
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>${spring.version}</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.codehaus.fabric3.api</groupId>
	 <artifactId>javax-jta</artifactId>
	 <version>1.1.1</version>
    </dependency>
           
  </dependencies>
  
</project>

 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us