All MVC frameworks need to be responsible for parsing the HTTP request parameters and passing the request parameters to the controller component. At this point, the problem arises: the HTTP request parameters are all string types, but the Java language is strongly typed, so the MVC framework must convert these string parameters to the appropriate data types-a function that all MVC frameworks should provide.
The direction of the presentation layer data and the type conversions required are shown in the following figure:
STRUTS2 provides a very powerful type conversion mechanism, STRUTS2 type conversions can be based on OGNL expressions, as long as we name HTTP parameters (form elements and other get/post parameters) as legitimate ognl expressions, You can take advantage of the STRUTS2 type conversion mechanism.
In addition, STRUTS2 provides good extensibility, developers can very simply develop their own type converter, complete the conversion between string and custom composite type (for example, to complete the conversion of string to person instance), if there is an unknown exception in the type conversion, The type converter developer does not have to care about the logic of exception handling, and the Struts2 Conversionerror interceptor automatically handles the exception and generates a hint on the page. In summary, STRUTS2 's type converters provide a very powerful mechanism for performing data processing, and developers can use the STRUTS2 type conversion mechanism to accomplish arbitrary type conversions.
Struts2 built-in type converters
For most of the common types, developers do not have to pipe type conversions at all, and Struts2 can do most of the commonly used type conversions. These commonly used type conversions are done through the Struts2 built-in type converter, STRUTS2 has built a converter for converting between string types and the following types.
Boolean and Boolean: completes the conversion between the string and the Boolean value.
Char and character: completes the conversion between the string and the character.
int and integer: completes the type conversion between the string and the integer value.
Long and long: the type conversion between the completion string and the length value.
float and float: completes the conversion between a string and a single-precision floating-point value.
Double and double: completes the conversion between a string and a double-precision floating-point value.
Date: Completes the conversion between a string and a date type, using the short format of the locale that the user requested.
Array: By default, the array element is a string, and if the user provides a custom type converter, it can also be an array of other composite types.
Collection: By default, assume that the collection element type is string and create a new ArrayList encapsulates all the strings.
Because STRUTS2 provides these types of converters, if you need to convert the HTTP request parameters to these types above, you do not have to do any special processing by the developer at all. This is also the so-called framework of the benefits, will be a lot of basic services for developers to encapsulate the good, we directly use, greatly improve the development efficiency, but also for developers and reduce the workload.