Home  >  Spring Core

Elvis Operator in Spring Expression Language (EL)

By Arvind Rai, October 24, 2013
In ternary operator, we need to repeat variable two times and in case of Elvis operator we do not need to repeat the variable. But Elvis Operator acts same as ternary operator. So Elvis Operator is the short form of ternary operator. Now find the example how to use Elvis Operator in real life. For our example we are using spring version 3.2.2.RELEASE.

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();
		std.setName("Ram");
		StandardEvaluationContext context = new StandardEvaluationContext(std);
		String name = parser.parseExpression("Name?:'Shyam'").getValue(context, String.class);
		System.out.println(name);
		std.setName(null);
        name = parser.parseExpression("Name?:'Shyam'").getValue(context, String.class);
		System.out.println(name);
	}
}
 


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;	
		}
	}
}
Output
Ram
Shyam
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us