Analyze struts2 type conversion

Source: Internet
Author: User

I. Overview

In B/S applications, the user data received by the server can only be strings or character arrays. However, in Java programs on the server side, data can be of various types, such as integer, floating point, date type, and various custom data types. Therefore, after receiving the string data from the client, we need to convert the data to the correct type. Converting string request parameters to corresponding data types is a function provided by the MVC framework, while struts2 is a good MVC Framework implementer. Naturally, it also provides a type conversion mechanism.


Struts2 type conversion is based on ognl expressions, as long as we name HTML input items (form elements and other get/poet parameters) as valid ognl expressions, the struts2 type conversion mechanism can be fully utilized. Struts2 has a converter for the basic data type, and also supports conversion of Java. util. date type. For the date type, you only need to input data in a format similar to (note that the default language of the browser should be Chinese) in the text input box. After submission, the data is automatically converted to Java. util. date type in the action.


In addition, struts2 provides excellent scalability. developers can easily develop their own Type converters to convert strings and custom composite types.


In short, the struts2 type converter provides a very powerful presentation layer data processing mechanism, which can be used to complete any type conversion.

Ii. Implement custom type converter


You need to implement the ognl. typeconverter interface or inherit the ognl. defaulttypeconverter class (this class implements the typecoverter interface)

Override the convertvalue (MAP context, object value, class totype) method to build your own type converter. The context parameter is the map object that represents the ognl context, and the value is the object that requires type conversion, totype is the target type to be converted.

To simplify the implementation of the type converter, struts2 provides the org. Apache. struts2.util. strutstypeconverter abstract class, which is a subclass of defaulttypeconverter and provides two methods for different conversion directions:

Object convertfromstring (MAP context, string [] values, class toclass );
The context parameter is a map object that represents the action context, values is the string value to be converted, and toclass is the target type to be converted.

String converttostring (MAP context, object O );
The context parameter indicates the map object of the action context, and O indicates the object to be converted.

3. Register a custom type converter (this step is very important. Through the attribute file, you can associate the converted attribute with the converter to perform conversion)

After writing a custom type converter class, you need to register the type converter in a web application before the struts2 framework can use the type converter normally.
You can register a type converter in three ways:

1. Register a local type converter: it only takes effect for the attribute in an action. (Common)

Files in the following format must be provided
File Name: ClassName-conversion.properties [classname is the name of the action that requires type conversion]
File Content: Multiple "propertyname (attribute name to be converted in action) = corresponding type converter class name (including package name)" items
Storage location: it is in the same path as the action class.

2. register the global type converter: It will take effect for all the attributes of a specific type of action.

Files in the following format must be provided
File Name: xwork-conversion.properties [fixed name, unchangeable]
File Content: Multiple "propertyclassname (full name of the class of the attribute to be converted in action) = corresponding type converter class name (including package name)" items
Storage location: WEB-INF \ Classes \, that is, under the src directory.
 
3. Register a type converter using Annotations: generate a type converter using annotations.
There are 6 Annotations: typeconversion, conversion, element, key, keyproperty, createifnull
Typeconversion: applies to attributes and methods.
Conversion: applies to the type level, that is, class, interface, or enumeration.
Element: Specifies the element type in the collection or map, which is applied to the attribute and method level.
Key: Specifies the key type in the map, which is applied to the attribute and method level.
Keyproperty: the attribute name used to index the collection element. It is applied to the attribute and method level.
Createifnull: whether to allow the framework to create a null element in the specified set, which is applied to attributes and methods.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.