Struts 2 OGNL Expression Tutorial with Examples

By Arvind Rai, June 16, 2014
OGNL is Object Graph Navigation Language. OGNL is an expression that can set and get the property of java object. In struts 2 OGNL is used to associate Action class and UI components. OGNL uses a standard naming convention to evaluate the Expression. OGNL is the root object for the value stack. In Struts 2 value stack is a set of several objects but OGNL looks value stack as a single object. OGNL expression can set and get values from application, session, value stack, action, request, parameters, and attr.
Value stack is the root of OGNL. Action class resides in value stack so we directly fetch action class items using OGNL expression as
<s:property value="students"/> 
But in case of session or request we need to use # as below.
<s:property value="#session['sessionPropKey']"/> 
<s:property value="#request['requestPropKey']"/> 
In our struts 2 examples, we will deal with action class properties to

1. Fetch Array Using OGNL as <s:property value="students[0]"/>
2. Fetch List Using OGNL as <s:property value="studentList[0]"/>
3. Display Map Using OGNL as <s:property value="studentMap['A']"/>
4. Use next level OGNL expression as <s:property value="college.name"/>
5. Fetch action methods passing arguments Using OGNL as <s:property value="%{getSum(2,3)}" />

And display results on UI components. We are providing complete example to run the demo. Find the eclipse structure of our example.
Struts 2 OGNL Expression Tutorial with Examples

Display Array

Create an array in action class. Assign values in array. Create setter and getter methods for the defined property.
UserActionOne.java
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@Action("/userOne")
@ResultPath(value="/")
@Result(name="success",location="userOne.jsp")
public class UserActionOne extends ActionSupport{
	private  String[] students = {"Ramesh","Mahesh","Dinesh"};
	public String execute() {
	   return SUCCESS; 
	}
	public String[] getStudents() {
		return students;
	}
	public void setStudents(String[] students) {
		this.students = students;
	}
} 
In JSP we can fetch array values as below using OGNL expression.
userOne.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 OGNL Example</title>
</head>
<body>
<h3> Struts 2 Array OGNL Example </h3>

<br/><b>students: </b><s:property value="students"/>
<br/><b>students.length:</b> <s:property value="students.length"/>
<br/><b>students[0]:</b> <s:property value="students[0]"/>
<br/><b>students[1]:</b> <s:property value="students[1]"/>
<br/><b>students[2]:</b> <s:property value="students[2]"/>
<br/><b>top.students: </b><s:property value="top.students"/>

</body>
</html> </pre>
Use the link http://localhost:8080/Struts2Demo-1/user/userOne to see the output.
Struts 2 OGNL Expression Tutorial with Examples

Display List

To work with list, create a list property in action class. Assign some values to the list and provide setter and getter methods.
UserActionTwo.java
package com.concretepage.action;
import java.util.Arrays;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@Action("/userTwo")
@ResultPath(value="/")
@Result(name="success",location="userTwo.jsp")
public class UserActionTwo extends ActionSupport{
	private  List<String> studentList = Arrays.asList("Ramesh","Mahesh","Dinesh");
	public String execute() {
	   return SUCCESS; 
	}
	public List getStudentList() {
		return studentList;
	}
	public void setStudentList(List studentList) {
		this.studentList = studentList;
	}
} 
In JSP, list can be fetched in the same way as array using OGNL expression.
userTwo.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 OGNL Example</title>
</head>
<body>
<h3>Struts 2 List OGNL Example</h3>

<br/><b>studentList: </b><s:property value="studentList"/>
<br/><b>studentList.size():</b> <s:property value="studentList.size()"/>
<br/><b>studentList[0]:</b> <s:property value="studentList[0]"/>
<br/><b>studentList[1]:</b> <s:property value="studentList[1]"/>
<br/><b>studentList[2]:</b> <s:property value="studentList[2]"/>
<br/><b>top.studentList: </b><s:property value="top.studentList"/>

