OGNL in struts 2

OGNL refers to Object Graph Navigation Language. It is mainly used to set or get the value of a java bean property.

Use of OGNL in struts 2.

  1. 1. As an expression language it is used in forms and response UI pages.
  2. 2. It is used to access the objects in ValueStack.
  3. 3. It is used in type conversion. When a form is submitted form values are transmitted as string values. OGNL converts these values to the specific types at the time of setting these values to the java beans.

Syntax of accessing the action property using OGNL.

<s:property value=”propertyName”/>

Syntax of accessing the property from a scope (request, session or application) using OGNL.

<s:property name=”#scopeName. propertyName “/>  

Example:

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
 
<html>
	<head>
		<title>Struts 2 OGNL example</title>
	</head>
	<body>
		<h3>This is a OGNL example.</h3>	 
		<h4><a href="welcome">Click here </a>
           to see the list values by using OGNL.</h4>	 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
        </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>test.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="user" extends="struts-default">
		<action name="welcome" 
		           class="com.javawithease.action.Test">
			<result name="success">/welcome.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import java.util.ArrayList;
import java.util.List;
 
/**
 * This class is used as an action class.
 * @author javawithease
 */
public class Test {
	//data members
	private List<String> myList = 
		         new ArrayList<String>();
 
	//business logic
	public String execute(){
		myList.add("Bharat");
		myList.add("Richi");
		myList.add("Sahdev");
		myList.add("Rajesh");
		myList.add("Himanshu");
		myList.add("Vivek");
		myList.add("Shveta");
		myList.add("Bharti");
		return "success";	
	}
 
	public List<String> getMyList() {
		return myList;
	}
 
	public void setMyList(List<String> myList) {
		this.myList = myList;
	}		
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
 <head>
	<title>Struts 2 OGNL example</title>
 </head>
 <body>
	<h3>This is a OGNL example.</h3>
 
	<h5> All list items: <s:property value="myList" /> </h5>
	<h5> Second list item: <s:property value="myList[1]" /> </h5>
	<h5> List size: <s:property value="myList.size" /> </h5>
 
 </body>
</html>

Output:

struts 6 first
 
Click on the link.
struts 6 final
 
Download this example.
 
Next Topic: Value Stack in struts 2 with example.
Previous Topic: Multiple configuration file in Struts 2 with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.