Wednesday, January 4, 2017

ValueStack and OGNL in Struts2

OGNL expression language is simple and powerful. OGNL expression language helps in accessing the values stored on the ValueStack and in the ActionContext.
First let's see how to access an array of String variables using OGNL. In the action class we create a string array and initialize it as shown below.
package vaannila;

public class SampleAction {

    private  String[] sampleArray;
    {
    	sampleArray =  new String[]{"item1","item2","item3"};
    }
    public String execute()
    {
    	return "success";
    }
    public String[] getSampleArray() {
    	return sampleArray;
    }
    public void setSampleArray(String[] sampleArray) {
    	this.sampleArray = sampleArray;
    }
}
You can access the array values in the jsp page using the OGNL expression language in the following way.
<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>
The figure shows the syntax and the corresponding output.
Since our object is on top of the ValueStack we can access it using [0] notation. If our object was in the second position from the top, we will access it using the [1] notation.
We can also do this using the top keyword. top returns the value on top of the ValueStack.
Now let's see how to access an ArrayList using the OGNL expression language. In the action class we create and initialize the ArrayList as shown below.
package vaannila;

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

public class SampleAction {

    private  List<String> sampleList = new ArrayList<String>();
    {
        sampleList.add("listItem1");
        sampleList.add("listItem2");
        sampleList.add("listItem3");
    }
    public String execute()
    {
    	return "success";
    }
    public List<String> getSampleList() {
    	return sampleList;
    }
    public void setSampleList(List<String> sampleList) {
    	this.sampleList = sampleList;
    }
}
You can access the ArrayList values in the jsp page using the following syntax.
<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>
The figure shows the syntax and the corresponding output.
Now let's see how to access a Map using the OGNL expression language. In the action class we create and initialize the HashMap as shown below.
package vaannila;

import java.util.HashMap;
import java.util.Map;

public class SampleAction {

	private  Map<Integer,String> sampleMap = new HashMap<Integer,String>();
	private  String carMake;
	{
		sampleMap.put(new Integer(1), "one");
		sampleMap.put(new Integer(2), "two");
		sampleMap.put(new Integer(3), "three");
	}
	public String execute()
	{
		return "success";
	}
	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;
	}

}
You can access the Map values in the jsp page using the following OGNL expression language syntax.
<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>
You can also create a map in the jsp page using the following syntax.
<s:select list="#{'make1':'Ford', 'make2':'Honda', 'make3':'Toyota'}" name="carMake" label="Select "></s:select>
The figure shows the syntax and the corresponding output.
Now let's see how to access the name property of the User object in the Action class using the OGNL expression language. The SampleAction contains the following code.
package vaannila;

public class SampleAction {

    private  User user = new User();
    {
        user.setName("Eswar");
    }
    public String execute()
    {
        return "success";
    }

    public String getQuote()
    {
        return "Don't think, just do";
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

}
You need to use the second-level OGNL expression language to access the user name property.
<b>user.name :</b> <s:property value="user.name"/> <br>
You can also invoke a method in the action class in the following way. This will invoke the quote() method in the action class.
<b>quote() :</b> <s:property value="quote()"/> <br>
The figure shows the syntax and the corresponding output.

No comments:

Post a Comment

Interview Prep: Java full stack

 SOLID principle :  https://www.educative.io/answers/what-are-the-solid-principles-in-java Design Pattern:  Creational:  https://medium.com...