</body>
</html> 
Use the link http://localhost:8080/Struts2Demo-1/user/userTwo to see the output.
Struts 2 OGNL Expression Tutorial with Examples

Display Map

In the same way we can access map using OGNL. Create a map and assign values in action class. And provide setter and getter methods.
UserActionThree.java
package com.concretepage.action;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@Action("/userThree")
@ResultPath(value="/")
@Result(name="success",location="userThree.jsp")
public class UserActionThree extends ActionSupport{
	private  Map<String,String> studentMap = new HashMap<String,String>();
	{
		studentMap.put("A","Ramesh");
		studentMap.put("B","Mahesh");
		studentMap.put("C","Dinesh");
	}
	public String execute() {
	   return SUCCESS; 
	}
	public Map getStudentMap() {
		return studentMap;
	}
	public void setStudentMap(Map studentMap) {
		this.studentMap = studentMap;
	}
} 
In JSP use OGNL expression to fetch map with the key as usually we do in java.
userThree.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 OGNL Example</title>
</head>
<body>
<h3>Struts 2 Map OGNL Example</h3>

<br/><b>studentMap: </b><s:property value="studentMap"/>
<br/><b>studentMap.size():</b> <s:property value="studentMap.size()"/>
<br/><b>studentMap['A']:</b> <s:property value="studentMap['A']"/>
<br/><b>studentMap['B']:</b> <s:property value="studentMap['B']"/>
<br/><b>studentMap['C']:</b> <s:property value="studentMap['C']"/>
<br/><b>top.studentMap: </b><s:property value="top.studentMap"/>

</body>
</html> 
Use link http://localhost:8080/Struts2Demo-1/user/userThree to see output.
Struts 2 OGNL Expression Tutorial with Examples

Use Next Level OGNL

To use next level OGNL expression, we are introducing a user bean College where we define id and name.
College.java
package com.concretepage.vo;
public class College {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
} 
Use the college bean in action class and set some values. Define corresponding setter and getter method.
UserActionFour.java
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.concretepage.vo.College;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@Action("/userFour")
@ResultPath(value="/")
@Result(name="success",location="userFour.jsp")
public class UserActionFour extends ActionSupport{
    private College college = new College();
    {
    	college.setId(10);
    	college.setName("UP College");
    }
    public String execute() {
	return SUCCESS; 
    }
    public College getCollege() {
	return college;
    }
    public void setCollege(College college) {
	this.college = college;
    }
} 
In JSP use next level OGNL expression to evaluate the values.
userFour.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 OGNL Example</title>
</head>
<body>
<h3> Struts 2 Next Level OGNL Example </h3>

<br/><b>college.id: </b><s:property value="college.id"/>
<br/><b>college.name:</b> <s:property value="college.name"/>
<br/><b>college.name.length():</b> <s:property value="college.name.length()"/>

</body>
</html> 
Struts 2 OGNL Expression Tutorial with Examples

Fetch Action Methods Passing Arguments

In this example we will deal with action class methods for OGNL expression to evaluate methods. Methods can be two types, one that will not accept any arguments and second one that accepts arguments. In the action class we have taken both type of methods.
UserActionFive.java
package com.concretepage.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/user")
@Action("/userFive")
@ResultPath(value="/")
@Result(name="success",location="userFive.jsp")
public class UserActionFive extends ActionSupport{
  	public String execute() {
	   return SUCCESS; 
	}
	public String getMsg(){
		return "Hello World!";
	}
	public int getSum(int a, int b){
		return a+b;
	}
} 
In JSP OGNL framework uses different approaches to fetch methods. Keep attention on JSP code how to fetch these two different methods.
userFive.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Struts 2 OGNL Example</title>
</head>
<body>
<h3> Struts 2 Method OGNL Example </h3>

<br/><b>getMsg(): </b><s:property value="msg"/>
<br/><b>getSum(a,b): </b><s:property value="%{getSum(2,3)}" />

</body>
</html>  
Struts 2 OGNL Expression Tutorial with Examples

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us