Type conversion is the process by which type-agnostic data is converted into more complex objects. These strategies are typically used in web applications to translate request parameters (strings) into the objects they represent. Although many web frameworks provide robust type conversion frameworks, many developers remain unaware of their powerful advantages and the simplicity with which they can be used.
Struts2 is one example of a web framework that provides a conversion approach that assists in translating request parameters into complex bean properties. By default, Struts2 will handle the conversion of parameters into primitives, collections (including indexed and mapped properties), and other simple objects (boxed types, dates, and arrays). Any time the framework identifies a request parameter that is mapped to a bean property of one of these types, it will utilize the default converter to translate the string value into the appropriate type. Developers often use these defaults without even realizing that they are part of a larger type conversion framework.
The real strength of the Struts2 Type Conversion Framework is the ability to add custom converters for use throughout the application. As an example, in the code snippet below, a type converter has been registered and will be used whenever a parameter name matches a bean property of type “com.threepillarsoftware.examples.PhoneNumber”. The converter that is registered extends “org.apache.struts2.util.StrutsTypeConverter” and implements two methods—one for converting from a string, the other for converting to a string.
Figure 1: Registering the converter in xwork-conversion.properties entry.
The Struts2 framework will ensure that the PhoneNumber converter is invoked any time that a PhoneNumber is submitted. If a failure occurs during conversion, it will automatically handle the exceptions by adding error messages to the current action and triggering the appropriate result.
Figure 2: Converting from a String to a PhoneNumber
Figure 3: Converting from a PhoneNumber to a String
One of the advantages Spring provides over other web frameworks is its ability to load framework components, including type converters, through an externally configured bean factory. By using the struts2-spring-plugin in conjunction with type conversion, developers easily can use dependency injection to provide a converter with services. These services can be used to materialize objects from persistence, or provide adapters to other conversion strategies.
Developers often spend a lot of time manually handling conversions on a request-by-request basis. By leveraging type conversion features in Struts2 and other frameworks that provide them, developers can significantly reduce the amount of boiler plate code they need to write.
About the Author
David DeWolf is the chief technical architect and founder of Three Pillar Software, Inc. He works with mid-sized and Fortune 1000 companies to establish corporate standards that promote best practices and agile development. David has over eight years of commercial software development experience and is a member of the Apache Software Foundation’s Struts, Tiles, and Portals projects. David actively participates in the Java Community Process as a member of the Java Portlet Specification Expert Group and is the author of various online publications.