Introduction
This article shows you how to perform Struts 2 Type Conversion. Though for the data types like boolean, character, integer, arrays, and other fundamental types already defined in language, struts automatically auto wires the arguments with the help of its variety of type converters. But we should have knowledge of struts 2 type conversion as we may need to create our own type converters for user-defined arguments or types.
Struts 2 Type Conversion
Be it any data type, numbers, booleans, dates, integers, decimals, or anything else, all these types are treated as strings in HTTP requests which makes it very important for the strut to have type conversion while using these data types. So let's see how the struts use its variety of type converters to do this struts 2 type conversion.
Struts automatically change the request type to its original type without your intervention if your action class contains a type among the fundamental types already defined. Struts have a variety of type converters by default.
These fundamental types about which we shouldn't worry include:
- Integer, Float, Double, Decimal
- Arrays and Collections
- Date and Datetime
- Boolean
- BigDecimal
- Enumerations
But we should be careful while using our own data types because it may be required to include your own converters when using your own data type so that Struts knows how to transform the values before displaying them.
Below is an example to demonstrate struts 2 type conversion:
Look at POJO class Env.java
package com.Coding Ninjas Studio.struts2;
public class Env {
private String name;
public Env(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Let's make SysD.java, a new class that includes details about the system.
You would obtain this information from the system settings in a real-time project.
Let's create the following action class:-
package com.Coding Ninjas Studio.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class SysD extends ActionSupport {
private Env environment = new Env("Development");
private String operatingSystem = "Windows XP SP3";
public String execute() {
return SUCCESS;
}
public Env getEnvironment() {
return environment;
}
public void setEnvironment(Env environment) {
this.environment = environment;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
}
Let's next construct a straightforward JSP file system. jsp to show information about the operating system and the environment.
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"
pageEncoding = "ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SysD</title>
</head>
<body>
Environment: <s:property value = "environment"/><br/>
Operating System:<s:property value = "operatingSystem"/>
</body>
</html>
Let's use struts.xml to connect the system.jsp and the SysD.java class.
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloworld" extends = "struts-default">
<action name = "system"
class = "com.Coding Ninjas Studio.struts2.SysD"
method = "execute">
<result name = "success">/System.jsp</result>
</action>
</package>
</struts>
Struts can display and convert other built-in data types, such as the string "Windows XP SP3," but it cannot handle the Environment type's property. In the class, it is called the function toString() method.
Let's now develop and register a basic TypeConverter for the Env class to address this issue.
The following code should be placed in a class called EnvConverter.java.
package com.Coding Ninjas Studio.struts2;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class EnvConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class clazz) {
Env e = new Env(values[0]);
return e;
}
@Override
public String convertToString(Map context, Object value) {
Env e = (Env) value;
return e == null ? null : e.getName();
}
}
By overriding two methods, convertFromString() and convertToString(), the EnvConverter class extends the StrutsTypeConverter class and instructs Struts on how to convert Environment to a String and vice versa ().
It is necessary to generate a property file with the name "[action-class]converstion.properties" if the converter will only be utilized in that action.
In our instance, the following registry entry is created in a file called SysD-converstion.properties:-
environment = com.Coding Ninjas Studio.struts2.EnvConverter
In the example mentioned above, we are instructing Struts to use the EnvConverter to convert to and from the property with the name "environment" found in the SysD.java class.
com.Coding Ninjas Studio.struts2.Env = \
com.Coding Ninjas Studio.struts2.EnvConverter
By registering the converter globally, Struts can do the conversion whenever it comes across an object of the Environment type.
Congratulations, you have learned how to apply struts 2 type conversion by creating your own type converters.
Let's not forget to have sight of the frequently asked questions.