Struts - SPLessons

Struts 2 OGNL

Chapter 18

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 OGNL

Struts 2 OGNL

shape Description

Struts 2 OGNL stands for Object Graph Navigation Language is an expression library. It will be tightly integrated into the entire framework, while doing coding developer will use this concept because it’s expression library. Struts 2 OGNL in Struts 2 is used inside of the templating languages. ValueStack is the root instance of OGNL. The main functionality of OGNL is retrieving the information or values from ValueStack where ValueStack is utilized to store the values. Following is an example which describes different syntax’s for getting collection classes.

shape Example

Following is an example which describes more about the Struts 2 OGNL and following is the java file where required items and username are mentioned.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SampleAction {

	private  String[] sampleArray;
	private  List<String> sampleList = new ArrayList<String>();
	private  Map<Integer,String> sampleMap = new HashMap<Integer,String>();
	private  String carMake;
	private  User user = new User();
	private  static String STR;
		sampleArray =  new String[]{"item1","item2","item3"};
		sampleMap.put(new Integer(1), "one");
		sampleMap.put(new Integer(2), "two");
		sampleMap.put(new Integer(3), "three");
	static {
		STR = "This is a static string variable";
	public String execute()
		return "success";
	public String quote()
		return "Stop thinking start coding";
	public static String proverb()
		return "Stay Hungry. Stay Foolish";
	public String[] getSampleArray() {
		return sampleArray;
	public void setSampleArray(String[] sampleArray) {
		this.sampleArray = sampleArray;
	public List<String> getSampleList() {
		return sampleList;
	public void setSampleList(List<String> sampleList) {
		this.sampleList = sampleList;
	public Map<Integer, String> getSampleMap() {
		return sampleMap;
	public void setSampleMap(Map<Integer, String> sampleMap) {
		this.sampleMap = sampleMap;
	public String getCarMake() {
		return carMake;
	public void setCarMake(String carMake) {
		this.carMake = carMake;
	public User getUser() {
		return user;
	public void setUser(User user) {
		this.user = user;

	public static String getStaticStr() {
		return STR;

	public static void setStaticStr(String STR) {
		SampleAction.STR = STR;

Sometime the developer want to create and initialize List like ArrayList or LinkedList in one line much like creating array and initializing it on same line. If anyone look Array on Java programming language you can create and initialize both primitive and object array, for example String array very easily in just one line but in order to create a List equivalent of that array, you need to type lot of code.

public class User {
	private String name;

	public String getName() {
		return name;

	public void setName(String name) { = name;

Here just performed GET and SET methods on the name.
Following is the page to compile the program.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=sampleAction.action">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


Following is the JSP code here strong code will be written.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@taglib uri="/struts-tags" prefix="s" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<s:head />
<style type="text/css">
@import url(style.css);
<div class="content">

<b>Array Usage Examples</b>
<b>sampleArray :</b> <s:property value="sampleArray"/> <br>
<b>sampleArray.length :</b> <s:property value="sampleArray.length"/> <br>
<b>sampleArray[0] :</b> <s:property value="sampleArray[0]"/> <br>
<b>[0].sampleArray :</b> <s:property value="[0].sampleArray"/> <br>
<b>top.sampleArray :</b> <s:property value="top.sampleArray"/> <br>

<b>List Usage Examples</b>
<b>sampleList :</b> <s:property value="sampleList"/> <br>
<b>sampleList.size :</b> <s:property value="sampleList.size"/> <br>
<b>sampleList[0] :</b> <s:property value="sampleList[0]"/> <br>

<b>Map Usage Examples</b>
<b>sampleMap[1] :</b> <s:property value="sampleMap[1]"/> <br>
<b>sampleMap.size :</b> <s:property value="sampleMap.size"/> <br>
<s:select list="#{'make1':'Ford', 'make2':'Honda', 'make3':'Toyota'}" name="carMake" label="Select "></s:select><br>

<b>Invoking a Method</b>
<b> :</b> <s:property value=""/> <br>
<b>quote() :</b> <s:property value="quote()"/> <br>

By using tag one can create a HTML drop down box.
Following is the struts.xml file where action class and result name needs to be placed.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <package name="default" extends="struts-default">
        <action name="sampleAction" class="com.splessons.SampleAction">
            <result name="success">/sample.jsp</result>

Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.
Following is the web.xml file where URL pattern needs to be placed.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Following is the code of CSS should placed under WebContent.

@CHARSET "ISO-8859-1";

.content {
font-family: sans-serif;
font-size: small;

.content b {


.songTable {
border-bottom: 1px, solid, black;
border-top: 1px, solid, black;
border-left: 1px, solid, black;
border-right: 1px, solid, black;
border-collapse: separate;
padding: 4px;

.odd {
background-color: yellow;

.even {
background-color: olive;

When compile the program following output will be displayed.


shape Key Points

  • Struts 2 OGNL is the powerful expression.
  • Struts 2 OGNL is utilized to getting an elements from ValueStack.
  • Struts 2 OGNL will have good naming convention to classify an expression.