Struts2 OGNL is the expression language where OGNL stands for Object-Graph Navigation Language. OGNL is tightly coupled in Struts2 and used to store form parameters as java bean variables in ValueStack and to retrieve the values from ValueStack in result pages.

Struts2 OGNL

Struts2 OGNL performs two important tasks – data transfer and type conversion.

OGNL in Struts2 takes the request parameters from the servlet request and transfer it to corresponding java variable.

Since we get request params as String but java bean variables can be String, int, array, list or any custom object, type conversion is also an important task and OGNL takes care of type conversion through it’s built-in type converters.

Struts2 OGNL is flexible and we can easily extend it to create our own custom converter class. We will first look into the OGNL usage with basic data types such as String, boolean, int, arrays and lists. Then we will create our own converter class for a custom java bean variable.

Struts2 OGNL Example

Our final project structure for Struts2 OGNL example looks like below image.

Struts2-OGNL-Example-Project

Struts 2 Configuration Files

web.xml

pom.xml

struts.xml

Configuration files are self understood and they are just to configure our application to use Struts 2 framework.

Struts2 OGNL Example Model Classes

MyJavaBean is the action bean class that we will use, notice the variable of type Rectangle. Since it’s a custom class, we need to implement our own converter class for this. We will look into this later.

Struts2 OGNL Example Action Class

Action class just returns the success page, there is no logic done here.

Struts2 OGNL Example Result Pages

home.jsp

home.jsp is used as input page where user can provide values and invoke welcome action.

welcome.jsp

welcome.jsp is just used to show the values used by the user as a proof that OGNL is taking care of data transfer as well as type conversion.

Before we move on to discuss on custom converter classes, let’s look at some important points in above implementation.

  1. Basic data types conversion is automatic, we don’t need to follow any special rules for them.
  2. Struts 2 takes care of converting String to Date also and we can use s:date to display it with specific format.
  3. Arrays and Lists can be used with name as well as index. If we use index, then we need to initialize the array in the bean. That’s why roles array is initialized in the bean class.
  4. Do not initialize the list variables in bean else it will throw error. OGNL takes care of initialization and populating values.
  5. OGNL in Struts2 also provides built-in support for Map that we can use in result pages.
  6. We can use iterator with multi-values data types such as List, Map, Array to traverse through them. We can use index or key to get specific values from these variables.

Struts2 OGNL Custom Type Converter

Creating and configuring custom type converter class is very easy. First step is to fix the input format for the custom class. For my example, I have fixed the user input to be R:x,y where x and y are rectangle variables and should be integers.

Second step is to implement the converter class. Type converter classes should implement com.opensymphony.xwork2.conversion.TypeConverter interface.

Since in web application, we always get the request in form of String and send response in the form of String, Struts 2 API provides a default implementation of TypeConverter interface, StrutsTypeConverter.

StrutsTypeConverter contains two abstract methods – convertFromString to convert String to Object and convertToString to convert Object to String. We will extend this class for custom type converter.

Notice that code is very simple and parse input string to object and vice versa.

Next step is to configure the type converter to be used for Rectangle type variables. There are two ways to configure this – first is to configure for specific action and second way is to configure globally.

For action specific converter, we can use com.opensymphony.xwork2.conversion.annotations.TypeConversion annotation and change the setter method like below.

Custom Type Converter for ModelDriven Action Classes

If Action class is implementing ModelDriven interface for java bean, another way is to create property file with name as {JavaBeanName}-conversion.properties and put it in the same package as java bean class, so we can create MyJavaBean-conversion.properties and put it in com.journaldev.struts2.model package with below data.

MyJavaBean-conversion.properties

For global conversion, as I have done in this project, we need to create xwork-conversion.properties properties file and make sure it’s in WEB-INF/classes directory. We need to provide the class name and converter as key-value pair. For us it’s

xwork-conversion.properties

Now when we will run our application, we will get following response pages.

Struts2-OGNL-Custom-Type-Converter-Response

That’s all for Struts2 OGNL example tutorial, I hope you liked it. Download project from below link and run yourself.

By admin

Leave a Reply

%d bloggers like this: