Struts - SPLessons
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. SampleAction.java [java]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"}; sampleList.add("listItem1"); sampleList.add("listItem2"); sampleList.add("listItem3"); sampleMap.put(new Integer(1), "one"); sampleMap.put(new Integer(2), "two"); sampleMap.put(new Integer(3), "three"); user.setName("splesson"); } 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; } } [/java] 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. User.java [java]public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }[/java] Here just performed GET and SET methods on the name. index.jsp Following is the page to compile the program. [java]<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=sampleAction.action"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title></title> </head> <body> </body> </html>[/java] sample.jsp Following is the JSP code here strong code will be written. [java]<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Result</title> <s:head /> <style type="text/css"> @import url(style.css); </style> </head> <body> <div class="content"> <b>Array Usage Examples</b> <br><hr> <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> <br> <b>List Usage Examples</b> <br><hr> <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> <br> <b>Map Usage Examples</b> <br><hr> <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> <br> <b>Invoking a Method</b> <br><hr> <b>user.name :</b> <s:property value="user.name"/> <br> <b>quote() :</b> <s:property value="quote()"/> <br> </div> </body> </html>[/java] By using tag one can create a HTML drop down box. struts.xml Following is the struts.xml file where action class and result name needs to be placed. [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="default" extends="struts-default"> <action name="sampleAction" class="com.splessons.SampleAction"> <result name="success">/sample.jsp</result> </action> </package> </struts>[/xml] 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. web.xml Following is the web.xml file where URL pattern needs to be placed. [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.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> [/xml] style.css Following is the code of CSS should placed under WebContent. [java]@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; }[/java] Output When compile the program following output will be displayed.

Summary

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.