Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
Spring Expression (SpEL) Ternary and Elvis Operator with Example
6272 Views
Hints 
Below is an example of "Spring Expression (SpEL) Ternary and Elvis Operator with Example"
Step.1 Start a Java Project with required jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "TernaryOperatorExample", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview 
RunMyProgram.java
package com.springexample;

import java.util.Iterator;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunMyProgram {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		UserNameHolder userNameDetails = (UserNameHolder) context.getBean("userNameHolder");
		
		System.out.println("Usernames");
		for (Iterator nameIterator = userNameDetails.getUserNames().iterator();nameIterator.hasNext();){
			String name = nameIterator.next();
			System.out.println(name);
		}
	}
}
UserNameHolder.java
package com.springexample;

import java.util.ArrayList;
import java.util.List;

public class UserNameHolder {
	List userNames = new ArrayList();

	public List getUserNames() {
		return userNames;
	}
	public void setUserNames(List userNames) {
		this.userNames = userNames;
	}
}
UserNameDetails.java
package com.springexample;

public class UserNameDetails {
	private String firstName;
	private String lastName;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="johnPeterDetails" class="com.springexample.UserNameDetails">
		<property name="firstName" value="#{'John'}" />
		<property name="lastName" value="#{'Peter'}" />
	</bean>
	
	<bean id="ramDetails" class="com.springexample.UserNameDetails">
		<property name="firstName" value="#{'Ram'}" />
	</bean>
	
	<bean id="userNameHolder" class="com.springexample.UserNameHolder">
		<property name="userNames">
			<list>
				<value>#{johnPeterDetails.firstName+" "+(johnPeterDetails.lastName != null?johnPeterDetails.lastName:' Indian')}</value>
				<value>#{ramDetails.firstName+" "+(ramDetails.lastName?:'Indian')}</value>
			</list>
		</property>
	</bean>
</beans>
Output 
Usernames
John Peter
Ram Indian
Download as Zip 
Link to download
TernaryOperatorExample

Hints.
Click on File menu. then click "Download"
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details